PHP Connection database to modify the contents of the Web page and delete

Source: Internet
Author: User

Example:

$db=NewMysqli ("localhost", "root", "" "," MyDB ");!Mysqli_connect_error() or die("Connection Failed! ");$sql= "SELECT * from Info,nation where Info.nation=nation.code";$result=$db->query ($sql);if($result){    $attr=$result-Fetch_all (); //Var_dump ($attr);    Echo"<table border= ' 1 ' cellpadding= ' 0 ' cellspacing= ' 0 ' align= ' center ' width= ' 100% ' >"; Echo"<tr> <td> code </td> <td> name </td> <td> sex </td> <td> Nationality </td> <td> Date of birth </td> <td> action </td> </tr> ;"; foreach($attr  as $v)    {        $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>"; }     Echo"</table>";}? ><a href= "add.php" > Add Data </a>

This page is the main page, with the Add data hyperlink at the bottom into the Add Data page , the following is the code to add the page :

<body>male<input type= "Radio" value= "female" name= "sex"/> Female </div><div> Nationality: <select name= "Nation" > <?PHP$db=NewMysqli ("localhost", "root", "" "," 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><div> Birthdays: <input type= "text" name= "Birthday"/></div><div>& Lt;input type= "Submit" value= "Add"/></div></form><div><a href= "test.5.3.php" > Back to Home </a ></div>

Enter the added data on this page, then connect to the background Add Processing page through the method in the form, and add the data to the database by working on it, the code is as follows:

<?PHP$code=$_post["Code"];$name=$_post["Name"];$sex=$_post["Sex"];$nation=$_post["Nation"];$birthday=$_post["Birthday"];$s=1;if($sex= = "female"){    $s=0;}$db=NewMysqli ("localhost", "root", "" "," MyDB ");$sql= "INSERT into Info values (' {$code}‘,‘{$name}‘,{$s},‘{$nation}‘,‘{$birthday}‘)";$result=$db->query ($sql);if($result){    Header("location:Add.php");}Else{    Echo"Add Failed";}?>

The above is the Add feature, followed by the deletion of the background processing page code:

<?PHP$code=$_get["Code"];$db=NewMysqli ("localhost", "root", "" "," MyDB ");$sql= "Delete from Info where code= ' {$code}‘";$r=$db->query ($sql);if($r){    Header("location:test.5.3.php");}Else{    Echo"Delete Failed";}?>

Finally, the function is modified , which is slightly more complicated than the previous two because it involves a default value, the code is as follows:

<Body><H1>modifying data</H1><?php$code= $_get["code"]; $db =new mysqli ("localhost", "root", "", "MyDB"), $sql 1= "select * from Info where code= ' {$code } ' "; $r = $db->query ($sql 1); $arr = $r->fetch_row ();?><formAction= "updatechuli.php"Method= "POST"><Div><inputtype= "hidden"name= "Code"value= "<?php echo $arr [0]?>"/></Div><Div>Name:<inputtype= "text"name= "Name"value= "<?php echo $arr [1]?>"/></Div><Div>Gender:<inputtype= "Radio"value= "Male"name= "Sex"<?php echo $arr [2]? " Checked= ' checked ' ": " "?>/> Male<inputtype= "Radio"value= "female"name= "Sex"<?php echo $arr [2]? ":" Checked= ' checked ' "?>/> Women</Div><Div>Nationality:<Selectname= "Nation">          <?php $sql = "SELECT * from Nation";          $r = $db->query ($sql);          $attr = $r->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>Birthday:<inputtype= "text"name= "Birthday"value= "<?php echo $arr [4]?>"/></Div><Div><inputtype= "Submit"value= "Modify" /></Div></form><ahref= "test.5.3.php">Back to Main Page</a></Body>
<?PHP$code=$_post["Code"];$name=$_post["Name"];$sex=$_post["Sex"];$nation=$_post["Nation"];$birthday=$_post["Birthday"];$s=1;if($sex= = "female"){    $s=0;}$db=NewMysqli ("localhost", "root", "" "," MyDB ");$sql= "Update Info set Name= ' {$name} ', sex={$s},nation= ' {$nation} ', birthday= ' {$birthday} ' where code= ' {$code}‘";$r=$db->query ($sql);if($r){    Header("location:test.5.3.php");}Else{    Echo"Modify Failed";}

This example is mainly to Delete and change the face of the application, in which the data revealed in the previous study, but on the basis of the search and deletion and change .

Add: Use the value that the user typed in the added data page, through the name defined in the form by the POST method, and then in the background through the $_post["value value" to get to a variable, and then connect to the database through the Structured Query language added to the database

Delete: The primary key value that is applied to each row of data, passed to the background by the Get method, and then connected to the database deletes the data through a structured query language

Change: To use the combination of increase and deletion. Background modification processing applies post communication, and the default value is displayed by primary key.

PHP Connection database to modify the contents of the Web page and delete

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.