PHP, mysqli implementation of simple Add, delete, change, check function (beginner)

Source: Internet
Author: User

<title>index.php</title><?php      #连接数据库      $conn  = mysqli_connect ("localhost", "root", "");         #判断是否连接成功     if (! $conn) {        echo  "failed";     }        //Select Database     mysqli_select_db ($ Conn, "BBS");         //preparing SQL statements      $sql  =  "Select * from bbs_user";         //Send SQL statements       $obj  = mysqli_query ($conn, $sql);         echo   "<center>";    echo  "<table border = 1 cellspacing  =  ' 0 '  cellpadding =  ' >;    echo  ' <th> ref. &LT;/TH ><th> name &Lt;/th><th> Password </th><th> Address </th><th> gender </th><th> age </th><th > Operation </th> ";     while ($row  = mysqli_fetch_assoc ($obj)) {         echo  "<tr>";             echo  ' <td> '. $row [' id ']. ' </td> ';            echo  ' <td> '. $ row[' username ']. ' </td> ';            echo  ' <td> '. $ row[' password ']. ' </td> ';            echo  ' <td> '. $ row[' address ']. ' </td> ';            echo  ' <td> '. $ row[' sex ']. ' </td> ';            echo  ' <td> '. $ Row[' age ']. ' </TD> ';            echo  ' <td><a  href =  "del.php?id=". $row [' id ']. ' " > Delete </a>/<a href =  "update.php?id=". $row [' id ']. ' " > Modify </a></td> ';        echo  ' </tr> ';     }        echo  "</table>";     echo  "<a href =  ' add.php ' > Add </a>";     echo   "<center>";         //close Connection     mysqli_ Close ($conn);? >



Click Delete:
<title>del.php</title><?php $id =$_get[' id '];    $link = Mysqli_connect (' localhost ', ' root ', ');    if (! $link) {exit (' connection failed ');    } mysqli_select_db ($link, ' BBS ');    $sql = "Delete from Bbs_user where id = $id";    $result = Mysqli_query ($link, $sql);    if ($result && mysqli_affected_rows ($link)) {echo "delete succeeded <a href = ' index.php ' > Return </a>";    }else{echo "Delete failed"; } mysqli_close ($link);? >



Change data:

<?php     $id  = $_get[' id '];        $ Link = mysqli_connect (' localhost ', ' root ', ');     if (! $link) {         exit (' connection failed ');    }         mysqli_select_db ($link, ' BBS ');     $sql  =  "Select * from bbs _user where id = $id ";     $obj  = mysqli_query ($link, $sql);      $row  = mysqli_fetch_assoc ($obj);? ><form action= "doupdate.php?" >    <!--use hidden to get ID, other currently not-->    <input type= "hidden"  name= "id"  value =  "<?php echo  $id;? > " />    <table border = 0 cellpadding="   cellspacing= "0" >        <tr><td> number:</td><td><?php echo  $row [' id '];? ></td></tr>        <tr><td> User name:</td> <td><input type= "text"  name =  "username"  value= "<?php echo  $row [' username '];? > " /></td></tr>        <!--<tr><td > Password: </td><td><input type= "text"  name =  "password"  value= "<?php  echo  $row [' Password '];? > "&NBSP;/&GT;&LT;/TD&GT;&LT;/TR&GT;--&GT;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&LT;TR&GT;&LT;TD > Password:</td><td><?php echo  $row [' Password '];? ></td></tr>        <tr><td> Address:</td> <td><input type= "text"  name= "Address"  value= "<?php echo  $row [' address '];? > " /></td></tr>        <tr><td> Sex: </td><td><input type= "text " name=" Sex " value=" <?php echo  $row [' sex '];? > " /></td></tr>        <tr><td> Age: </td><td><input type= "Text"  name= "Age"  value= "<?php echo  $row [' Age ' ];? > " /><br />        <tr><td colspan = "2"  align= "center" ><input type= "submit"  name= " value=" Commit " /></td> </tr>    </table></form>

Execute changes:

<?php      $id  = $_get[' id '];     $username  =  $_get[' username '];    # $password  = $_get[' password '];      $address  = $_get[' address '];     $sex  = $_get[' sex '];      $age  = $_get[' age '];          $link  =  mysqli_connect (' localhost ', ' root ', ');     if (! $link) {         exit (' connection failed ');     }        mysqli _select_db ($link, ' BBS ');     $sql  =  "Update bbs_user set username  =  ' $username ', address =  ' $address ', sex =  ' $sex ',  age =   ' $age '  where id = $id ";     $obj  = mysqli_query ($link, $sql);     iF ($obj  && mysqli_affected_rows ($link)) {        echo   "Modify success <a href =  ' index.php ' > Return </a>";    }else{         echo  "Modify Failed";    }         mysqli_close ($link);    ?>


Add Data:
<form action =  ' doadd.php '  method= ' get ' >      ID: <input type= "text"  name= "id"  value= ""/><br />      Name: <input type= "text"  name= "username"  value= ""/><br />      Password: <input type= "text"  name= "password"  value= ""/><br />      Address: <input type= "text"  name= "Address"  value= ""/><br />      Sex: <input type= "Radio"  name= "Sex"  value =  "male"/> Male           <input type= "Radio"  name= "Sex"  value= "women"  /> women < br />    : <input type= "text"  name= "Age"  value= ""/><br  />    <input type= "Submit"  value= "Submission" &NBSP;/></FORM> 


To perform the Add data:
<?php     //get added data information      $id  = $_get[' id '];      $username  = $_get[' username '];     $password  = md5 ($_ get[' password ');//encryption      $address  = $_get[' address '];    $ sex = $_get[' sex '];     $age  = $_get[' age '];    // Connect to Database      $link  = mysqli_connect (' localhost '  ,  ' root '  ,  ');         //determine if the connection succeeds     if (! $link) {         exit (' Connection database failed '. Mysqli_connect_error ());    }         //Select Database     mysqli_select_db ($link  ,  ' BBS ');         //preparing SQL statements      $sql  =  " Insert into bbs_user (iD,username,password,address,sex,age)  values (' $id ', ' $username ', ' $password ', ' $address ', ' $sex ', ' $age ');         //Sending SQL statements      $obj  = mysqli_query ($link  ,  $sql);         if ($obj) {         echo  Add success <a href =  ' index.php ' > Return </a> ';     }else{        echo  "Add failed";     }     mysqli_close ($link);? >

PHP, mysqli implementation of simple Add, delete, change, check function (beginner)

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.