Update query in mysql in php


Update Data In a MySQL Table Using MySQLi and PDO

The UPDATE statement is used to update existing records in a table:

UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value 

Notice the WHERE clause in the UPDATE syntax: The WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause, all records will be updated!

To learn more about SQL, please visit our SQL tutorial.

Let's look at the "MyGuests" table:

idfirstnamelastnameemailreg_date
1 John Doe 2014-10-22 14:26:15
2 Mary Moe 2014-10-23 10:22:30

The following examples update the record with id=2 in the "MyGuests" table:

Example (MySQLi Object-oriented)

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";

if ($conn->query($sql) === TRUE) {
  echo "Record updated successfully";
} else {
  echo "Error updating record: " . $conn->error;
}

$conn->close();
?>




Example (MySQLi Procedural)

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}

$sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";

if (mysqli_query($conn, $sql)) {
  echo "Record updated successfully";
} else {
  echo "Error updating record: " . mysqli_error($conn);
}

mysqli_close($conn);
?>


Example (PDO)

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";

try {
  $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  // set the PDO error mode to exception
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

  $sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";

  // Prepare statement
  $stmt = $conn->prepare($sql);

  // execute the query
  $stmt->execute();

  // echo a message to say the UPDATE succeeded
  echo $stmt->rowCount() . " records UPDATED successfully";
} catch(PDOException $e) {
  echo $sql . "
" . $e->getMessage();
}

$conn = null;
?>


After the record is updated, the table will look like this:

idfirstnamelastnameemailreg_date
1 John Doe 2014-10-22 14:26:15
2 Mary Doe 2014-10-23 10:22:30



Can anybody help me understand why this update query isn't updating the fields in my database? I have this in my php page to retrieve the current values from the database:


Here i my HTML Form:

ID:
Username:
Title:
Date:
Message:

and here is my 'editblogscript':


I don't understand why it doesn't work.

Update query in mysql in php

Dharman

27.7k21 gold badges75 silver badges126 bronze badges

asked Jan 17, 2012 at 11:06

0

You have to have single quotes around any VARCHAR content in your queries. So your update query should be:

mysql_query("UPDATE blogEntry SET content = '$udcontent', title = '$udtitle' WHERE id = $id");

Also, it is bad form to update your database directly with the content from a POST. You should sanitize your incoming data with the mysql_real_escape_string function.

answered Jan 17, 2012 at 11:11

Update query in mysql in php

davidethelldavidethell

11.5k5 gold badges41 silver badges60 bronze badges

4

Need to add quote for that need to use dot operator:

mysql_query("UPDATE blogEntry SET content = '".$udcontent."', title = '".$udtitle."' WHERE id = '".$id."'");

Ani Menon

25.8k16 gold badges93 silver badges121 bronze badges

answered Oct 9, 2012 at 6:44

Update query in mysql in php

Without knowing what the actual error you are getting is I would guess it is missing quotes. try the following:

mysql_query("UPDATE blogEntry SET content = '$udcontent', title = '$udtitle' WHERE id = '$id'")

answered Jan 17, 2012 at 11:12

MrKianeMrKiane

4,5332 gold badges16 silver badges27 bronze badges

0

Here i updated two variables and present date and time

$id = "1";
$title = "phpmyadmin";

 $sql=  mysql_query("UPDATE table_name SET id ='".$id."', title = '".$title."',now() WHERE id = '".$id."' ");

now() function update current date and time.

note: For update query we have define the particular id otherwise it update whole table defaulty

answered Feb 15, 2014 at 11:34

Update query in mysql in php

sudhakarsudhakar

3021 gold badge3 silver badges9 bronze badges

First, you should define "doesn't work".
Second, I assume that your table field 'content' is varchar/text, so you need to enclose it in quotes. content = '{$content}'
And last but not least: use echo mysql_error() directly after a query to debug.

answered Jan 17, 2012 at 11:12

pdupdu

10.2k4 gold badges57 silver badges92 bronze badges

3

Try like this in sql query, It will work fine.

$sql="UPDATE create_test set url= '$_POST[url]' WHERE test_name='$test_name';";

If you have to update multiple columns, Use like this,

$sql="UPDATE create_test set `url`= '$_POST[url]',`platform`='$_POST[platform]' WHERE test_name='$test_name';";

answered Feb 22, 2018 at 12:17

VidyaVidya

632 silver badges10 bronze badges

you must write single quotes then double quotes then dot before name of field and after like that

mysql_query("UPDATE blogEntry SET content ='".$udcontent."', title = '".$udtitle."' WHERE id = '".$id."' ");

CloudyMarble

36.3k70 gold badges94 silver badges128 bronze badges

answered Jan 7, 2014 at 10:04

How do I update a MySQL query?

MySQL UPDATE.
First, specify the name of the table that you want to update data after the UPDATE keyword..
Second, specify which column you want to update and the new value in the SET clause. ... .
Third, specify which rows to be updated using a condition in the WHERE clause..

How do I update MySQL version in PHP?

HOW TO UPGRADE PHP VERSION, PHP SETTINGS OR MYSQL VERSION.
a. Type “php” in the find functions dialogue box..
b. Click on Select PHP version..
a. You may for example, want the “mysqli” extension enabled in which case you can tick that box to enable that version of the mysql database client interface..

What is PHP update statement?

The UPDATE statement is used to update existing records in a table: UPDATE table_name. SET column1=value, column2=value2,...

How do I update phpmyadmin data?

Use the navigation tree in the left sidebar to locate the database table you wish to modify..
The database fields are displayed in the right pane. ... .
You can now proceed to modify the data within the field..
If you wish to modify table details, select the Operations tab at the top of the screen..