Jump to content

forum reply help?


simmsy

Recommended Posts

Hi can anyone help this is my forum reply 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();

?>

 

and this is the error message anyone know why it gives this Thanks

 

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

Link to comment
https://forums.phpfreaks.com/topic/232581-forum-reply-help/
Share on other sites

Alright, use this code and try again:

<?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) or die("MySQL Error in '$sql': ".mysql_error());
$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) or die("MySQL Error in '$sql2': ".mysql_error());

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) or die("MySQL Error in '$sql3': ".mysql_error());

mysql_close();
?>

Link to comment
https://forums.phpfreaks.com/topic/232581-forum-reply-help/#findComment-1196670
Share on other sites

well it looks like its partly worked as I have a new error but it says this now:

 

MySQL Error in 'INSERT INTO reply(reply_id, a_id, a_username, a_reply, a_date, a_time) VALUES('2', '1', 'dfgdfg', 'dfgdfg', CURDATE(), CURTIME())': Duplicate entry '1' for key 1

 

any ideas how I get round this?

Link to comment
https://forums.phpfreaks.com/topic/232581-forum-reply-help/#findComment-1196695
Share on other sites

Ahhh, I think I know what your problem is. You're getting the MAX a_id but only where the question_id matches your $id, but a_id has to be unique even if the question_id is different. Just don't enter a value for a_id and let MySQL take care of it, since it's AUTO_INCREMENT.

// Insert answer
$sql2="INSERT INTO reply(question_id, a_username, a_reply, a_date, a_time) VALUES('$id', '$a_username', '$a_reply', CURDATE(), CURTIME())";
$result2=mysql_query($sql2) or die("MySQL Error in '$sql2': ".mysql_error());

 

and get rid of this section:

// Find highest answer number.
$sql="SELECT MAX(a_id) AS Maxa_id FROM reply WHERE question_id='$id'";
$result=mysql_query($sql) or die("MySQL Error in '$sql': ".mysql_error());
$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;
}

Link to comment
https://forums.phpfreaks.com/topic/232581-forum-reply-help/#findComment-1196813
Share on other sites

yea that was part of the problem but also took out the last query too so now the code is and finally works, thanks alot for your help finally got there lol!

 

<?php

include "connect.php";

 

// Get value of id that sent from hidden field

$id=$_POST['id'];

 

// get values that sent from form

$a_username=$_POST['a_username'];

$a_reply=$_POST['a_reply'];

 

// Insert answer

$sql2="INSERT INTO reply(reply_id, a_username, a_reply, a_date, a_time) VALUES('$id', '$a_username', '$a_reply', CURDATE(), CURTIME())";

$result2=mysql_query($sql2) or die("MySQL Error in '$sql2': ".mysql_error());

 

echo "Successful<BR>";

echo "<a href='viewtopic.php?id=".$id."'>View your answer</a>";

 

mysql_close();

?>

Link to comment
https://forums.phpfreaks.com/topic/232581-forum-reply-help/#findComment-1196823
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.