Update Query
How Modify data into DataBase.

For Modify Data use the update query:

Syntax:-
UPDATE table_name
SET column1=value, column2=value2, column3=value3…
WHERE anycolumn=value

anycolumn means you write which column you want to modify and write its value in update query
Note:- where clause is necessary otherwise all records will be update.

Example 1:- Simple query for Update Data.

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
die('Mysql not connect ');
$database=mysql_select_db("test", $con);
if (!$database)
die('DataBase not connect ');
mysql_query("update info set username= 'mitesh pal',city=’Bareilly’
where userid=’1001’;
mysql_close($con);
?>

Example 2:- Script for Modify data through form.

<html>
<head>
<title>Update Record</title>
</head>
<body>
<form name="frm" method="post" action="">
<table width="315" height="78" border="1" align="center">
<tr>
<td height="23" colspan="2" align="center">Script for Update User </td>
</tr>
<tr>
<td width="128">Enter User I D </td>
<td width="171"><input name="userid" type="text" id="userid" /></td>
</tr>
<tr>
<td width="128">Enter User Name </td>
<td width="171"><input name="username" type="text" id="username" /></td>
</tr>
<tr>
<td width="128">Enter User Address </td>
<td width="171"><input name="useraddress" type="text" id="useraddress" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="Submit" value="Submit" /></td>
</tr>
<tr>
<td colspan="2">
<?php
if(isset($_POST['Submit']))
{
$userid=$_POST['userid'];
$username=$_POST['username'];
$useraddress=$_POST['useraddress'];
$conn=mysql_connect('localhost','root','');
if(!$conn)
die('Mysql Not Connected');
$database=mysql_select_db('test',$conn);
if(!$database)
die('DataBase Not Connected');
$query="update info set username='$username',useraddress='$useraddress' where userid='$userid'";
$data=mysql_query($query);
echo $data;
if(!$data)
die('Error in Query');
else
echo "Record Successfully Update";
}
?>
</td>
</tr>
</table>
</form>
</body>
</html>


Previous
Next