Data Access ... Change and delete (i)

Source: Internet
Author: User

Take the Info table for example:

First, set up a homepage (main.php)

<! DOCTYPE html Public"-//w3c//dtd XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >PHP$db=NewMysqli ("localhost", "root", "123", "MyDB");!Mysqli_connect_error() or die("Connection failed!"));$sql= "SELECT * FROM Info";$r=$db->query ($sql);if($r){    $attr=$r-Fetch_all (); foreach($attr  as $v)    {        //dealing with gender        $sex=$v[2]? " Male ":" Female "; //dealing with ethnic names        $sqln= "Select Name from Nation where code= ' {$v[3]} ' "; $rnation=$db->query ($sqln); $attrn=$rnation-Fetch_assoc (); Echo"<tr> <td>{$v[0]}</td> <td>{$v[1]}</td> <td>{$sex}</td> <td>{$attrn[' 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>";//hyperlink jumps to the corresponding page and usesget pass value to pass code value pass through    }}? ></table><div><a href= "add.php" > Add Data </a></div></body>

Second, add data

1. Create Add Data page (add.php)

<title> Add Data </title>30px;}</style><body>class= "form" > Code: <input type= "text" name= "code"/></div> <divclass= "form" > Name: <input type= "text" name= "name"/></div> <divclass= "form" > Gender: <!--Sex for radio button--<input type= "Radio" value= "male" name= "sex"/>Male <!--name is set to the same, who is selected to submit who- -<input type= "Radio" value= "female" name= "sex"/>female</div> <divclass= "Form" > Nationalities: <!--nationalities as drop-down list--<select name= "Nation" > <?php//Drop -down list each item is isolated from the database, so you need to embed PHP code to access the database            $db=NewMysqli ("localhost", "root", "xiao8888", "MyDB"); $sql= "SELECT * FROM Nation"; $r=$db->query ($sql); $attr=$r-Fetch_all (); foreach($attr  as $v)            {                Echo"<option value= ' {$v[0]} ' >{$v[1]} </option> "; }            ?> </select> </div> <divclass= "form" > Birthday: <input type= "text" name= "Birthday"/></div><br/> <divclass= "form" > <input type= "Submit" value= "Add Data"/></div></form><divclass= "form" ><a href= "main.php" > main Page </a></div></body>

2. Create a Process Add Data page (addchuli.php)

<?PHP$code=$_post["Code"];//use a variable to receive the submitted value .$name=$_post["Name"];$sex=$_post["Sex"];$s= 1;//judge sex, Sex is Boolean, 1 means true,0 represents falseif($sex= = "female"){    $s= 0; }$nation=$_post["Nation"];$birthday=$_post["Birthday"];//to add the received data to the database$db=NewMysqli ("localhost", "root", "xiao8888", "MyDB");!Mysqli_connect_error() or die("Connection failed!"));$sql= "INSERT into Info values (' {$code}‘,‘{$name}‘,{$s},‘{$nation}‘,‘{$birthday}‘)";//The gender cannot be quoted, and the quotation marks are strings, which are treated as true$result=$db->query ($sql);//returns TRUE or falseif($result)//true{    Header("location:Add.php");//If added successfully jump to add.php, jump with header ("Location: Jump to the page");}Else          //false{    Echo"Add failed!"; }

Third, delete data

Create a Delete data page (delete.php)

Click Delete on the main page to connect to the delete data page by hyperlink and use the Get method to pass the primary key value code over, then determine the data to be deleted according to the primary key value

<?PHP$code=$_get["Code"];//using variables to receive primary key values$db=NewMysqli ("localhost", "root", "xiao8888", "MyDB");!Mysqli_connect_error() or die("Connection failed!"));$sql= "Delete from Info where code= ' {$code}‘";//delete the corresponding data by primary key code$r=$db->query ($sql);if($r){    Header("location:main.php"); }Else{    Echo"Delete failed!"; }

Iv. Modification of data

1. Create a Change data page (update.php)

On the main page, click Modify to connect to the Modify data page by hyperlink and get the primary key value code pass through, and then according to the primary key value to determine the data that needs to be modified, modify the data page and add data page style layout, and will need to modify the data information displayed on the page

<title> Modify Data </title>30px;}</style><body>PHP$code=$_get["Code"];//get a code value from Get mode$db=NewMysqli ("localhost", "root", "xiao8888", "mydb");//querying the code value for all information through a database!Mysqli_connect_error() or die("Connection failed!"));$sinfo= "SELECT * from Info where code= ' {$code}‘";//only one message can be queried based on the primary key value, and the value can be Fetch_row ()$r=$db->query ($sinfo);$arr=$r->fetch_row ();//returns an array of one-dimensional indexes?><!--A page can be embedded in multiple PHP code, regardless of HTML code, just look at the PHP code, the equivalent of connecting, so the variables defined above can be found below the--><form action= "updatechuli.php" method= "POST" > <div><input type= "hidden" name= "code" value= "<?php Echo$arr[0]?> "/></div> <!--primary key value cannot be modified, but must have to be submitted, so the settings are hidden, can also be set to read-only <divclass= "form" > Name: <input type= "text" name= "name" value= "<?php Echo$arr[1]?> "/></div> <!--display the data information to be modified by setting the value values--<divclass= "form" > Gender: <input type= "Radio" value= "male" name= "sex" <?phpEcho $arr[2]? " Checked= ' checked ' ":" "?>/> Male <!--if$arr[2] Tru is selected by default male-to-<input type= "Radio" value= "female" name= "sex" <?phpEcho $arr[2]? "": "checked= ' checked '"?>/> Women <!--if$arr[2] False is selected by default---</div> <divclass= "Form" > Nation: <select name= "Nation" > <?PHP$sql= "SELECT * FROM Nation"; $r=$db->query ($sql); $attr=$r-Fetch_all (); foreach($attr  as $v)            {                if($v[0]==$arr[3])//If the code of nationality equals this person's national code, the output is selected by default                {                    Echo"<option selected= ' selected ' value= ' {$v[0]} ' >{$v[1]} </option> "; }                Else                  //otherwise output the normal                {                    Echo"<option value= ' {$v[0]} ' >{$v[1]} </option> "; }            }            ?> </select> </div> <divclass= "form" > Birthday: <input type= "text" name= "Birthday" value= "<?php Echo$arr[4]?> "/></div><br/> <divclass= "form" > <input type= "Submit" value= "Modify data"/></div></form><div><a href= "main.php" > Main Page </a></div></body>

Modifying the data page is complicated because the default value needs to be loaded according to the code passed in.

2. Set up Modification Data 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=NewMysqli ("localhost", "root", "xiao8888", "MyDB");!Mysqli_connect_error() or die("Connection failed!"));$sql= "Update Info set Name= ' {$name} ', sex={$s},nation= ' {$nation} ', birthday= ' {$birthday} ' where code= ' {$code}‘";//SQL statement to write well$r=$db->query ($sql);if($r){    Header("location:main.php"); }Else{    Echo"The modification failed!"; }

Data Access ... Additions and deletions (i)

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.