simmsy Posted April 3, 2011 Share Posted April 3, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/232581-forum-reply-help/ Share on other sites More sharing options...
dcro2 Posted April 3, 2011 Share Posted April 3, 2011 Your mysql_query is probably failing, so add a check for failure: // 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: ".mysql_error()); $rows=mysql_fetch_array($result); Quote Link to comment https://forums.phpfreaks.com/topic/232581-forum-reply-help/#findComment-1196330 Share on other sites More sharing options...
simmsy Posted April 3, 2011 Author Share Posted April 3, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/232581-forum-reply-help/#findComment-1196361 Share on other sites More sharing options...
dcro2 Posted April 3, 2011 Share Posted April 3, 2011 Could you tell us what that error is after using my code? Quote Link to comment https://forums.phpfreaks.com/topic/232581-forum-reply-help/#findComment-1196364 Share on other sites More sharing options...
simmsy Posted April 4, 2011 Author Share Posted April 4, 2011 theres no physical error with the code now it just says ERROR which the code said to say if it didn't post the message. It allows me to reply on one topic only when I try other topics it just says ERROR? Quote Link to comment https://forums.phpfreaks.com/topic/232581-forum-reply-help/#findComment-1196486 Share on other sites More sharing options...
dcro2 Posted April 4, 2011 Share Posted April 4, 2011 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(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/232581-forum-reply-help/#findComment-1196670 Share on other sites More sharing options...
simmsy Posted April 4, 2011 Author Share Posted April 4, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/232581-forum-reply-help/#findComment-1196695 Share on other sites More sharing options...
dcro2 Posted April 4, 2011 Share Posted April 4, 2011 Can you paste here how the reply table is structured? It looks like a_id is supposed to be UNIQUE or AUTO_INCREMENT and there's already a row with that a_id. Quote Link to comment https://forums.phpfreaks.com/topic/232581-forum-reply-help/#findComment-1196742 Share on other sites More sharing options...
simmsy Posted April 4, 2011 Author Share Posted April 4, 2011 Yea a_id is the primary key (int) and auto_increment Quote Link to comment https://forums.phpfreaks.com/topic/232581-forum-reply-help/#findComment-1196800 Share on other sites More sharing options...
dcro2 Posted April 4, 2011 Share Posted April 4, 2011 Then stop trying to insert a row with an a_id that already exists. That's what the error says. Quote Link to comment https://forums.phpfreaks.com/topic/232581-forum-reply-help/#findComment-1196803 Share on other sites More sharing options...
simmsy Posted April 4, 2011 Author Share Posted April 4, 2011 but shouldnt it be +1 to that id row every time so should be different, theres where im getting mixed up on Quote Link to comment https://forums.phpfreaks.com/topic/232581-forum-reply-help/#findComment-1196806 Share on other sites More sharing options...
dcro2 Posted April 4, 2011 Share Posted April 4, 2011 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; } Quote Link to comment https://forums.phpfreaks.com/topic/232581-forum-reply-help/#findComment-1196813 Share on other sites More sharing options...
simmsy Posted April 4, 2011 Author Share Posted April 4, 2011 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(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/232581-forum-reply-help/#findComment-1196823 Share on other sites More sharing options...
dcro2 Posted April 4, 2011 Share Posted April 4, 2011 No problem. Good luck and keep learning! Quote Link to comment https://forums.phpfreaks.com/topic/232581-forum-reply-help/#findComment-1196827 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.