PDA

View Full Version : undefined variable


kontrol
05-21-2011, 11: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.

<?php
$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
$posts_id=$_POST['posts_id'];

// 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);
$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
$sub_id=$_POST['sub_id'];
$posts_id=$_POST['posts_id'];
$rep_by=$_POST['rep_by'];
$details=$_POST['details'];
$rep_id=$_POST['rep_id'];
$rep_mail=$_POST['rep_mail'];

$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')";
$result2=mysql_query($sql2);

if($result2){
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
$tbl_name2="forum_posts";
$sql3="UPDATE $tbl_name2 SET reply='$Max_id' WHERE posts_id='$posts_id'";
$result3=mysql_query($sql3);

}
else {
echo "ERROR";
}

mysql_close();
?>

edbr
05-23-2011, 08: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

$Max_id++;

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