使用mysqli库对mysql进行操作

前台界面:
<html>
<head>
<title>New books</title>
</head>
<body>
<h1>NEW BOOK</h1>
<form action="insertbook.php" method="POST">
<table border="0">
<tr>
<td>ISBN:</td>
<td><input type="text" name="isbn" maxlength="20" size="13"></td>
</tr>
<tr>
<td>Author:</td>
<td><input type="text" name="author" maxlength="50" size="30"></td>
</tr>
<tr>
<td>Title:</td>
<td><input type="=text" name="title" maxlength="50" size="30"></td>
</tr>
<tr>
<td>Price:</td>
<td><input type="text" name="price" maxlength="50" size="30"></td>
</tr>
<tr>
<td align="center" colspan="2"><input type="submit" value="确认"></td>
</tr>
</table>
</body>
</form>
</html>
 
操作页面:(insertbook.php)
<html>
<head>
<title>Insert new books</title>
</head>
<body>
<h1>Insert new books</h1>
<?php
$isbn=$_POST['isbn'];
$author=$_POST['author'];
$title=$_POST['title'];
$price=$_POST['price'];
if (!$isbn || !$author ||!$title || !$price) {
 echo "You have no entered any thing,please try again!";
 exit;
}
$db=new mysqli('host','user','password',database');//connect to database
//output the error if error occurre
if (mysqli_connect_errno()) {
 echo "Error:Coule not connect to the database!Please tyr again later!";
 exit;
}
//query on the database;
//$query="insert into books values('".$isbn."','".$author."','".$title."','".$price."')";
//$result=$db->query($query);
$query="insert into books values(?,?,?,?)";
$stmt=$db->prepare($query);
$stmt->bind_param("sssd",$isbn,$author,$title,$price);
$stmt->execute();
if ($stmt)
{
 echo $stmt->affected_rows.'book insert into database!';
 $stmt->close();
 /*
 echo $db->affected_rows."book inserted into database.<br/>";
 $query_new="select * from books where isbn='".$isbn."'";
 $newbook=$db->query($query_new);
 $row=$newbook->fetch_assoc();//取出每一行的结果
 echo '<strong>ISBN:';
 echo stripslashes($row['isbn']);
 echo '</strong><br/>Author:';
 echo stripslashes($row['author']);
 echo '<br/>Title:';
 echo stripslashes($row['title']);
 echo '<br/>Price:';
 echo stripslashes($row['price']);
 echo '</p>';
 */
}
else
{echo "An error has occurred.The item was not added!";}
$db->close();
?>
</body>
</html>
 
 

你可能感兴趣的:(mysql,数据库,职场,休闲)