Page 1 of 1

forum reply help

Posted: Sun Apr 03, 2011 2:45 pm
by simmsy
Hi can anyone help and tell me whats wrong with this code?

<?php
include "connect.php";

// Get value of id that sent from hidden field
$id=$_POST['id'];

// Find highest answer number.
$sql="SELECT MAX(a_id) AS Maxa_id FROM reply WHERE question_id='$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);

// 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
$a_name=$_POST['a_username'];
$a_answer=$_POST['a_reply'];

// Insert answer
$sql2="INSERT INTO reply(question_id, a_id, a_username, a_reply, a_date, a_time)VALUES('$id', '$Max_id', '$a_username', '$a_reply', CURDATE(), CURTIME())";
$result2=mysql_query($sql2);

if($result2){
echo "Successful<BR>";
echo "<a href='viewtopic.php?id=".$id."'>View your answer</a>";

// If added new answer, add value +1 in reply column
$sql3="UPDATE topic SET reply='$Max_id' WHERE id='$id'";
$result3=mysql_query($sql3);

}
else {
echo "ERROR";
}

mysql_close();
?>

but gives this error and dont know why?

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/fightwa1/public_html/addreply.php on line 10

please help Thanks

Re: forum reply help

Posted: Sun Apr 03, 2011 3:12 pm
by klandaika

Code: Select all

$result=mysql_query($sql);
must have returned no result. You should add a check for that

Code: Select all

$result=mysql_query($sql);
if(!$result){
//do some error handeling
}else{
$rows=mysql_fetch_array($result);
//process the rest of code
}
also you should use mysql_real_escape_string when you retrieve your id http://php.net/manual/en/function.mysql ... string.php

Code: Select all

$id=mysql_real_escape_string($_POST['id']);

Re: forum reply help

Posted: Sun Apr 03, 2011 4:40 pm
by simmsy
well it lets me enter a numerous replies but in only one topic and says error when I try and reply on another topic any suggestions?

Re: forum reply help

Posted: Sun Apr 03, 2011 6:12 pm
by klandaika
simmsy wrote: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/fightwa1/public_html/addreply.php on line 10
Is that the only error you get? or are there other errors?
make sure you use mysql_real_escape_string() on every $_POST[] that you get.

Re: forum reply help

Posted: Mon Apr 04, 2011 1:40 am
by simmsy
yea thats sorted now but it wont let me reply on multiple topics only on one?