Sunday, January 16, 2011

PHP MYSQL Tutorial 2: Insert, Update, Delete records in a Table.

This is second of PHP MYSQL basics tutorials. If you have not read the first one, please do it here :
http://codingbasics.blogspot.com/2011/01/php-mysql-tutorial-1-install-php-mysql.html
where each and every step is very detailed explained with code.

Insert rows into table


Insert.php


<html> 
<body> 
<? 
if ($UserName) 

mysql_connect (localhost, username, password) or die ("Problem connecting to DataBase"); 
$query = "insert into cust values ('$cid','$UserName','$LastName','$Details')"; 
$result = mysql_db_query("eshop", $query); 


echo "Data inserted. new table:<br><p></p>"; 
$query = "SELECT * FROM cust"; 
$result = mysql_db_query("eshop", $query); 


if ($result) { 
echo "<table width=90% align=center border=1><tr> 
<td align=center bgcolor=#00FFFF>idx</td> 
<td align=center bgcolor=#00FFFF>User Name</td> 
<td align=center bgcolor=#00FFFF>Last Name</td> 
<td align=center bgcolor=#00FFFF>Free Text</td> 
</tr>"; 


while ($r = mysql_fetch_array($result)) { 
$cid = $r["cid"]; 
$user = $r["UserName"]; 
$last = $r["LastName"]; 
$det = $r["Details"]; 


echo "<tr> 
<td>$idx</td> 
<td>$user</td> 
<td>$last</td> 
<td>$det</td> 
</tr>"; 


} // end of while loop 
echo "</table>"; 
} else { 
echo "No data."; 
} // end of if ($result) 
} else { 
echo "No UserName Entered. Please go back and reenter UserName"; 
} // end of if ($UserName) 
echo "<p></p>"; 
include ('sitelinks.txt'); 
?> 
</body> 
</html>

Read more for update and Delete records from the table.



Update Records in the table


<?php 
mysql_connect (localhost, username, password)or die ("Problem connecting to DataBase"); 

$query = "update cust set 
cid='$cid',UserName='$UserName',LastName='$LastName',Details='$Details' where 
cid='$cid'"; 

$result = mysql_db_query("eshop", $query); 
$query = "SELECT * FROM cust"; 
$result = mysql_db_query("eshop", $query); 

if ($result) { 
echo "Found these entries in the database:<br><p></p>"; 
echo "<table width=90% align=center border=1><tr> 
<td align=center bgcolor=#00FFFF>cid</td> 
<td align=center bgcolor=#00FFFF>User Name</td> 
<td align=center bgcolor=#00FFFF>Last Name</td> 
<td align=center bgcolor=#00FFFF>Details</td> 
</tr>"; 

while ($r = mysql_fetch_array($result)) { 

$cid = $r["cid"]; 
$user = $r["UserName"]; 
$last = $r["LastName"]; 
$det = $r["Details"]; 

echo "<tr> 
<td>$cid</td> 
<td>$user</td> 
<td>$last</td> 
<td>$det</td> 
</tr>"; 


echo "</table>"; 

} else { 

echo "No data."; 


mysql_free_result($result); 

include ('sitelinks.txt'); 

?> 

Delete rows from table.
This is like the same above, hence I am just sharing the query now.

$query = "delete from cust where cidx='$cid' and UserName='$UserName' 
and LastName='$LastName' and Details='$Details'"; 

Hope these two tutorials cover all your basics with PHP MYSQL database connections.

2 comments:

  1. hi,it's a good post and very easy to understand.
    thanks for sharing.

    ReplyDelete
  2. Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your further write ups thanks once again.

    ReplyDelete

subversion video