Main Page
PHP//1. Making Connection Objects$db=NewMysqli ("localhost", "root", "123", "MyDB");//2. Determine if there is an error!Mysqli_connect_error() or die("Connection Failed! ");//3. Execute SQL statements$result=$db->query ("SELECT * from Info");//4. Reading data$attr=$result-Fetch_all ();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>";}?>
Add data
add.php
Gender:<input type= "Radio" value= "male" name= "sex"/>male<input type= "Radio" value= "female" name= "sex"/>female</div> <div>Ethnicity:<select name= "Nation" > <?PHP$db=NewMysqli ("localhost", "root", "123", "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> & Lt;div><input type= "Submit" value= "Add Data"/></div></form><div><a href= "main.php" > Main Page </a></div>
addchuli.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", "123", "MyDB");$sql= "INSERT into Info values (' {$code}‘,‘{$name}‘,{$s},‘{$nation}‘,‘{$birthday}‘)";$result=$db->query ($sql);if($result){ Header("location:Add.php");//Jump to add.php}Else{ Echo"Add failed! ";}
Delete data
delete.php
<?PHP$code=$_get["Code"];$db=NewMysqli ("localhost", "root", "123", "MyDB");$sql= "Delete from Info where Code = ' {$code}‘";$r=$db->query ($sql);if($r){ Header("location:main.php");}Else{ Echo"Delete failed!" ";}
Modify Home Page
update.php
<body>PHP$code=$_get["Code"];$db=NewMysqli ("localhost", "root", "123", "MyDB");$sinfo= "SELECT * from Info where code= ' {$code}‘";$r=$db->query ($sinfo);$arr=$r->fetch_row ();//all the information about this guy.? ><form action= "updatechuli.php" method= "POST" > <div><input type= "hidden" name= "code" value= "<? PHP Echo$arr[0]?> "/></div> <div> name: <input type=" text "name=" name "value=" <?php Echo$arr[1]?> "/></div> <div>Gender:<input type= "Radio" value= "male" name= "sex" <?phpEcho $arr[2]? " Checked= ' checked ' ":" "?>/>male<input type= "Radio" value= "female" name= "sex" <?phpEcho $arr[2]? "": "checked= ' checked '"?>/>female</div> <div>Ethnicity:<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]) { Echo"<option selected= ' selected ' value= ' {$v[0]} ' >{$v[1]} </option> "; } Else { Echo"<option value= ' {$v[0]} ' >{$v[1]} </option> "; } } ?> </select> </div> <div> Birthdays: <input type= "text" name= "Birthday" value= "<?php Echo$arr[4]?> "/></div> <div><input type=" Submit "value=" Modify Data "/></div></form><div ><a href= "main.php" > Homepage </a></div></body>
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", "123", "MyDB");$sql= "Update Info set Name= ' {$name} ', sex={$s},nation= ' {$nation} ', birthday= ' {$birthday} ' where code= ' {$code}‘ ";$r=$db->query ($sql);if($r){ Header("location:main.php");}Else{ Echo"The modification failed!" ";}
PHP access to data, additions and deletions change