kiwisi Posted June 18, 2013 Share Posted June 18, 2013 This is doing my head in. I have another page on a different table (same database) where this works fine. Yet now when I try to update the record, the eventbody (text area) just echos the data with formatting and it says "unable to update record" - which is defined error. All I did was change the field names on this page... Hope you can help <?php error_reporting(E_ALL); ini_set("display_errors", 1); //include database connection include 'db_connect.php'; //check any user action $action = isset( $_POST['action'] ) ? $_POST['action'] : ""; if($action == "update"){ //if the user hit the submit button //write our update query //$mysqli->real_escape_string() function helps us prevent attacks such as SQL injection $query = "update eventguide set eventtitle = '".$mysqli->real_escape_string($_POST['eventtitle'])."', eventbody = '".$mysqli->real_escape_string($_POST['eventbody'])."', eventdate = '".$mysqli->real_escape_string($_POST['eventdate'])."', where eventid = S'".$mysqli->real_escape_string($_REQUEST['eventid'])."'"; //execute the query if( $mysqli->query($query) ) { //if updating the record was successful echo "<h1><strong>User was updated.</strong></h1>"; }else{ //if unable to update new record echo "Database Error: Unable to update record."; } } //select the specific database record to update $query = "select eventid, eventtitle, eventbody, eventdate from eventguide where eventid = '".$mysqli->real_escape_string($_REQUEST['eventid'])."' limit 0,1"; //execute the query $result = $mysqli->query( $query ); //get the result $row = $result->fetch_assoc(); //assign the result to certain variable so our html form will be filled up with values $eventid = $row['eventid']; $eventtitle = $row['eventtitle']; $eventbody = $row['eventbody']; $eventdate = $row['eventdate']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/279302-database-wont-update/ Share on other sites More sharing options...
doddsey_65 Posted June 18, 2013 Share Posted June 18, 2013 (edited) You have a trailing comma at this line which would cause an error in your sql syntax eventdate = '".$mysqli->real_escape_string($_POST['eventdate'])."', You also have a syntax error here where eventid = S'".$mysqli->real_escape_string($_REQUEST['eventid'])."'"; "S" should be within the quotes Edited June 18, 2013 by doddsey_65 Quote Link to comment https://forums.phpfreaks.com/topic/279302-database-wont-update/#findComment-1436564 Share on other sites More sharing options...
DaveyK Posted June 18, 2013 Share Posted June 18, 2013 In the future, if you want to know what the error is, you should be able to do something like: }else{ //if unable to update new record echo "Database Error: Unable to update record.<br>"; echo $mysqli->error; } Quote Link to comment https://forums.phpfreaks.com/topic/279302-database-wont-update/#findComment-1436566 Share on other sites More sharing options...
Solution kiwisi Posted June 18, 2013 Author Solution Share Posted June 18, 2013 (edited) God what a nump.... Doddsey -the S wasn't meant to be there! And the comma was the killer - presumably because it precedes the 'where'? DaveyK - cheers. knew the error reporting had to be in the somewhere, just wasn't sure where to put it. Thanks for your help guys. Edited June 18, 2013 by kiwisi Quote Link to comment https://forums.phpfreaks.com/topic/279302-database-wont-update/#findComment-1436567 Share on other sites More sharing options...
DaveyK Posted June 18, 2013 Share Posted June 18, 2013 Doddsey -the S wasn't meant to be there! And the comma was the killer - presumably because it precedes the 'where'? Thats correct. The error wouldve reported that Quote Link to comment https://forums.phpfreaks.com/topic/279302-database-wont-update/#findComment-1436569 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.