edgustaf Posted January 6, 2011 Share Posted January 6, 2011 When I am trying to update a record in the database i get this error after I submit the form that is populated from the database. 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 'desc = 'SFR ANNOUNCES 3RD ANNUAL \"12 DAYS OF CHRISTMAS FOOD DRIVE\"', body = '<' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/223618-help-errors/ Share on other sites More sharing options...
edgustaf Posted January 6, 2011 Author Share Posted January 6, 2011 This is my code so far <?php include("includes/connect.php"); $updateid = $_GET["id"]; include("includes/connect.php"); $editquery = mysql_query("SELECT * FROM news WHERE id = '$updateid'"); $row = mysql_fetch_assoc($editquery); $currentid = $row["id"]; $currenttitle = $row["title"]; $currentdesc = $row["desc"]; $currentbody = $row["body"]; $currenttype = $row["type"]; $currentdate = $row["date"]; ?> <form style="margin:20px 0 0 0; padding:0;" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <label style="color:#000;">Title</label> <input type="text" name="title" size="40" value="<?php echo "$currenttitle"; ?>" /> <label style="color:#000;">News Ticker Description</label> <textarea cols="80" rows="10" name="desc"><?php echo "$currentdesc"; ?></textarea> <label style="color:#000;">Body</label> <?php // Make sure you're using correct paths here include_once 'ckeditor/ckeditor.php'; include_once 'ckfinder/ckfinder.php'; $ckeditor = new CKEditor(); $ckeditor->basePath = '/ckeditor/'; CKFinder::SetupCKEditor($ckeditor, '/ckfinder/'); $ckeditor->editor('body',$currentbody); ?> <input style="margin:20px 0 0 0;" type="submit" name="submit" value="Submit" /> </form> <?php $submit = $_POST["submit"]; $updatetitle = $_POST['title']; $updatebody = $_POST['body']; $updatetype = $_POST['type']; $updatedate = $_POST['date']; $updatedesc = $_POST['desc']; if ($submit) { $updatearticle = mysql_query("UPDATE news SET title = '$updatetitle', desc = '$updatedesc', body = '$updatebody' WHERE id = $updateid")or die(mysql_error()); echo "<h1>Article has been updated</h1>"; } ?> </div> </div> </div> <?php include("includes/footer.php"); ?> </div> </body> </html> MOD Edit: . . . tags added . . . Quote Link to comment https://forums.phpfreaks.com/topic/223618-help-errors/#findComment-1155893 Share on other sites More sharing options...
BlueSkyIS Posted January 6, 2011 Share Posted January 6, 2011 echo out the SQL so you can see what the problem is $update_sql = "UPDATE news SET title = '$updatetitle', desc = '$updatedesc', body = '$updatebody' WHERE id = $updateid"; $updatearticle = mysql_query($update_sql)or die(mysql_error() . " IN $update_sql "); Quote Link to comment https://forums.phpfreaks.com/topic/223618-help-errors/#findComment-1155897 Share on other sites More sharing options...
Pikachu2000 Posted January 6, 2011 Share Posted January 6, 2011 When posting code, please make use of the forum's . . . tags. I've added them to your post this time . . . Quote Link to comment https://forums.phpfreaks.com/topic/223618-help-errors/#findComment-1155926 Share on other sites More sharing options...
edgustaf Posted January 6, 2011 Author Share Posted January 6, 2011 Thank you for the replies. For some reason when I echo out the error it shows the the $updateid variable is not getting passed in the update statement but when I echo the $updateid by itsself it comes out fine. Quote Link to comment https://forums.phpfreaks.com/topic/223618-help-errors/#findComment-1155929 Share on other sites More sharing options...
Pikachu2000 Posted January 6, 2011 Share Posted January 6, 2011 For now, change the UPDATE query execution to this. There's more work that needs to be done here, such as sanitizing form inputs, but let's start with this. if ($submit) { $query ="UPDATE news SET title = '$updatetitle', desc = '$updatedesc', body = '$updatebody' WHERE id = $updateid"; $result = mysql_query($query) or die( "<br>Query string: $query<br>Produced Error: " . mysql_error() ); echo "<h1>Article has been updated</h1>"; } Quote Link to comment https://forums.phpfreaks.com/topic/223618-help-errors/#findComment-1155930 Share on other sites More sharing options...
PFMaBiSmAd Posted January 6, 2011 Share Posted January 6, 2011 the right syntax to use near 'desc ... ^^^ The error message is calling your attention to the point in the query where there is a problem. desc is a reserved mysql keyword and you should either rename your column to something else or you must enclose desc in back-ticks `` every time you use it in a query. Quote Link to comment https://forums.phpfreaks.com/topic/223618-help-errors/#findComment-1155931 Share on other sites More sharing options...
edgustaf Posted January 6, 2011 Author Share Posted January 6, 2011 Would it matter if I was trying to insert html tags through the sql statement? Quote Link to comment https://forums.phpfreaks.com/topic/223618-help-errors/#findComment-1155933 Share on other sites More sharing options...
edgustaf Posted January 6, 2011 Author Share Posted January 6, 2011 ahhhhhhh!! makes sense Quote Link to comment https://forums.phpfreaks.com/topic/223618-help-errors/#findComment-1155934 Share on other sites More sharing options...
edgustaf Posted January 7, 2011 Author Share Posted January 7, 2011 Thanks so much. I am not receiving the error anymore but my database doesn't update for some reason. Quote Link to comment https://forums.phpfreaks.com/topic/223618-help-errors/#findComment-1155940 Share on other sites More sharing options...
Pikachu2000 Posted January 7, 2011 Share Posted January 7, 2011 You need to see what is actually happening there when the query executes (or not), so as a debugging tool, see * if the query successfully executes, and if so how many records were updated * if not, what the error message is, along with the query string. if ($submit) { $query ="UPDATE news SET title = '$updatetitle', `desc` = '$updatedesc', body = '$updatebody' WHERE id = $updateid"; if( $result = mysql_query($query) ) { echo '<br>Update query ran and affected ' . mysql_affected_rows() . ' rows<br>'; } else { echo "<br>Query string: $query<br>Produced Error: " . mysql_error(); } } Quote Link to comment https://forums.phpfreaks.com/topic/223618-help-errors/#findComment-1155943 Share on other sites More sharing options...
edgustaf Posted January 7, 2011 Author Share Posted January 7, 2011 Your a genius! Thank you soo much!! Quote Link to comment https://forums.phpfreaks.com/topic/223618-help-errors/#findComment-1155944 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.