View Full Version : undefined variable

05-21-2011, 10:50 AM
i will grateful to receive help on this problem. the code below is intended to send form values to a db table and update another db table's with a +1 but it displays an error message saying " undefined variable in rows on line 319 which the line i have marked with asterisk (max_id). i have tried all i could.

$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="urbansystems"; // Database name
$tbl_name="forum_replies"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Get value of id that sent from hidden field

// Find highest answer number.
$sql="SELECT MAX(reply_id) AS Maxa_id FROM $tbl_name WHERE forum_post_id='$posts_id'";
$result=mysql_query($sql) or die(mysql_error());

// add + 1 to highest answer number and keep it in variable name "$Max_id". if there no answer yet set it = 1
* if ($rows) { *
$Max_id = $rows['Maxa_id']+1;
else {
$Max_id = 1;
* } *

// get values that sent from form

$datetime=date("d/m/y H:i:s"); // create date and time

// Insert answer
$sql2="INSERT INTO $tbl_name(sub_cat_id, forum_post_id, reply_by, reply_details, rep_by_id, rep_by_mail, datetime)VALUES('$sub_id', '$Max_id', '$posts_id','$rep_by','$details', '$rep_id', '$rep_mail', '$datetime')";

echo "<h2>Successful posting</h2><BR>";
echo "<a href='replies.php?posts_id=".$post_id."'>Click here to go back</a>";

// If added new answer, add value +1 in reply column
$sql3="UPDATE $tbl_name2 SET reply='$Max_id' WHERE posts_id='$posts_id'";

else {
echo "ERROR";


05-23-2011, 07:05 AM
i would first get the value

* if ($rows) { *
$Max_id = $rows['Maxa_id'];
else {
$Max_id = 1;
* }

the normal way to increment is to use


that should ,i say with fingers slightly crossed, increment the value of either the obtained value or from 1 if no record