Jump to content

Can't work out why there's a MySQL error


Seaholme

Recommended Posts

I've been trying to make this script work (to create a Forum) that I found here: http://www.tutorialized.com/view/tutorial/Creating-simple-PHP-forum-tutorial/9962 and all of it works except for the script to add an answer to the topic. Other people's comments on that tutorial page seem to suggest that this script works, so I've been scratching my head to try and get it to function. However, I've been trying for ages to figure out what this error is referring to and can't work out what's wrong! This is the error I keep getting:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/content/91/6351791/html/add_reply.php on line 18

ERROR

 

I've highlighted line 18 with ***LINE 18.

 

Can anybody help me figure out whereabouts the problems might lie? I'm quite new to php (3 days of trying to work it out, now xD) and so I dunno if it's quite obvious or not. Sorry if it is! I'll be extremely grateful if anybody could tell me what it might be, or at least point me in the direction of what might be wrong.

 

<?php
$host="xx"; // Host name 
$username="xx"; // Mysql username 
$password="xx"; // Mysql password 
$db_name="xx"; // Database name 
$tbl_name="forum_question"; // Table name

// Connect to server and select database.
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); ***LINE 18

// 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_answer=$_POST['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_answer, a_datetime)VALUES('$id', '$Max_id', '$a_name', '$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();
?>

 

Thanks very much! :)

Link to comment
Share on other sites

Thanks for the reply.

I tried

echo $result;

and it came out with a blank, so you're right, that seems to be where the problem lies.

 

Also did what you said for the other bit and it very handily told me what was missing! :D Awesome stuff.

 

Unknown column 'a_id' in 'field list'

 

That's its new (well, more specific!) problem. I checked the database to make sure I didn't misname anything and there definitely is an a_id there. Does this mean there's some sort of bit missing from the code where a_id is meant to have been defined? If so, I don't understand how this worked for other people! o.O

 

Any thoughts?

Link to comment
Share on other sites

Hey,

 

I tried it with the backwards thingys and it didn't work, so I tried it with ' instead and it DID work... leading me onto another error which revealed where the problem was! I copied & pasted something and managed to get it all inserting into the wrong table. Smart, right? xP

 

Thanks so much for your help!

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.