May 3 Increase and deletion check

Source: Internet
Author: User

Access to the database to implement additions and deletions and changes in the page display slightly dynamic application:

Add Data:

Main side: main.php


<?php
$db = new Mysqli ("localhost", "root", "" "," mydb ");
!mysqli_connect_error () or Die ("Connection failed");
$sql = "SELECT * from Info";
$result = $db->query ($sql);
$attr = $result->fetch_all ();
if ($result)
{
foreach ($attr as $v)
{
Dealing with gender
$sex = $v [2]? ' Male ': ' Female ';
Dealing with ethnic
$sql 1 = "SELECT name from Nation where Code = ' {$v [3]} '";
$rnation = $db->query ($sql 1);
$attr 1 = $rnation->fetch_assoc ();
echo "<tr>
<td>{$v [0]}</td>
<td>{$v [1]}</td>
<td>{$sex}</td>
<td>{$attr 1[' name ']}</td>
<td>{$v [4]}</td>
<td>
<a href= ' > Delete </a>
<a href= ' > Modify </a>
</td>
</tr> ";
}
}
?>
</table>
<div><a href= "add.php" > Add Data </a></div>

Add Page: add.php

<form action= "addchuli.php" method= "POST" >
<div> code: <input type= "text" name= "code"/></div>

<div> Name: <input type= "text" name= "name"/></div>

<div> Sex: <input type= "Radio" name= "Sex"/> Male
<input type= "Radio" name= "Sex"/> Female
</div>

<div> people: <select name= "Nation" >
<?php
$db =new mysqli ("localhost", "root", "" "," mydb ");
!mysqli_connect_error () or Die ("Connection failed");
$sql = "SELECT * From Nation";
$result = $db->query ($sql);
if ($result)
{
$attr = $result->fetch_all ();
foreach ($attr as $v)
{
echo "<option value= ' {$v [0]} ' >{$v [1]}</option>";
}
}
?>
</select>
</div>
<div> Birthdays: <input type= "text" name= "Birthday"/></div>
<div><input type= "Submit" value= "Add Data"/></div>
</form>
<div><a href= "main.php" > Homepage </a></div>

Add Processing page: addchuli.php

<?php

$code = $_post["code"];
$name = $_post["name"];
$sex = $_post["Sex"];
Dealing with gender
$s = 1;
if ($sex = = "female")
{
$s = 0;
}
$nation = $_post["Nation"];
$birthday = $_post["Birthday"];

$db = new Mysqli ("localhost", "root", "" "," mydb ");
$sql = "INSERT into Info values (' {$code} ', ' {$name} ', ' {$s} ', ' {$nation} ', ' {$birthday} ')";
$result = $db->query ($sql);
if ($result)
{
Header ("location:add.php");//Jump to add.php
}
Else
{
echo "Add failed";
}
The above is the process of adding, divided into three pages need to operate a jump

Delete data:

Main side: main.php

<table width= "100%" cellpadding= "0" cellspacing= "0" border= "1" >
<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 ");
!mysqli_connect_error () or Die ("Connection failed");
$sql = "SELECT * from Info";
$result = $db->query ($sql);
$attr = $result->fetch_all ();
if ($result)
{
foreach ($attr as $v)
{
Dealing with gender
$sex = $v [2]? ' Male ': ' Female ';
Dealing with ethnic
$sql 1 = "SELECT name from Nation where Code = ' {$v [3]} '";
$rnation = $db->query ($sql 1);
$attr 1 = $rnation->fetch_assoc ();
echo "<tr>
<td>{$v [0]}</td>
<td>{$v [1]}</td>
<td>{$sex}</td>
<td>{$attr 1[' name ']}</td>
<td>{$v [4]}</td>
<td>

<a href= ' delete.php?code={$v [0]} ' > Remove special attention from </a>//
<a href= ' > Modify </a>
</td>
</tr> ";
}
}
?>
</table>
<div><a href= "add.php" > Add Data </a></div>

Delete Processing page: delete.php

<?php

$code = $_get["code"];

$db = new Mysqli ("localhost", "root", "" "," mydb ");
$sql = "Delete from Info where Code = ' {$code} '";
$result = $db->query ($sql);
if ($result)
{
Header ("location:Main.php");
}
Else
{
echo "Delete Failed";
}

To modify the data:

Main side: main.php

<table width= "100%" cellpadding= "0" cellspacing= "0" border= "1" >
<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 ");
!mysqli_connect_error () or Die ("Connection failed");
$sql = "SELECT * from Info";
$result = $db->query ($sql);
$attr = $result->fetch_all ();
if ($result)
{
foreach ($attr as $v)
{
Dealing with gender
$sex = $v [2]? ' Male ': ' Female ';
Dealing with ethnic
$sql 1 = "SELECT name from Nation where Code = ' {$v [3]} '";
$rnation = $db->query ($sql 1);
$attr 1 = $rnation->fetch_assoc ();
echo "<tr>
<td>{$v [0]}</td>
<td>{$v [1]}</td>
<td>{$sex}</td>
<td>{$attr 1[' 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>

Modification page: update.php

<?php
$code = $_get["code"];
$db = new Mysqli ("localhost", "root", "" "," mydb ");
$ssql = "SELECT * from Info where Code = ' {$code} '";
$result = $db->query ($ssql);
$arr = $result->fetch_row ();//The person's information
,
<form action= "updatechuli.php" method= "post";
<div> code: <input type= "hidden" name= "code" value= "<?php echo $arr [0]?>"/></DIV>
<div > Name: <input type= "text" name= "name" value= "<?php echo $arr [1]?>"/></DIV>

<div> Gender: <input type= "Radio" name= "Sex" value= "male" <?php echo $arr [2]? Checked= ' checked ': "?>/> male
<input type=" Radio "name=" Sex "value=" female "<?php echo $arr [2]?": "Checked=" Checked ' "?>/> female
</div>

<div> people: <select name= "Nation" >
<?php

$sql = "SELECT * from Nation";
$result = $db->query ($sql);
$attr = $result->fetch_all ();
foreach ($attr as $v)
{
if ($v [0]== $arr [3])
{
echo "<option selected= ' selected ' value= ' {$v [0]} ' >{$v [1]}</option> ';
}
Else
{
echo "<option value= ' {$v [0]} ' >{$v [1]}</option>";
}
}

?>
</select>
</div>
<div> Birthdays: <input type= "text" name= "Birthday" value= "<?php echo $arr [4]?>"/></div>

<div><input type= "Submit" value= "Modify Data"/></div>
</form>
<div><a href= "main.php" > Homepage </a></div>


Modify Processing page: updatechuli.php

<?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 ");
$sql = "Update Info set name= ' {$name} ', sex={$s},nation= ' {$nation} ', birthday= ' {$birthday} ' where code= ' {$code} '";

$result = $db->query ($sql);
if ($result)
{
Header ("location:Main.php");
}
Else
{
echo "failed to modify";
}

May 3 Increase and deletion check

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.