By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,905 Members | 2,075 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,905 IT Pros & Developers. It's quick & easy.

Undefined variable php error guestbook app

P: 1
Hi im making something like guestbook. I have a form with fields like name , email , comment. Im trying to add the content of the fields to table and view it. Im beginner in php can someone correct errors for me ???

Notice: Undefined variable: result in C:\xampp\htdocs\addguestbook.php on line 56
ERROR


this is my code :


Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $host="localhost"; // Host name
  3. $username="user"; // Mysql username
  4. $password="pass"; // Mysql password
  5. $db_name="bai2011_cba_pl"; // Database name
  6. $tbl_name="guestbook"; // Table name
  7.  
  8. // Connect to server and select database.
  9. mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
  10. mysql_select_db("$db_name")or die("cannot select DB");
  11.  
  12.  
  13.  
  14.  
  15. if (isset($_POST['submit']))
  16.   {
  17.     $name=$_POST['name'];
  18.  
  19.     $email=$_POST['email'];
  20.  
  21.     $comment=$_POST['comment'];
  22.  
  23.  
  24.  if(!$name || !$comment)
  25.     {
  26.       print "<font color='red'>Name or comment not entered, please go back and sign again</font><br>";
  27.     }
  28.  
  29.    else
  30.     {
  31.  
  32.      $datetime=date("D M d, Y H:i:s");    
  33.      $putinguestbook="INSERT INTO gbook(name, email, comment) VALUES('$name','$email','$comment','$day')";
  34.   $result=mysql_query($putinguestbook);
  35.  
  36.  
  37.  
  38.     }
  39.   }
  40.  
  41.          if($result){
  42. echo "Successful";
  43. echo "<BR>";
  44. echo "<a href='viewguestbook.php'>View guestbook</a>"; // link to view guestbook page
  45. }
  46.  
  47. else {
  48. echo "ERROR";
  49. }
  50.  
  51.  
  52.  
  53. mysql_close();
  54. ?>

This is my form :

Expand|Select|Wrap|Line Numbers
  1. <table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
  2. <tr>
  3. <td><strong>Test Sign Guestbook </strong></td>
  4. </tr>
  5. </table>
  6. <table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
  7. <tr>
  8. <form id="form1" name="form1" method="post" action="addguestbook.php">
  9. <td>
  10. <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
  11. <tr>
  12. <td width="117">Name</td>
  13. <td width="14">:</td>
  14. <td width="357"><input name="name" type="text" id="name" size="40" /></td>
  15. </tr>
  16. <tr>
  17. <td>Email</td>
  18. <td>:</td>
  19. <td><input name="email" type="text" id="email" size="40" /></td>
  20. </tr>
  21. <tr>
  22. <td valign="top">Comment</td>
  23. <td valign="top">:</td>
  24. <td><textarea name="comment" cols="40" rows="3" id="comment"></textarea></td>
  25. </tr>
  26. <tr>
  27. <td>&nbsp;</td>
  28. <td>&nbsp;</td>
  29. <td><input type="submit" name="Submit" value="Submit" /> <input type="reset" name="Submit2" value="Reset" /></td>
  30. </tr>
  31. </table>
  32. </td>
  33. </form>
  34. </tr>
  35. </table>
  36. <table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
  37. <tr>
  38. <td><strong><a href="viewguestbook.php">View Guestbook</a> </strong></td>
  39. </tr>
  40. </table>

and this is my table :

`guestbook`
`id` int(4) NOT NULL auto_increment,
`name` varchar(65) NOT NULL default '',
`email` varchar(65) NOT NULL default '',
`comment` longtext NOT NULL,
`datetime` varchar(65) NOT NULL default '',
PRIMARY KEY (`id`)
)ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Nov 13 '11 #1
Share this Question
Share on Google+
1 Reply


Dormilich
Expert Mod 5K+
P: 8,639
the variable $result is only initialised in the else part of the condition (line #24). if the condition matches, $result is not initialised and therefore the condition using $result emits said error. to prevent the error you need to intialise this variable in both cases.
Nov 16 '11 #2

Post your reply

Sign in to post your reply or Sign up for a free account.