Data access to change and delete

Source: Internet
Author: User

First, view the data

It's natural to make connections, connect to the database, then write SQL statements, execute SQL statements, and learn the data access process yesterday.

Output a table, the use of the table is the label, the first is the output of a line of content, that is, the table field name row. For example:

<table width= "100%" height= "100%" border= "1px" cellpadding= "0" cellspacing= "0" >      <tr>      <td> Ref. </td>        <td> name </td>        <td> price </td>        <td> Origin </td>        <td > Inventory </td>      </tr>  </table>

Then there is the display of the data, which can be embedded in the PHP language for the output of the data, which uses the content of the data access (said yesterday). For example:

<?php$db = new Mysqli ("localhost", "root", "123", "Test2"), $sql = "SELECT * from Fuzhuang";  All data in the query table $result = $db->query ($sql);    /Execute SQL statement $attr = $result->fetch_all (), foreach ($attr as $v)    //Traverse {echo ' <tr><td>{$v [0]}</td> <td>{$v [1]}</td><td>{$v [2]}</td><td>{$v [3]}</td><td>{$v [4]}</td ></tr> ";  }? >    

Ii. deletion of data

(1) Add a field at the end of the table. Cases:

<table width= "100%" height= "100%" border= "1px" cellpadding= "0" cellspacing= "0" >      <tr>      <td> Ref. </td>        <td> name </td>        <td> price </td>        <td> Origin </td>        <td > Inventory </td>        <td> operations </td>      </tr>   </table>

Then it is to add the "delete" data in the column of the operation, so that in the embedded PHP language of the traversal, plus the traversal of the last column of cells can be, the direct deletion will cause misoperation, you can add a click event. Cases:

foreach ($attr as $v) {echo "<tr><td>{$v [0]}</td><td>{$v [1]}</td><td>{$v [2]} </td><td>{$v [3]}</td><td>{$v [4]}</td><td> <a href= ' test10_shanchu.php? code={$v [0]} ' onclick=\ ' return confirm (' OK to delete? ') \ "> Delete </a> </td></tr>";    The code={$v [0]} uses the Get transfer method here, because the post must be sent in the form, so here is the Get Transfer method}

(2) Delete the data you have a processing page.

In the main table above, the deletion is linked to the delete processing page, so also write a delete processing page to delete. Cases:

<?php$code = $_get["code"];   The delete transfer mode in the primary table uses GET, so here is the get transfer $db = new mysqli ("localhost", "root", "123", "test2");  Create a new link $sql = "Delete from Fuzhuang where ids= ' {$code} '";   Write the SQL statement, because the code is not duplicated, so the lookup code equals the definition of code$r = $db->query ($sql);    Executes the SQL statement if ($r) {header ("location:./test10_main.php");   Delete successfully returned to the main Table page}else{echo "Delete failed! ";}? >

(3) In the main page to browse, in the display of the primary table delete operation can be, delete any data can be, click Delete will pop up a dialog box to reconfirm

Third, add data

(1) Add a link to the main table to add data, add the data will certainly have some hints, add what data, naturally need a page to write

<a href= "test10_tianjiashuju.php" > Add Data </a>   //link to this page to add data

(2) in the writing of the added page, the corresponding text box should be built according to the table in the database.

<body>        

(3) Add data to the page to write, so that when you click the Add button, you should also have an added processing page, in the form has been the corresponding name can be connected to this processing page to add data to the database: action= "test10_tianjiachuli.php"

<?php//corresponding because the way to add data is post, so this is also used post, here is the corresponding field name in the datasheet $ids = $_post["IDs"]; $name = $_post["name"]; $price = $_ post["Price"]; $chandi = $_post["Chandi"]; $kucun = $_post["Kucun"];//build connection $db = new mysqli ("localhost", "root", "123", " Test2 ");//write SQL statement: Because it is writing data to the database, use the Add data statement $sql =" INSERT into Fuzhuang values (' {$ids} ', ' {$name} ', ' {$price} ', ' {$ Chandi} ', ' {$kucun} ') ';//Execute SQL statement = $db->query ($sql), if ($r) {header ("location:test10_main.php");// Add successful return to Main table to see Add result}else{echo "Add failed! ";}? >

Iv. Modification of data

It is easy to modify a table with no connection, the trouble is that there is a linked table, and the following is the modification of the table with the connection

(1) If there is no foreign key table, then modify the normal modification of the page and modify the processing page can be, but with the foreign key table, in carrying out the time to write the corresponding conditions.

For example, there is a table with a gender, but the creation of the table is 1 or both, if the user does not know what 1 or 2 represents, so it is necessary to deal with the user can understand the data

while ($attr = $result->fetch_row ()) {$sex = $attr [2];      Assign the column with the gender of the index number 2 to Sex$sex = $sex? " Male ":" Female ";   This processing can let the user understand, because the final display to the user is "male" "female", true returns the male, FALSE returns the female echo "<tr><td>{$attr [0]}</td><td>{ $attr [1]}</td><td>{$sex}</td><td>{$an [0]}</td><td>{$attr [4]}</td> <td><a href= ' test101_xiugai.php?code={$attr [0]} ' > Modify </a></td></tr> ';}   The index number in the re-output is 2, and it needs to be modified into a defined sex.

(2) Now the sex that column can let the user understand, but the national because is the foreign key, such display user also does not understand, so in the traverse in addition to deal with gender, but also to deal with ethnic

<?php $db = new mysqli ("localhost", "root", "123", "test3"); $sql = "SELECT * from Info"; $result = $db->query ($sql); while ($attr = $result->fetch_row ()) {$nation = $attr [3], $sqln = "SELECT name from Nation where code= ' {$nation} '";    Make SQL statements query the names and nationalities of the national tables $rn = $db->query ($SQLN);      Execute SQL statement        $an = $rn->fetch_row ();       echo "<tr><td>{$attr [0]}</td><td>{$attr [1]}</td><td>{$attr [2]}</td> <td>{$an [0]}</td><td>{$attr [4]}</td><td><a href= ' test101_xiugai.php?code={$ Attr[0]} ' > Modify </a></td></tr> ';}    {$an [0]} This is the traversal of the national name?>

(3) Make changes to the page, because it is modified, there will definitely be a default value: embedded in the PHP language, the normal modification of the page, so that the name of the table and the definition of the code names equal

<?php$code = $_get["code"]; $db = new mysqli ("localhost", "root", "123", "test3"); $sql = "SELECT * from info where code= ' {$ Code} ' ";     Query the info table for the code and get pass the code equal all information $result = $db->query ($sql); $attr = $result->fetch_row (); >

After this, the text box has value values, so that the value is equal to the corresponding index number, but the gender is to be selected by default the corresponding gender, in addition to equal to the corresponding index number also to let True is selected, False is not selected

<input type= "hidden" name= "code" value= "<?php echo $attr [0]?>"/>  <div> Name: <input type= "Text" Name= "name" value= "<?php echo $attr [1]?>"/></div> <div> Gender:      <input <?php echo $attr [2]? " Checked= ' checked ' ":" "?> type=" Radio "name=" Sex "  value=" 1 "/> male   //checked equals itself is selected by default      <input <?php echo $attr [2]? ":" Checked= ' checked ' ";?> type=" Radio "name=" Sex "value=" 0 "/> Women </div> <div> National:</div>   //In the code below show how to make it the default <div> birthday: <input type= "text" name= "Birthday" value= "<?php echo $ ATTR[4]?> "/></div>   

Because the nation has more than one, can show all the people, let the user choose to make changes, so you can make a drop-down menu

<div> nationalities:         <select name= "Nation" >            <?php $sqln = "SELECT * from Nation";   Query all data in the Nation table $rn = $db->query ($SQLN);  while ($attr 1 = $rn->fetch_row ()) {if ($attr [3]== $attr 1[0])   //info The names of the families and nation tables in the table are the same {echo ' <option Selected= ' selected ' value= ' {$attr 1[0]} ' >{$attr 1[1]}</option> ';//The index number of the parse is the name of the nation in the nation table,    Selected equals this value is selected by default} else {echo ' <option value= ' {$attr 1[0]} ' >{$attr 1[1]}</option> '; }}?>            </select>         </div>

(4) The last is to modify the processing of the page, the page SQL statement is to modify the statement, and then return to the main page can be

<?php$code = $_post["code"]; $name = $_post["name"]; $sex = $_post["Sex"]; $nation = $_post["Nation"]; $birthday = $_post ["Birthday"]; $db = new mysqli ("localhost", "root", "123", "Test3"), $sql = "Update info set name= ' {$name} ', sex={$sex}, Nation= ' {$nation} ', birthday= ' {$birthday} ' where code= ' {$code} ';   SQL Modify statement = $db->query ($sql), if ($r) {header ("location:test101_main.php");//Returns the page}else{echo "Modify failed! ";}? >

This is the addition and deletion of the examples of change, there are some basic content.

Data access to change 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.