PHP Web服务端脚本编写示例——图书售卖系统示例[1/2]
PHP Web数据库访问编写简单示例——图书售卖系统示例[2/2]
(2020年1月29日11:32:15更新文章排版,内容未修改)
南邮web技术的第三次实验,看起来与上次第二次的内容差别不大,但这次要访问数据库。用php连接的数据库。
代码如下:(分为main.html、process.php、order_query.html、process_query.php)
main.html
mian.html
<html>
<head>
<title>Welcome to book seller</title>
</head>
<body>
<form action = "process.php" method = "post">
<h2>Welcome!</h2>
<table>
<tr>
<td>please input your name</td>
<td><input type = "text" name = "name" size = "30" /></td>
</tr>
<tr>
<td>please input your address</td>
<td><input type = "text" name = "address" size = "30" /></td>
</tr>
<tr>
<td>please input your zip</td>
<td><input type = "text" name = "zip" size = "30" /></td>
</tr>
</table>
<p>please fiil in the quantify field of the following form</p>
<table border = "border">
<tr>
<th>book</th>
<th>publisher</th>
<th>price</th>
<th>quantitiy</th>
</tr>
<tr>
<td>Web technology</td>
<td>Springer press</td>
<td>$5.0</td>
<td><input type = "text" name = "web_t" size = "7" /></td>
</tr>
<tr>
<td>mathematics</td>
<td>ACM press</td>
<td>$6.2</td>
<td><input type = "text" name = "math" size = "7" /></td>
</tr>
<tr>
<td>principle of OS</td>
<td>Science press</td>
<td>$10</td>
<td><input type = "text" name = "os" size = "7" /></td>
</tr>
<tr>
<td>Theory of matrix</td>
<td>High education press</td>
<td>$7.8</td>
<td><input type = "text" name = "matrix" size = "7" /></td>
</tr>
</table>
<h3>Payment method</h3>
<p>
<input type = "radio" name = "payment" value = "cash" chexked = "unchecked" />
cash <br/>
<input type = "radio" name = "payment" value = "cheque" chexked = "unchecked" />
cheque <br/>
<input type = "radio" name = "payment" value = "credit card" chexked = "unchecked" />
credit card <br/>
<input type = "submit" value = "submit" />
<input type = "reset" value = "reset" />
</p>
</form>
</body>
</html>
process.php
process.php:
<!-- -->
<html>
<head>
<title>This is the output of process.php</title>
</head>
<body>
<?php
$name = $_POST["name"];
$address = $_POST["address"];
$zip = $_POST["zip"];
$web_t = $_POST["web_t"];
$math = $_POST["math"];
$os = $_POST["os"];
$matrix = $_POST["matrix"];
$payment = $_POST["payment"];
if($web_t == "") $web_t = 0;
if($math == "") $math = 0;
if($os == "") $math = 0;
if($matrix == "") $matrix = 0;
$web_t_cost = 5.0*$web_t;
$math_cost = 6.2*$math;
$os_cost = 10*$os;
$matrix_cost = 7.8*$matrix;
$total_num = $web_t+$math+$os+$matrix;
$total_price = $web_t_cost+$math_cost+$os_cost+$matrix_cost;
$con = mysql_connect("localhost" , "root", "");
if(!$con)
{
die('Could not connect:'.mysql_error());
}
mysql_select_db("myEx", $con);
$sql = "insert into customers values('$name', '$address', '$zip')";
if(!mysql_query($sql, $con))
{
die('ERROR:'.mysql_error());
}
if($web_t != 0)
{
$sql = "insert into books values('Web technology', 'Springer press', 5)";
if(!mysql_query($sql, $con))
{
die('ERROR:'.mysql_error());
}
$sql = "insert into orders values('$name', 'Web technology', $web_t)";
if(!mysql_query($sql, $con))
{
die('ERROR:'.mysql_error());
}
}
if($math != 0)
{
$sql = "insert into books values ('mathematics', 'ACM press', 6.2)";
if(!mysql_query($sql, $con))
{
die('ERROR:'.mysql_error());
}
$sql = "insert into orders values ('$name', 'mathematics', $math)";
if(!mysql_query($sql, $con))
{
die('ERROR:'.mysql_error());
}
}
if($os != 0)
{
$sql = "insert into books values ('principle of OS', 'Science press', 10)";
if(!mysql_query($sql, $con))
{
die('ERROR:'.mysql_error());
}
$sql = "insert into orders values ('$name', 'principle of OS', '$os')";
if(!mysql_query($sql, $con))
{
die('ERROR:'.mysql_error());
}
}
if($matrix != 0)
{
$sql = "insert into books values ('Theory of matrix', 'High education press', 7.8)";
if(!mysql_query($sql, $con))
{
die('ERROR:'.mysql_error());
}
$sql = "insert into orders values ('$name', 'Theory of matrix' , '$matrix')";
if(!mysql_query($sql, $con))
{
die('ERROR:'.mysql_error());
}
}
echo'END';
?>
</body>
</html>
order_query.html
order_query.html:
<html>
<head>
<title>order_query.html</title>
</head>
<body>
<form action = "process_query.php" method = "post" align = "middle">
<label>please input customer name</label>
</br>
</br>
<input type = "text" name = "user_name" />
<br/>
<br/>
<input type = "submit" value = "submit" />
</form>
</body>
</html>
process_query.php
process_query.php:
<html>
<head>
<title>porcess_query.php form</title>
</head>
<body>
<?php
$user_name = $_POST["user_name"];
$con = mysql_connect("localhost", "root", "");
if(!$con)
{
die('Could not connect:'.my_sqlerror());
}
mysql_select_db('myEx', $con);
$result = mysql_query("select name, orders.book, publisher, quantity
from orders, books where orders.book = books.book and name = '$user_name'"); //
echo"<table border = '1'>
<tr>
<th>name</th>
<th>book</th>
<th>publisher</th>
<th>quantity</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo"<tr>";
echo"<td>".$row['name']."</td>";
echo"<td>".$row['book']."</td>";
echo"<td>".$row['publisher']."</td>";
echo"<td>".$row['quantity']."</td>";
echo"</tr>";
}
echo"</table>";
mysql_close($con);
?>
</body>
</html>