Click here to Skip to main content
15,896,348 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
its always show that my query is not working. "Database Error: Unable to update record. i already try to print my query and show this

"UPDATE students set first_name = 'Alex', last_name = 'Rawr', year = '4', course = 'IT', subjects = 'sdd', WHERE id ='1' " the id is given. any tips about this.

PHP
$action = isset( $_POST['action'] ) ? $_POST['action'] : "";

if($action == "update"){

    $query = "UPDATE students

    set 

     first_name = '".$mysqli->real_escape_string($_POST['first_name'])."',

     last_name = '".$mysqli->real_escape_string($_POST['last_name'])."',

     year = '".$mysqli->real_escape_string($_POST['year'])."',

     course = '".$mysqli->real_escape_string($_POST['course'])."',

     subjects = '".$mysqli->real_escape_string($_POST['subjects'])."',

     WHERE id ='".$mysqli->real_escape_string($_REQUEST['id'])."' ";

        if($mysqli->query($query) ) {

        //if updating the record was successful

        echo "User was updated.";

        }else{

        //if unable to update new record

        echo "Database Error: Unable to update record.";

        }

}


$row = $result->fetch_assoc();

//assign the result to certain variable so our html form will be filled up with values
    $id         = $row['id'];
    $first_name = $row['first_name'];
    $last_name  = $row['last_name'];
    $year       = $row['year'];
    $course     = $row['course'];
    $subjects   = $row['subjects'];


HTML
<!--we have our html form here where new user information will be entered-->

<form action='#' method='post' border='0'>

<table>

<tr>

<td>Firstname</td>

<td><input type='text' name='first_name' value='<?php echo $first_name;  ?>' /></td>

</tr>

<tr>

<td>Lastname</td>

<td><input type='text' name='last_name' value='<?php echo $last_name;  ?>' /></td>

</tr>

<tr>

<td>Year</td>

<td><input type='text' name='year'  value='<?php echo $year;  ?>' /></td>

</tr>

<tr>

<td>Course</td>

<td><input type='text' name='course'  value='<?php echo $course;  ?>' /></td>

<tr>

<tr>

<td>Subjects</td>

<td><input type='text' name='subjects'  value='<?php echo $subjects;  ?>' /></td>

<tr>

<td></td>

<td>

<!-- so that we could identify what record is to be updated -->

<input type='hidden' name='id' value='<?php echo $id ?>' />

<!-- we will set the action to update -->

<input type='hidden' name='action' value='update' />

<input type='submit' value='Edit' />

<a href='displayRecords.php'>Back to display page</a>

</td>

</tr>
Posted
Updated 22-Feb-15 2:03am
v6
Comments
Richard MacCutchan 22-Feb-15 7:42am    
Please format your code properly and add some detail about what you mean by "it's not updating".
Member 11111612 22-Feb-15 7:59am    
okay bro. i update it already

I have a comma before WHERE and after my last set value. solve :3
 
Share this answer
 
PHP
$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 students

		set

		first_name = '".$mysqli->real_escape_string($_POST['first_name'])."',

		last_name = '".$mysqli->real_escape_string($_POST['last_name'])."',

		year = '".$mysqli->real_escape_string($_POST['year'])."',

		course  = '".$mysqli->real_escape_string($_POST['course'])."',

		subjects  = '".$mysqli->real_escape_string($_POST['subjects'])."'

		where id='".$mysqli->real_escape_string($_REQUEST['id'])."'";

//execute the query

		if( $mysqli->query($query) ) {

		//if updating the record was successful

		echo "User was updated.";

		}else{

		//if unable to update new record

		echo "Database Error: Unable to update record.";

		}

}


it work :3 but i cant see the difference of the codes i mean what happened, im just restart my laptop and then update.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900