forum reply help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simmsy
Forum Newbie
Posts: 24
Joined: Wed Apr 14, 2010 9:50 am

forum reply help

Post 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
klandaika
Forum Newbie
Posts: 19
Joined: Wed Mar 30, 2011 3:25 pm
Location: New York

Re: forum reply help

Post 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']);
simmsy
Forum Newbie
Posts: 24
Joined: Wed Apr 14, 2010 9:50 am

Re: forum reply help

Post 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?
klandaika
Forum Newbie
Posts: 19
Joined: Wed Mar 30, 2011 3:25 pm
Location: New York

Re: forum reply help

Post 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.
simmsy
Forum Newbie
Posts: 24
Joined: Wed Apr 14, 2010 9:50 am

Re: forum reply help

Post by simmsy »

yea thats sorted now but it wont let me reply on multiple topics only on one?
Post Reply