Implementing operations on the database in the PHP page

Source: Internet
Author: User
Tags button type

Submit the content of the page and add it to the database:

<?PHP//Var_dump ($_post);$title=$_post["title"];$Author=$_post["Author"];$source=$_post["Source"];$content=$_post["Content"];$time= Time();$db=NewMysqli ("localhost", "root", "123", "Newssystem");$sql= "INSERT into news values (0, ' {$title}‘,‘{$Author}‘,‘{$source}‘,‘{$content} ', now ()) ";Echo $sql;$result=$db->query ($sql);if($result){    Echo"<script type= ' text/javascript ' >window.location.href= ' fabuxinwen.php ';</script> ';}Else{    Echo"Add failed! ";}

Display the contents of the database in the Web page:

//first add a table to the page and set the column name<tableclass= "Table table-bordered" > <thead> <tr> <th>id</th> <th>title</th> <th>author</th> <th>source</th> <th>Date</th> <th>update</th> <th>delete</th> </tr> </thead>//The contents of the table are stored in the database and displayed<tbody> <?PHP$db=NewMysqli ("localhost", "root", "123", "Newssystem"); $sql= "SELECT * FROM News"; $result=$db->query ($sql); if($result){        $arr=$result-Fetch_all (); foreach($arr  as $v){            Echo"<tr> <td>{$v[0]}</td> <td>{$v[1]}</td> <td>{$v[2]}</td> <td>{$v[3]}</td> <td>{$v[5]}</td> <td> <a href= ' xiugai.php?newsid={$v[0]} ' ><button type= ' button ' class= ' btn btn-primary btn-sm ' > Modify </button></a> </td> <td> <a href= ' shanchu.php?title={$v[1]} ' onclick=\ ' return confirm (' Confirm delete? ') \ ">
<button type= ' button ' class= ' btn btn-primary btn-sm ' > Delete </button>
</a> </td> </tr>"; } } ?> </tbody></table>

To delete the data:

<?PHP$title=$_get["title"];$db=NewMysqli ("localhost", "root", "123", "Newssystem");$sql= "Delete from news where title= ' {$title}‘";$result=$db->query ($sql);if($result){    Header("location:chakan.php");}Else{    Echo"Delete failed!" ";}

If you implement bulk Delete:

First, precede each line of the list with a check box:

<td><input type= ' checkbox ' class= ' ck ' name= ' ck[] ' value= ' {$v [0]} '/>{$v [0]}</td>

Then, at the top of the list column, add a check box that can be selected all:

<input type= "checkbox" id= "Ckall"/>

Add JS code to achieve a full selection:

<script type= "Text/javascript" >var ckall = document.getElementById ("Ckall"); Ckall  function() {    var xz = Ckall.  Checked;     var ck = document.getelementsbyclassname ("ck");      for (var i=0;i<ck.length;i++) {        ck[i]. checked = xz;     }} </script>

Add the Bulk Delete button and then make it available to run the code for the next page:

<?PHP$arr=$_post["CK"];//Delete from info where code in (' P001 ', ' p002 ', ' p003 ')$str=implode("‘,‘",$arr);$sql= "Delete from info where code in (' {$str}‘)";$db=NewMysqli ("localhost", "root", "123", "MyDB");$result=$db->query ($sql);if($result){    Header("location:main.php");}Else{    Echo"Delete failed!" ";}

Can implement the bulk delete function.

Modification of data (need to apply both to add and delete):

First, the data to be modified is shown:

//Add a PHP code to the modified page you want to show<?PHP//Remove primary key value$newsid=$_get["NewSID"];//Read the data$db=NewMysqli ("localhost", "root", "123", "Newssystem");$sql= "SELECT * FROM news where newsid={$newsid}";$result=$db->query ($sql);$arr 1=$result-Fetch_row ();?>

It then displays the contents (assigning Values to the text box) within each piece of data, such as: <?php echo $arr 1[0]?>

To create a new PHP-only page:

<?PHP$newsid=$_post["NewSID"];$title=$_post["title"];$Author=$_post["Author"];$source=$_post["Source"];$content=$_post["Content"];$db=NewMysqli ("localhost", "root", "123", "Newssystem");$sql= "Update news set title= ' {$title} ', author= ' {$Author} ', source= ' {$source} ', content= ' {$content} ' where NewSID = {$newsid}";$result=$db->query ($sql);if($result){    Header("location:chakan.php");}Else{    Echo"The modification failed!" ";}

Implementing operations on the database in the PHP page

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.