I am a beginner.
I am trying to create a guestbook. I have the code for it and it is in three parts. Part 1 "dp.php" to open database and make connection Part 2 "index.php" which will show the guestbook entries and Part 3 "add.php" to add new entry.
The problems is when i run any of the file nothing happens i.e. no error(s) and no result.
I tired to check the code so many times and couldnt any find any errors.
Part 1
Expand|Select|Wrap|Line Numbers
- <?php
- $user="user";
- $host="localhost";
- $password="password";
- $database = "test";
- $connection = mysql_connect($host,$user,$password) //open connection to MySQL
- or die ("couldn't connect to server");
- $db = mysql_select_db($database,$connection) // open session to Database
- or die ("Couldn't select database");
- mysql_select_db("$db", $connection);
- ?>
Expand|Select|Wrap|Line Numbers
- <?php
- ****$sql*=*"SELECT text*FROM*guestbook*ORDER*BY*insertdate*DESC";
- ****$result*=*mysql_query($sql);
- ?>
- <html>
- <body>
- ****<a href="add.php">Add*a*comment</a>
- ****<h1>Guestbook</h1>
- ****<br/><table>****
- <?php*while*($row*=*mysql_fetch_assoc($result))*{*?>
- *********<tr><th><?php*echo*$row['name']."*-*".$row['insertdate'];*?></th></tr>
- *********<tr><td><?php*echo*nl2br($row['text']);*?></td></tr>
- <?php*}*?>
- ****</table>****
- </body>
- </html>
Expand|Select|Wrap|Line Numbers
- <?php
- include 'dp.php';
- if (isset($_POST['submitBtn']))
- {
- $name = (isset($_POST['name'])) ? addslashes(htmlentities($_POST['name'])) : '' ;
- $comment = (isset($_POST['comment'])) ? addslashes(htmlentities($_POST['comment'])) : '' ;
- $actDate = date("d-m-Y H:i:s");
- //Minimum name and comment length.
- if ((strlen($name) > 2) && (strlen($comment) > 5))
- {
- $sql = "INSERT INTO guestbook (name,text,insertdate) VALUES ('$name','$comment','$actDate')";
- mysql_query($sql);
- }
- header("Location: index.php");
- }
- else {
- ?>
- <html>
- <body>
- <h1>Add comment</h1>
- <form action= method="post" name="gbook">
- <table>
- <tr><th>Name:</th><td><input type="text" name="name" id="name"/></td></tr>
- <tr><th>Comment:</th>
- <td><textarea name="comment" cols=30 rows=5></textarea></td>
- </tr>
- <tr><td colspan="2" align="center">
- <input type="submit" name="submitBtn" id="submitBtn" value="Add comment"/></td>
- </tr>
- </table>
- </form>
- </body>
- <?php } ?>
- </html>
Regards
Zeshan