Seaholme Posted July 1, 2010 Share Posted July 1, 2010 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 https://forums.phpfreaks.com/topic/206457-cant-work-out-why-theres-a-mysql-error/ Share on other sites More sharing options...
Pikachu2000 Posted July 1, 2010 Share Posted July 1, 2010 It usually means the query failed. Echo the query to make sure it contains the values you'd expect it to contain, and change the query execution to report errors $result=mysql_query($sql) or die(mysql_error()); Link to comment https://forums.phpfreaks.com/topic/206457-cant-work-out-why-theres-a-mysql-error/#findComment-1079994 Share on other sites More sharing options...
Seaholme Posted July 1, 2010 Author Share Posted July 1, 2010 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! 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 https://forums.phpfreaks.com/topic/206457-cant-work-out-why-theres-a-mysql-error/#findComment-1080001 Share on other sites More sharing options...
Pikachu2000 Posted July 2, 2010 Share Posted July 2, 2010 Try it with backticks enclosing the field name MAX(`a_id`) Link to comment https://forums.phpfreaks.com/topic/206457-cant-work-out-why-theres-a-mysql-error/#findComment-1080069 Share on other sites More sharing options...
Seaholme Posted July 2, 2010 Author Share Posted July 2, 2010 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 https://forums.phpfreaks.com/topic/206457-cant-work-out-why-theres-a-mysql-error/#findComment-1080184 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.