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
Share on other sites

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);

Link to comment
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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.