PHP implements database addition, deletion, modification and query

1. Query:

Data display, here you can embed php for data output

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>无标题文档</title></head><body><table width="100%" border="1" cellpadding="0" cellspacing="0"><tr><td>代号</td><td>名称</td><td>性别</td><td>生日</td><td>班级</td><td>操作</td></tr><?php$db = new MySQLi("localhost","root","12345678","heiheihei");//连接数据库$sql = "select * from student ";//写sql语句$r = $db->query($sql);//Execute sql statement and return to rif($r)//condition {while ($attr = $r->fetch_row()){$ssex = "";if($attr[2]){$ssex = "Male ";}else{$ssex = "女";}echo"<tr><td>{$attr[0]}</td><td>{$attr[1]}</td><td>{ $ssex}</td><td>{$attr[3]}</td><td>{$attr[4]}</td>//Add a click event to prevent accidentally deleting <td ><a οnclick=\"return confirm('Are you sure you want to delete????')"href='shanchu.php?sno={$attr[0]}'>Delete</a><a href='xiugai .php?sno={$attr[0]}'>Modify</a> </td> </tr>";}} ?></table><a href="tianjia.php" rel="external nofollow" >Add page</a></body></html>td>{$attr[1]}</td><td>{$ssex}</td><td>{$attr[3]}</td><td>{$attr[4]}</ td>//Add a click event to prevent accidentally deleting <td><a οnclick=\"return confirm('Are you sure you want to delete????')"href='shanchu.php?sno={$attr [0]}'>Delete</a><a href='xiugai.php?sno={$attr[0]}'>Edit</a> </td> </tr>";}} ?> </table><a href="tianjia.php" rel="external nofollow" >Add page</a></body></html>td>{$attr[1]}</td><td>{$ssex}</td><td>{$attr[3]}</td><td>{$attr[4]}</ td>//Add a click event to prevent accidentally deleting <td><a οnclick=\"return confirm('Are you sure you want to delete????')"href='shanchu.php?sno={$attr [0]}'>Delete</a><a href='xiugai.php?sno={$attr[0]}'>Edit</a> </td> </tr>";}} ?> </table><a href="tianjia.php" rel="external nofollow" >Add page</a></body></html> } } ?></table><a href="tianjia.php" rel="external nofollow" >添加页面</a></body></html> } } ?></table><a href="tianjia.php" rel="external nofollow" >添加页面</a></body></html>

2. Deleted processing page

When deleting, it is linked to the delete processing page, so a delete processing page should also be written:

<?php$aaa = $_GET ["sno"]; //get used in the delete method, as usual $db = new mysqli("localhost","root","12345678","heiheihei");//Connect. ..$sql = "delete from student WHERE sno='{$aaa}'";//write sql statement, sno primary key if($db->query($sql)) //execute sql statement {header("location :text.php");//Go back to the table page after deleting}else{ echo "Deletion failed";}?>

Here is an effect picture:

3. Add data:

Click to enter the add page

Add page:

<body><h1>Add</h1><form action="add.php" method="post" ><div>Code: <input type="text" name="sno"/></div>< div>Name:<input type="text" name="sname"/></div><div>Gender: <input type="radio" value="1" name="sex" />Male<input type ="radio" value="0" name="sex"/>Female</div><div>Date:<input type="text" name="sbirthday"/></div>//Sex when creating the table It is represented by 1 or 2. If you do not know what 1 or 2 represents, you need to process it and process it into a male and female that the user can understand. <div>Class:<select name="class"> <?php $db = new MYSQLi("localhost","root","12345678","heiheihei");//Connect... $sql = "select * from class ";//write sql... $ r = $db->query($sql);//Execute...return...while($arr = $r->fetch_row()){echo "<option value='{$arr[0]}'>{$arr[1]}</option >";//Add back to the form page after adding}?></select></div><div><input type="submit" value="add"/></div></form></body>

Adding also requires a processing page to determine the addition:

<?php$sno = $_POST["sno"];//The $_POST variable is used to collect values ​​from the form with method="post". $sname = $_POST["sname"];$ssex = $_POST["ssex"];$sbirthday = $_POST["sbirthday"];$class = $_POST["class"];$db = new mysqli( "localhost","root","12345678","heiheihei");$sql = "insert into student VALUES ('{$sno}','{$sname}','{$ssex}','{$ sbirthday}','{$class}')";//Add data written to the database if($db->query($sql)){ header("location:text.php"); //header( ) Function to send the original HTTP header to the client. }else {echo "Failed to add";}?>

Effect picture:

4. Modify data: the primary key cannot be modified! !

<html xmlns="http://www.w3.org/1999/xhtml"><head>  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  <title>无标题文档</title></head><body><h1>修改</h1><?php$sno = $_GET{"sno"};$db = new mysqli("localhost","root","12345678","heiheihei");$sql = "select * from student WHERE sno='{$sno}'";$r = $db->query($sql);$arr = $r->fetch_row();?>  <form action="update.php" method="post">  <div>代号:<input readonly="readonly" type="text" name="sno" value="<?phpecho $arr[0];?>"/></div>//readonly只可读 <div>名称:<input type="text" name="sname" value="<?php echo $arr[1]; ?>"/></div> <div>性别: <input type="radio" name="ssex" value="1" <?php echo $arr[2]?"checked='checked'":""; ?>/>男 <!-- 三元运算符,如果性别=ture,默认值就在男上面,否则空--> <input type="radio" name="ssex" value="0" <?php echo $arr[2]?"":"checked='checked'"; ?>/>女 </div> <div>日期:<input type="text" name="sbirthday" value="<?php echo $arr[3]; ?>"/></div> <div>班级:<select name="class">//value取默认值   <?php      $sclass = "select * from class";      $rclass = $db->query($sclass);      while($attr = $rclass->fetch_row()) //Fetched class information {//Determine whether the class to be output is the same as the person's if($arr[4]==$attr[0])//arr is the class name, attr It is the code name of the class, two tables {echo "<option value ='{$attr[0]}' selected='selected'>{$attr[1]}</option>";} else{ echo "<option value ='{$attr[0]}'>{$attr[1]}</option>";}} ?> </select></div> <div><input type="submit" value="Edit Finished "/></div></form></body></html>/select></div>  <div><input type="submit" value="修改完毕"/></div></form></body></html>/select></div>  <div><input type="submit" value="修改完毕"/></div></form></body></html>

Modified processing page:

<?php$sno = $_POST["sno"];$sname = $_POST["sname"];$ssex = $_POST["ssex"];$sbirthday = $_POST["sbirthday"];$class = $_POST["class"];$db = new mysqli("localhost","root","12345678","heiheihei");$sql = "update student set sname='{$sname}',ssex='{$ssex}',sbirthday='{$sbirthday}',class='{$class}' WHERE sno='{$sno}'";//看一下是不是传递过来的sno值;if($db->query($sql)){  header("location:text.php");}else{  echo "修改失败";}?>

Revised renderings:

Guess you like

Origin blog.csdn.net/hdl17822307857/article/details/112727782