Jump to content
Sign in to follow this  
francis Aneke

PLEASE AM STUCK WITH SQL SYNTAX ERROR PROBLEM!,HELP

Recommended Posts

Am designing a simple php forum platform with mysql database.

But on redirecting from a page called main_forum.php to a page called view_topic.php, am stuck with this error message------>

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE id='4'' at line 1

What should i do, please am dieing

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////main_forum.php////

<?phpinclude("connect_to_mysql.php");$forum_output='';$sql=mysql_query("SELECT * FROM forum_question ORDER BY id DESC LIMIT 6") or die(mysql_error()); while($row=mysql_fetch_array($sql)){ $id=$row['id']; $topic=$row['topic']; $view=$row['view']; $reply=$row['reply']; $email=$row['email']; $datetime=$row['datetime']; $forum_output.=' <table width="1300" border="0" cellspacing="2" cellpadding="6" align="center" ><tr> <th width="49" bgcolor="#004200" scope="col" height="10"><ul><li style="color: #FFF">ID</li></ul></th> <th width="362" bgcolor="#004200" scope="col" height="10"><ul><li style="color: #FFF">Your Topic</li></ul></th> <th width="120" bgcolor="#004200" scope="col" height="10"><ul><li style="color: #FFF">Views</li></ul></th> <th width="100" bgcolor="#004200" scope="col" height="10"><ul><li style="color: #FFF">Reply</li></ul></th> <th width="83" bgcolor="#004200" scope="col" height="10"><ul><li style="color: #FFF">Email</li></ul></th> <th width="100" bgcolor="#004200" scope="col" height="10"><ul><li style="color: #FFF">Date</li></ul></th> </tr><br /><br />'; $forum_output.=' <tr> <td bgcolor="#CCCCCC">'.$id.'</td> <td bgcolor="#CCCCCC"><a href="view_topic.php?id='.$id.'"><font size="large">'.$topic.'</font></a></td> <td bgcolor="#CCCCCC" align="center">Viewed by  '.$view.'  persons</td> <td bgcolor="#CCCCCC" align="center"><b>'.$reply.'</b></td> <td bgcolor="#CCCCCC">'.$email.'</td> <td bgcolor="#CCCCCC">'.$datetime.'</td> </tr> <tr> <td align="center" colspan="2" bgcolor="#CCCCCC">Create New Topic</td> </tr> </table><br /><br />'; }?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title><link href="style.css" rel="stylesheet" type="text/css" media="screen" /></head><body><div id="mainwrapper"> <div id="container"> <?php echo $forum_output; ?> <!---<table width="1300" border="0" cellspacing="1" cellpadding="6" align="center" > <!--- <tr> <th width="49" bgcolor="#004200" scope="col" style><h2 style="color: #FFF">ID</h2></th> <th width="362" bgcolor="#004200" scope="col"><h2 style="color: #FFF">TOPIC</h2></th> <th width="302" bgcolor="#004200" scope="col"><h2 style="color: #FFF">VIEWS</h2></th> <th width="264" bgcolor="#004200" scope="col"><h2 style="color: #FFF">REPLY</h2></th> <th width="83" bgcolor="#004200" scope="col"><h2 style="color: #FFF">EMAIL</h2></th> <th width="89" bgcolor="#004200" scope="col"><h2 style="color: #FFF">DATE</h2></th> </tr> <tr> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> </tr>----> <!-- <tr> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> </tr> </table> ---> </div></div></body></html>

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

///////view_topic.php///////

<?php///Error reportingerror_reporting(E_ALL);ini_set('display errors','1');include_once("connect_to_mysql.php");$topic_output='';$id=$_GET['id'];$sql="SELECT * FROM forum_question WHERE id='".$_GET['id']."'";$result=mysql_query($sql) or die(mysql_error()); $row = mysql_fetch_array($result); $name=$row['name']; $topic=$row['topic']; $view=$row['view']; $reply=$row['reply']; $email=$row['email']; $detail=$row['detail']; $datetime=$row['datetime']; $topic_output=' <table width="900" height="127" border="0" cellpadding="2" cellspacing="4" align="center"> <tr> <th scope="col"><li style="color:#004200;"><h3>'.$topic.'</h3></li></th> </tr> <tr> <td bgcolor="#CCCCCC"><div><p width="500px">'.$detail.'</p></div></td> </tr> <tr> <td bgcolor="#004200"><li style="color:white;"><strong>By:   '.$name.'</strong>             <strong> Email:   '.$email.'</strong></li></td> </tr> <tr> <td bgcolor="#004200"><li style="color:white;"><strong>Date/Time:        <font>'.$datetime.'</strong></li></td> </tr> <tr> <td> </td> </tr> </table>'; ?> <?php///Error reportingerror_reporting(E_ALL);ini_set('display errors','1');$answer_output='';$sql2=mysql_query("SELECT * FROM forum_answer WHERE question_id='$id'") or die(mysql_error());$row=mysql_fetch_array($sql2);while($row){ $a_id=$row['a_id']; $a_name=$row['a_name']; $a_email=$row['a_email']; $a_answer=$row['a_answer']; $a_detail=$row['a_detail']; $a_datetime=$row['a_datetime']; $answer_output.=' <table width="600" height="127" border="0" cellpadding="2" cellspacing="4" align="center"> <tr> <td><li style="color:#004200;"><strong>By:   '.$a_name.'</strong></li></td> </tr> <tr> <td bgcolor="#CCCCCC"><div><p width="500px" align="center">'.$a_detail.'</p></div></td> </tr> <tr> <td bgcolor="#004200"></li></td> </tr> <tr> <td bgcolor="#004200"><li style="color:white;"><strong>Date/Time:     '.$a_datetime.'                     <strong> Email:   '.$a_email.'</strong></strong></li></td> </tr> </table> '; }?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title></head><body><div align="center" style="margin:0px 24px 0px 24px" id="topic_output"><div align="center" style="margin:0px 24px 0px 24px" id="topic_output"> <?php echo $topic_output; ?></div><br /><br /><div align="center" style="margin:0px 24px 0px 24px" id="answer_output"> <?php echo $answer_output; ?></div><br /> <?php$sql3=mysql_query("SELECT view FROM forum_question WHERE id='$id'") or die(mysql_error()); $row = mysql_fetch_array($sql3); $view=$row['view']; ////if have no counter value set counter ==1 if($view=0){ $view=1; } $sql4=mysql_query("INSERT INTO forum_question(view) VALUE('$view') WHERE id='$id'") or die(mysql_error()); ///Count more values $addview=$view+1; $sql5=mysql_query("UPDATE forum_question SET view='$addview' WHERE id='$id'") or die(mysql_error()); mysql_close(); ?><br /><br /><br /><div align="left" style="margin:0px 0px 0px 24px" id="table"><form action="add_answer.php" method="POST"><table width="595" border="0" cellspacing="0" cellpadding="2"> <tr> <td width="203"><li style="color:#004200;">NAME:</li></td> <td width="386"><label for="a_name"></label> <input type="text" name="a_name" id="a_name" size="60"/></td> </tr> <tr> <td><li style="color:#004200;">EMAIL:</li></td> <td><input type="text" name="a_email" id="a_email" size="60"/></td> </tr> <tr> <td valign="bottom"><li style="color:#004200;">ANSWER:</li></td> <td><label for="a_answer"></label> <textarea name="a_answer" id="a_answer" cols="57" rows="10"></textarea></td> </tr> <tr> <td colspan="2"> <input type="submit" name="submit" id="submit" value="SUBMIT" /> <input type="reset" name="reset" id="submit2" value="RESET" /> </td> </tr></table><input name="id" type="hidden" value="<?php echo $id; ?>"/></form></div></div></body></html>

Edited by francis Aneke

Share this post


Link to post
Share on other sites

Great, so now why don't you put all this aside and go write a tiny little database test?

 

Also, what reasonable person would ever use a raw $_GET in their database query?

Share this post


Link to post
Share on other sites

Please don't post the same question multiple times. You already asked this question yesterday to which you were replied to. Please keep it one thread per question / issue.

 

so, which line is the issue on? You have a WHERE clause using this ID in multiple places in your script. Are you sure id is defined? How do you even know which one you're having a problem with? As Dave J suggested, break down your script to smaller sections and debug one piece at a time.

Share this post


Link to post
Share on other sites

am very sorry@ thescientist, its just that i have done all i could to solve the problem so am kind of going mad

Share this post


Link to post
Share on other sites

The first step is to figure out which of those queries is causing the error. Instead of just dieing with the error message, add the line number. The __LINE__ constant will give you the line number where it appears.... or die('Line ' . __LINE__ . ': ' . mysql_error());Once you know which query is causing the problem, then look at that query to figure out why. I see at least one SQL error which I assume is the problem.

Share this post


Link to post
Share on other sites

still not yet solved @justsomeguy, the problem is that i don't know how to split the script and run them in fragments to know which exact line has the problem

Share this post


Link to post
Share on other sites

you don't even have to split them. just use JSG's suggestion

Share this post


Link to post
Share on other sites

@thescientist, please don't be offended, how can i use JSG suggestion

Share this post


Link to post
Share on other sites

which part about it don't you understand? he gave a very clear example

 

The first step is to figure out which of those queries is causing the error. Instead of just dieing with the error message, add the line number. The __LINE__ constant will give you the line number where it appears.... or die('Line ' . __LINE__ . ': ' . mysql_error());Once you know which query is causing the problem, then look at that query to figure out why. I see at least one SQL error which I assume is the problem.

 

If you aren't familiar with the __LINE_ constant, here's the documentation on it

http://www.php.net/manual/en/language.constants.predefined.php

Share this post


Link to post
Share on other sites

You have this in your code:or die(mysql_error());The only thing that tells you is the error from MySQL. Add the line number to that like I showed and then you'll also know which query it was.

Share this post


Link to post
Share on other sites

@JUSTSOMEGUY,@THESCIENTIST

I have good news for you guys...

I HAVE SOLVED IT using the very __LINE__ "magical" PHP constant.

but am coming back to tell you how i did it

Share this post


Link to post
Share on other sites

/////////////////////////////////////// line:113 ///////////////////////////////////////////////

$sql4=mysql_query("INSERT INTO forum_question(view) VALUE('$view') WHERE id='$id'") or die(mysql_error());

//////////////////////////////////////////////////////////

////////////////////////////////////////////////////////

The above line of code is where my error stuck its ######ing head at......The view record in my database table has already contained a value of '0' but being a ######ing guy, am trying to INSERT INTO an already occupied ###### instead of "UPDATE forum_question SET view='$view' WHERE id=".$id.""----->

 

I did that and it worked perfectly

 

Thanks to JUSTSOMEGUY for suggesting the __LINE__ "magical" PHP constant.

 

Thanks to THESCIENTIST for directing me to PHP.net

 

Am grateful!

 

Hope to count on you next time

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...
Sign in to follow this  

×
×
  • Create New...