PHP MySQL data Delete, UPDATE, insert save data Code

Source: Internet
Author: User
Tags php mysql

I. Data deletion

The code is as follows Copy Code

<?php
Connecting to a database tutorial
$link _id = mysql_connect ("localhost", "root", "") or Die ("Connection failed");
if ($link _id)
{
mysql_select_db ("My_test");
if (!$_get[id])
{

$result =mysql_query ("SELECT * from UserInfo");
echo "<table border=1>
<TR>
<TD> number www.111cn.net</td>
<TD> User name </TD>
<TD> Sex </TD>
<TD> Age </TD>
<TD> Registration Time </TD>
<TD> Operations </TD>
</TR> ";

while ($row =mysql_fetch_array ($result)) {
echo "<TR>
<TD> ". $row [id]." </TD>
<TD> ". $row [username]." </TD>
<TD> ". $row [Gender]." </TD>
<TD> ". $row [age]." </TD>
<TD> ". $row [RegDate]." </TD>
<td><a href=delete.php?id= ". $row [id]." > Delete </a></TD>
</TR> ";
}
echo "</TABLE>";

}//Display the contents of a list
Else
{

$sql = "Delete from userinfo where id=". $_get[id];
$result =mysql_query ($sql);
if ($result)
echo "Record has been successfully deleted <br><a href= ' delete.php ' > Return </a>";
Else
echo "Record deletion failed <br><a href=delete.php.php?id=". $_get[id]. " > Return </a> ";

}//else ($id part)
}//End Ifwww.111cn.net
?>

Second, PHP data modification

The code is as follows Copy Code

<?php
Connecting to a database
$link _id = mysql_connect ("localhost", "root", "") or Die ("Connection failed");
if ($link _id)
{
mysql_select_db ("My_test");
if (!$_get[id])
{

$result =mysql_query ("SELECT * from UserInfo");
echo "<table border=1>
<TR>
<TD> number </TD>
<TD> User name </TD>
<TD> Sex </TD>
<TD> Age </TD>
<TD> Registration Time </TD>
<TD> Operations </TD>
</TR> ";

while ($row =mysql_fetch_array ($result)) {
echo "<TR>
<TD> ". $row [id]." </TD>
<TD> ". $row [username]." </TD>
<TD> ". $row [Gender]." </TD>
<TD> ". $row [age]." </TD>
<TD> ". $row [RegDate]." </TD>
<td><a href=modify.php?id= ". $row [id]." > Editor www.111cn.net</a></td>
</TR> ";
}
echo "</TABLE>";

}//Display the contents of a list
Else
{
if (!$_post[ok])
{
$sql = "SELECT * from UserInfo where id=". $_get[id];
$result =mysql_query ($sql);
$row =mysql_fetch_array ($result);
?>
<form method=post action= ' modify.php?id=<? Echo $_get[id];? > ' >
?
echo $row [id]. <br> ";
?>
<input type= "hidden" name= "id" Value=<?echo $row [id];? >>
Name <input type=text name= "username" Value=<?echo $row [username];? >><br>
Gender <input type=text name= "Gender" Value=<?echo $row [gender];? >><br>
Ages <input type=text name= "age" Value=<?echo $row [aged];? >><br>
Registration time <input type=text name= "regdate" Value=<?echo $row [' regdate '];? >><br>
<input type=submit Name=ok value= "Submit" >
</form>
?
}//if (!$_post[ok])
else{//processing for $ok after activation:

$sql = "Update userinfo set Username= '". $_post[username]. "', gender= '". $_post[gender]. "', age= '". $_post[age]. "', Regdate= '. $_post[regdate]. "' Where id= '". $_post[id].
$result =mysql_query ($sql);
if ($result)
echo "Record has been successfully modified <br><a href= ' modify.php ' > continue to modify record www.111cn.net</a>";
Else
echo "Record modification failed <br><a href=modify.php?id=". $_post[id]. " > Return </a> ";
}
}//else ($id part)
}//End If
?>

Third, PHP data preservation

The code is as follows Copy Code

<?php
if ($_post[ok])
{
$link _id = mysql_connect ("localhost", "root", "") or Die ("Connection failed");
if ($link _id)
{
Select Database
mysql_select_db ("My_test");
Insert Data SQL statement
$sql = "INSERT into userinfo values ('. $_post[id]." ', ' ". $_post[name]." ', ' ". $_post[gender]." ', '. $_post[age]. "', '". $ _post[regdate]. "')";
Execute SQL statement
$result =mysql_query ($sql);
if ($result)
{
echo "Record has been successfully inserted into <br><a href= ' insert.php ' > Continue insert record </a>";
}
Else
echo "Failed to execute INSERT SQL statement";
Close Database
Mysql_close ($link _id);
}
}
Else
{
?>
<form Method=post action=insert.php>
Number <input type=text name= "id" ><br>
Name <input type=text name= "name" ><br>
Gender <input type=text name= "gender" ><br>
Ages <input type=text name= "age" ><br>
Registration time <input type=text name= "RegDate" ><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type=submit Name=ok value= "Submit" >
</form>
?
}//end if
?>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.