Main pages (Main page)
<table width= "100%" border= "1" cellpadding= "0" cellspacing= "0" >
<tr>
<td> Code </td>
<td> name </td>
<td> Sex </td>
<td> ethnic </td>
<td> Birthdays </td>
<td> Operations </td>
</tr>
<?php
$db = new Mysqli ("localhost", "root", "" "," mydb ");
if (Mysqli_connect_error ()) {
Die ("Connection failed");
}
$sql = "SELECT * from Info";
$result = $db->query ($sql);
$attr = $result->fetch_all ();
foreach ($attr as $v)
{
$sex = $v [2]? ' Male ': ' Female ';//ternary operator to judge gender
$sql = "Select Name from Nation where Code = ' $v [3] '";
$result = $db->query ($sql);
$attr = $result->fetch_assoc ();
echo "<tr>
<td>{$v [0]}</td>
<td>{$v [1]}</td>
<td>{$sex}</td>
<td>{$attr [' Name ']}</td>
<td>{$v [4]}</td>
<td>
<a href= ' delete.php?code={$v [0]} ' > Delete </a>
<a href= ' update.php?code={$v [0]} ' > Modify </a>
</td>
</tr> ";
}
?>
</table>
<div>
<a href= "add.php" > Add Data </a>
</div>
Add (adding data page)
<form action= "addchuli.php" method= "POST" >
<div> code: <input type= "text" name= "code"/></div>
<div> Name: <input type= "text" name= "name"/></div>
<div> Gender:
<input type= "Radio" value= "male" name= "sex"/> Male
<input type= "Radio" value= "female" name= "sex"/> Female
</div>
<div> Nationalities:
<select name= "Nation" >
<?php
$db = new Mysqli ("localhost", "root", "" "," mydb ");
if (Mysqli_connect_error ()) {
Die ("Connection failed");
}
$sql = " SELECT * from Nation ";
$r = $db->query ($sql);
$att = $r->fetch_all ();
foreach ($att as $v)
{
echo ' <option value= ' {$v [0]} ' >{$v [1]}</option> ';
}
;
</select>
</div>
<div> Birthday: <input type= "Text" name= "Birthday"/></DIV>
<div><input type= "Submit" value= "Add Data"/></div
</form>
Addchuli page (Add Data processing page)
<?php
$code = $_post["code"];
$name = $_post["name"];
$sex = $_post["Sex"];
$s = 1;
if ($sex = = "female")
{
$s = 0;
}
$nation = $_post["Nation"];
$birthday = $_post["Birthday"];
$db = new Mysqli ("localhost", "root", "" "," mydb ");
if (Mysqli_connect_error ()) {
Die ("Connection failed");
}
$sql = "INSERT into Info values (' {$code} ', ' {$name} ', ' {$s} ', ' {$nation} ', ' {$birthday} ')"; //Add Data statement
$result = $db->query ($sql);
if ($result)
{
Header ("location:main.php"); //php jump page mode
}
Else
{
echo "Add failed! ";
}
Deletechuli page (delete data processing page)
<?php
$code = $_get["code"];
$db = new Mysqli ("localhost", "root", "" "," mydb ");
if (Mysqli_connect_error ()) {
Die ("Connection failed");
}
$sql = "Delete from Info where Code = ' {$code} '"; //DELETE statement
$r = $db->query ($sql);
if ($r)
{
Header ("location:main.php");
}
Else
{
echo "Delete failed! ";
}
Update page (modify data page)
<?php
$code = $_get["code"];
$db = new Mysqli ("localhost", "root", "" "," mydb ");
if (Mysqli_connect_error ()) {
Die ("Connection failed");
}
$sql 1 = "SELECT * from Info where code= ' {$code} '";
$r 1 = $db->query ($sql 1);
$att 1 = $r 1->fetch_row ();
?>
<form action= "updatechuli.php" method= "POST" >
<div> code: <input type= "hidden" name= "code" value= "<?php Echo $att 1[0]?>"/></div>
<div> Name: <input type= "text" name= "name" value= "<?php Echo $att 1[1]?>"/></div>
<div> Gender:
<input type= "Radio" value= "male" name= "Sex" <?php echo $att 1[2]? "Checked= ' checked '": "";?>/> Male
<input type= "Radio" value= "female" name= "Sex" <?php echo $att 1[2]? "": "checked= ' checked '";?>/> Female
</div>
<div> Nationalities:
<select name= "Nation" >
<?php
$db = new Mysqli ("localhost", "root", "" "," mydb ");
if (Mysqli_connect_error ()) {
Die ("Connection failed");
}
$sql = "SELECT * from Nation";//Query Nation a set of data
$r = $db->query ($sql);
$att = $r->fetch_all ();
foreach ($att as $v)
{
if ($att 1[3]== $v [0])
{
echo "<option value= ' {$v [0]} ' selected= ' Selectec ' >{$v [1]}</option>";
}
Else
{
echo "<option value= ' {$v [0]} ' >{$v [1]}</option>";
}
}
?>
</select>
</div>
<div> Birthdays: <input type= "text" name= "Birthday" value= "<?php Echo $att 1[4]?>"/></div>
<div><input type= "Submit" value= "Modify Data"/></div>
</form>
Updatechuli page (Modify data processing page)
<?php
$code = $_post["code"];
$name = $_post["name"];
$sex = $_post["Sex"];
$s = 1;
if ($sex = = "female")
{
$s = 0;
}
$nation = $_post["Nation"];
$birthday = $_post["Birthday"];
$db = new Mysqli ("localhost", "root", "" "," mydb ");
if (Mysqli_connect_error ()) {
Die ("Connection failed");
}
$sql = "Update Info set name= ' {$name} ', sex={$s},nation= ' {$nation} ', birthday= ' {$birthday} ' where code= ' {$code} '"; //Modify Data statement
$r = $db->query ($sql);
if ($r)
{
Header ("location:main.php");
}
Else
{
echo "Modification failed! ";
}
Using PHP to connect database--the whole operation example of adding and deleting user data