使用PHP从Web访问MySQL数据库

数据查询:

查询页面:search.html

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>商品订单查询</title>
</head>
<body>
<h2>商品订单查询</h2>
<form action="results.php" method="post">
    <label>选择查询类型:</label>
    <select name="searchtype">
        <option value="author">Author</option>
        <option value="title">Title</option>
        <option value="isbn">ISBN</option>
    </select>
    <br/>
    <label>输入查询关键字:</label>
    <input name="searchterm" type="text" size="40"/>
    <br/>
    <input type="submit" name="submit" value="查询"/>
</form>
</body>
</html>
查询结果页面:results.php

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>订单查询结果</title>
</head>
<body>
<h2>商品订单查询结果</h2>
<?php
//获取并过滤用户输入的数据
$searchType=$_POST['searchtype'];
$searchTerm=trim($_POST['searchterm']);
//如果用户没有输入任何内容时提示
if(!$searchType||!$searchTerm){
    echo '您没有输入任何搜索内容,请返回后再次输入';
    exit;
}
//去掉所有临时字符,对数据进行格式过滤
if(!get_magic_quotes_gpc()){
    $searchType=addslashes($searchType);
    $searchTerm=addslashes($searchTerm);
}

//连接数据库,@是错误抑制操作符
@ $db=new mysqli('localhost','root','','books');

//如果连接数据库出现异常时提示
if(mysqli_connect_errno()){
    echo '无法连接数据库,请稍后再试';
    exit;
}
//定义SQL语句并执行
$query="SELECT * FROM books WHERE ".$searchType." LIKE '%".$searchTerm."%'";
$result=$db->query($query);
//获取查询结果的数量
$num_results=$result->num_rows;
echo "<p>找到的书籍数量为:".$num_results."</p>";

for($i=0;$i<$num_results;$i++){
    //获取结果中的每一行
    $row=$result->fetch_assoc();
    echo "<p><strong>"
        .($i+1)
        ." Title:"
        .htmlspecialchars(stripslashes($row['title']))
        ."</strong><br/>Author:."
        .stripslashes($row['author'])
        ."<br/>ISBN:"
        .stripslashes($row['isbn'])
        ."<br/>Price:"
        .stripslashes($row['price'])
        ."</p>";
}
//释放结果集
$result->free();
//断开数据库连接
$db->close();
?>
</body>
</html>
添加数据页面:newbook.html

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>添加新书</title>
</head>
<body>
<h2>添加新书</h2>
<form action="insert_book.php" method="post">
    <label>ISBN</label><input type="text" name="isbn" maxlength="13" size="13"/>
    <label>Author</label><input type="text" name="author" maxlength="30" size="30"/>
    <label>Title</label><input type="text" name="title" maxlength="60" size="30"/>
    <label>Price $</label><input type="text" name="price" maxlength="7" size="7"/>
    <input type="submit" value="提交">
</form>
</body>
</html>
添加之后的结果页面:insert_book.php

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>添加书籍后的结果</title>
</head>
<body>
<h2>添加书籍后的结果</h2>
<?php
//获取用户输入的数据
$isbn=$_POST['isbn'];
$author=$_POST['author'];
$title=$_POST['title'];
$price=$_POST['price'];
//如果用户输入的数据为空时提示
if(!$isbn || !$author || !$title || !$price){
    echo "您有其中一项没有输入任何数据,请返回并重试。";
    exit;
}
//去掉所有临时字符,对数据进行格式过滤
if(!get_magic_quotes_gpc()){
    $isbn=addslashes($isbn);
    $author=addslashes($author);
    $title=addslashes($title);
    $price=doubleval($price);
}

//连接数据库
@ $db=new mysqli('localhost','root','','books');
//数据库连接异常提示
if(mysqli_connect_errno()){
    echo '无法连接数据库,请稍后再试';
    exit;
}
/**
 *
//定义SQL语句并执行
$query="INSERT INTO books VALUES ('".$isbn."','".$author."','".$title."','".$price."')";
$result=$db->query($query);
//添加是否成功给予提示
if($result){
echo $db->affected_rows."书籍已经添加到数据库";
}else{
echo "添加异常,书籍没有添加成功";
}
//释放结果集
$result->free();
//断开数据库连接
$db->close();
 */

/**
 * 用Prepared语句
 */
$query="INSERT INTO books VALUES (?,?,?,?)";
$stmt=$db->prepare($query);
$stmt->bind_param("sssd",$isbn,$author,$title,$price);
$stmt->execute();
echo $stmt->affected_rows.'本书添加到数据库';
$stmt->close();
?>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值