Jump to content


Forum coding problem (add reply part)


7 replies to this topic

#1 AnonyDeath

    Young Padawan

  • Members
  • Pip
  • 6 posts

Posted 11 November 2006 - 12:29 PM

(database details have been removed)
Full code to add reply:

<?php
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// 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 $tbl_name 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_name'];
$a_email['a_email'];
$a_answer['a_answer'];

$datetime=date("d/m/y H:i:s"); // create date and time

// Insert answer
$sql2="INSERT INTO $tbl_name(question_id, a_id, a_name, a_email, a_answer, a_datetime)VALUES('$id', '$Max_id', '$a_name', '$a_email', '$a_answer', '$datetime')";
$result2=mysql_query($sql2);

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

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

}
else {
echo "ERROR";
}

mysql_close();
?>

I seem to have a problem with the following line(s?):

// get values that sent from form
$a_name=$POST['a_name'];
$a_email['a_email'];
$a_answer['a_answer'];

I am new to PHP and I would love to figure this one out, so I can continue with my forum. ^^;

The error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/httpd/vhosts/*********/subdomains/********/httpdocs/!forum/add_reply.php on line 18
ERROR

#2 cheerio

    Young Padawan

  • Members
  • Pip
  • 246 posts
  • Gender:Male

Posted 11 November 2006 - 01:27 PM

$a_name = $_POST['a_name'];
$a_email = $_POST['a_email'];
$a_answer = $_POST['a_answer'];

Also you need to set the $tbl_name variable

Edited by cheerio, 11 November 2006 - 01:27 PM.


#3 AnonyDeath

    Young Padawan

  • Members
  • Pip
  • 6 posts

Posted 11 November 2006 - 01:35 PM

Thanks for your help.
But.. I'm clueless as to where to put the $tbl_name variable.

#4 Mr. Matt

    Moderator

  • P2L Staff
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 11 November 2006 - 01:42 PM

can go here:

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$tbl_name = 'table_name';

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

I would also tidy up that query:

"SELECT MAX(`a_id`) AS `Maxa_id` FROM `".$tbl_name."` WHERE `question_id` = '".$id."'"

Matt

#5 AnonyDeath

    Young Padawan

  • Members
  • Pip
  • 6 posts

Posted 11 November 2006 - 02:03 PM

Hmm... with all the changes, I still get the exact same error. o.O;

#6 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 11 November 2006 - 02:09 PM

So, rather then throwing darts blindly, lets narrow the problem down.

Find all instances of mysql_query(); in there, and add some error handling. Like so:
mysql_query($sql);
// Change to
mysql_query($sql) or die(mysql_error());


#7 AnonyDeath

    Young Padawan

  • Members
  • Pip
  • 6 posts

Posted 11 November 2006 - 03:06 PM

View Postrc69, on Nov 11 2006, 10:09 PM, said:

So, rather then throwing darts blindly, lets narrow the problem down.

Find all instances of mysql_query(); in there, and add some error handling. Like so:
mysql_query($sql);
// Change to
mysql_query($sql) or die(mysql_error());

Result: Unknown column 'a_id' in 'field list'

and when I changed some things: Unknown column 'question_id' in 'where clause'

#8 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 11 November 2006 - 03:12 PM

Ok, now echo your SQL statement before sending it to the mysql_query() function, and double-check your table to make sure the columns exist.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users