Undefined variable php error guestbook app

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ravencrest
    New Member
    • Nov 2011
    • 1

    #1

    Undefined variable php error guestbook app

    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.p hp on line 56
    ERROR


    this is my code :


    Code:
    <?php
    $host="localhost"; // Host name
    $username="user"; // Mysql username
    $password="pass"; // Mysql password
    $db_name="bai2011_cba_pl"; // Database name
    $tbl_name="guestbook"; // Table name
    
    // Connect to server and select database.
    mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
    mysql_select_db("$db_name")or die("cannot select DB");
    
    
    
    
    if (isset($_POST['submit']))
      {
        $name=$_POST['name'];
        
        $email=$_POST['email'];
      
        $comment=$_POST['comment'];
      
    
     if(!$name || !$comment)
        {
          print "<font color='red'>Name or comment not entered, please go back and sign again</font><br>";
        }
        
       else
        {
     
         $datetime=date("D M d, Y H:i:s");    
         $putinguestbook="INSERT INTO gbook(name, email, comment) VALUES('$name','$email','$comment','$day')";
      $result=mysql_query($putinguestbook);
    	 
    	 
    	 
        }
      }
    
    	 	if($result){
    echo "Successful";
    echo "<BR>";
    echo "<a href='viewguestbook.php'>View guestbook</a>"; // link to view guestbook page
    }
     
    else {
    echo "ERROR";
    }
     
    
    
    mysql_close();
    ?>

    This is my form :

    Code:
    <table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
    <tr>
    <td><strong>Test Sign Guestbook </strong></td>
    </tr>
    </table>
    <table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
    <tr>
    <form id="form1" name="form1" method="post" action="addguestbook.php">
    <td>
    <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
    <tr>
    <td width="117">Name</td>
    <td width="14">:</td>
    <td width="357"><input name="name" type="text" id="name" size="40" /></td>
    </tr>
    <tr>
    <td>Email</td>
    <td>:</td>
    <td><input name="email" type="text" id="email" size="40" /></td>
    </tr>
    <tr>
    <td valign="top">Comment</td>
    <td valign="top">:</td>
    <td><textarea name="comment" cols="40" rows="3" id="comment"></textarea></td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td><input type="submit" name="Submit" value="Submit" /> <input type="reset" name="Submit2" value="Reset" /></td>
    </tr>
    </table>
    </td>
    </form>
    </tr>
    </table>
    <table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
    <tr>
    <td><strong><a href="viewguestbook.php">View Guestbook</a> </strong></td>
    </tr>
    </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 ;
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    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.

    Comment

    Working...