PHP笔记1-crud

PHPcrud例子

题目:PHP连接Mysql

实验目的:

通过实验实现PHP连接Mysql,并实现对数据的增、删、改、查功能。

实验要求:

1.创建一个数据库,并创建一张书籍表包含如下字段:

a)书籍编号(整型,主键,自动增长)、书籍名称、出版社、价格、数量

2.创建一个PHP项目后,创建html表单页面,实现书籍信息的提交。

3.创建PHP页面实现数据连接并将提交的书籍信息添加到表中。

4.在页面中显示书籍信息,并有对应的修改、删除操作

5.在上述页面中完成书籍的修改、删除操作。

6.编写实验心得。

软件:phpStudy+phpstorm+sqlyog

新建add.htmml

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>实验三</title>
</head>
<body>
<h3>添加书籍</h3>
<form action="add.php" method="post">
    书籍名称:<input type="text" name="name"><p></p>
    出版社:<input type="text" name="place"><p></p>
    价格:<input type="text" name="price"><p></p>
    数量:<input type="text" name="number"><p></p>
    <input type="submit" value="提交">
</form>
</body>
</html>

新建add.php

<?php
$name=$_POST["name"];
$place=$_POST["place"];
$price=$_POST["price"];
$number=$_POST["number"];

$con=mysqli_connect("localhost","root","root","phpdatebase");
$sql="insert into bookinfo (name,place,price,number) values ('$name','$place',$price,$number)";
//echo $sql;
$result=mysqli_query($con,$sql);
if ($result){
//    echo "添加成功";
    include "show.php";
}else{
    echo "添加失败";
}
//mysqli_close($con);

新建show.php

<?php
header("content-type:text/html;charset=utf-8");
$con=mysqli_connect("localhost","root","root","phpdatebase");
$sql="select * from bookinfo ";
//echo $sql;
$result=mysqli_query($con,$sql);
echo "<h3>主页</h3>";
echo "<table border='1'>
<tr>
<th>id</th>
<th>书名</th>
<th>出版社</th>
<th>价格</th>
<th>数量</th>
<th>操作</th>
</tr>";

while($row = mysqli_fetch_array($result))//转成数组,且返回第一条数据,当不是一个对象时候退出
{
    echo "<tr>
    <td> {$row['id'] }  </td>
    <td> {$row['name']} </td>
    <td> {$row['place']} </td>
    <td> {$row['price']} </td>
    <td> {$row['number']}</td>
    <td>
        <a href='delete.php?id={$row['id']}'>删除</a>
        <a href='update1.php?id={$row['id']}'>修改</a>
    </td>
    </tr>";
}
echo "</table>";
echo "
    <a href='add.html'>添加</a>
";

mysqli_close($con);
?>

新建delete.php

<?php
$id=$_GET['id'];
$con=mysqli_connect("localhost","root","root","phpdatebase");
$sql="DELETE FROM bookinfo WHERE id=$id ";
//echo $sql;
$result=mysqli_query($con,$sql);
if ($result){
     echo "删除成功";
    include "show.php";
}else{
    echo "删除失败";
}
//mysqli_close($con);

新建update1.php

<?php
$id=$_GET['id'];
$con=mysqli_connect("localhost","root","root","phpdatebase");
$sql="select * from bookinfo where id=$id ";
//echo $sql;
$result=mysqli_query($con,$sql);
$info=mysqli_fetch_array($result);
echo "<h3>请重新输入</h3>";
echo "
    <form action=\"update2.php\" method=\"post\">
    id:<input type=\"text\" name=\"id\" value='{$info["id"]}'><p></p>
    书籍名称:<input type=\"text\" name=\"name\" value='{$info["name"]}'><p></p>
    出版社:<input type=\"text\" name=\"place\" value='{$info["place"]}'><p></p>
    价格:<input type=\"text\" name=\"price\" value='{$info["price"]}'><p></p>
    数量:<input type=\"text\" name=\"number\" value='{$info["number"]}'><p></p>
    <input type=\"submit\" value=\"提交\">
</form>
";
mysqli_close($con);

新建update2.php

<?php
$id=$_POST['id'];
$name=$_POST['name'];
$place=$_POST['place'];
$price=$_POST['price'];
$number=$_POST['number'];
$con=mysqli_connect("localhost","root","root","phpdatebase");
$sql="UPDATE bookinfo SET NAME='$name',place='$place',price=$price,number=$number  WHERE id=$id";
$result=mysqli_query($con,$sql);
include "show.php";

sql

部分效果图

show

初学php,又想学markdown,所以借用markdwn记录php学习笔记,不喜勿喷

posted @ 2019-03-01 11:05  等不到时光等天亮  阅读(424)  评论(0编辑  收藏  举报