[基础]PHP Web数据库访问编写简单示例——图书售卖系统示例[2/2]

3 篇文章 0 订阅
2 篇文章 0 订阅

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>

 

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

源计划猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值