web | 实验 3 Web 数据库访问

初始版

<html>
    <head>
	    <meta charset="UTF-8">
		<title>Cashier</title>
	</head>

	<body>
		<?php
		
            $name=$_POST["name"];
			$address=$_POST["address"];
			$zip=$_POST["zip"];
			$quantity1=$_POST["quantity1"];
			$quantity2=$_POST["quantity2"];
			$quantity3=$_POST["quantity3"];
			$quantity4=$_POST["quantity4"];
			$payment=$_POST["pm"];

			if($quantity1=="") $quantity1=0;
			if($quantity2=="") $quantity2=0;
			if($quantity3=="") $quantity3=0;
			if($quantity4=="") $quantity4=0;

			$quantity1_cost=5.0*$quantity1;
			$quantity2_cost=6.2*$quantity2;
			$quantity3_cost=10.0*$quantity3;
			$quantity4_cost=7.8*$quantity4;
			$total_item=$quantity1+$quantity1+$quantity1+$quantity1;
			$total_cost=$quantity1_cost+$quantity2_cost+$quantity3_cost+$quantity4_cost;
            
			// 链接sql
			$con=mysqli_connect("localhost","root","");
			if($con){
		     	echo "Connect database succeed.";
			} 
			else {
				echo "Connect database fail.";
			}
            // 创建数据库
			if (mysqli_query($con,"CREATE DATABASE con")){
			    echo "<br/>Database created";
			}
			else{
			    echo "<br/>Error creating database: " . mysqli_error($con);
			}
			// 在数据库创建表格
			mysqli_select_db($con,"con");
			$sqluser = "CREATE TABLE user 
			(
			name char(40),
			address char(40),
			zip int
			)";
			mysqli_query($con,$sqluser); 
			$sqlbook = "CREATE TABLE book 
			(
			name char(40),
			publisher char(40),
			price double
			)";
			mysqli_query($con,$sqlbook); 
			$sqlform = "CREATE TABLE form 
			(
			name char(40),
			book char(40),
			quantity int
			)";
			mysqli_query($con,$sqlform); 


            //表格user插入买家资料
            $sql="INSERT INTO user (name, address, zip)
			      VALUES ('$name','$address','$zip')";
			if (!mysqli_query($con,$sql)){
			    die('Error: ' . mysql_error());
			}

		    //表格form插入买家四本书的数据
			if($quantity1>0){
				$sql="INSERT INTO form (name, book, quantity)
					  VALUES ('$name','Web technology','$quantity1')";
				if (!mysqli_query($con,$sql)){
					die('Error: ' . mysql_error());
				}
			}
            if($quantity2>0){
				$sql="INSERT INTO form (name, book, quantity)
					  VALUES ('$name','mathematics','$quantity2')";
				if (!mysqli_query($con,$sql)){
					die('Error: ' . mysql_error());
				}
			}
			if($quantity3>0){
				$sql="INSERT INTO form (name, book, quantity)
					  VALUES ('$name','principle of OS','$quantity3')";
				if (!mysqli_query($con,$sql)){
					die('Error: ' . mysql_error());
				}
			}
			if($quantity4>0){
				$sql="INSERT INTO form (name, book, quantity)
					  VALUES ('$name','Theory of matrix','$quantity4')";
				if (!mysqli_query($con,$sql)){
					die('Error: ' . mysql_error());
				}
			}

			// 查询表格book
			echo '<table border="1" cellspacing="1"><tr>';
			$result = mysqli_query($con,"SELECT * FROM book");
			// 输出表格表头
			$row = mysqli_fetch_assoc($result);
			$num_fields=mysqli_num_fields($result);
			$num_rows=mysqli_num_rows($result);
			echo "<br />BOOK TABLE rows : ".$num_rows;
			$keys=array_keys($row);
			for($index=0;$index<$num_fields;$index++)
			{
				print "<th>".$keys[$index]."</th>";
			}
			print "</tr><tr>";
            // 输出表格信息
			//mysqli_fetch_array每获取一条数据就将当前记录指向下一条,当到达记录末尾时,该函数返回false,退出while循环
			for($index=0;$index<$num_rows;$index++)
			{
				echo '<tr>';
				echo '<td>' .$row['name'] . '</td><td>' . $row['publisher'] . '</td><td>' . $row['price'] . '</td>';
				echo '</tr>';
				$row = mysqli_fetch_assoc($result);
			} 
            echo '</tr></table>';

			// 查询表格form
			echo '<table border="1" cellspacing="1"><tr>';
			$result = mysqli_query($con,"SELECT * FROM form");
			// 输出表格表头
			$row = mysqli_fetch_assoc($result);
			$keys=array_keys($row);
			for($index=0;$index<$num_fields;$index++)
			{
				print "<th>".$keys[$index]."</th>";
			}
			print "</tr><tr>";
            // 输出表格信息
			$num_rows=mysqli_num_rows($result);
			echo "<br />FORM TABLE rows : ".$num_rows;
			for($index=0;$index<$num_rows;$index++)
			{
				echo '<tr>';
				echo '<td>' .$row['name'] . '</td><td>' . $row['book'] . '</td><td>' . $row['quantity'] . '</td>';
				echo '</tr>';
				$row = mysqli_fetch_assoc($result);
			}
            echo '</tr></table>';

			// 查询表格user
			echo '<table border="1" cellspacing="1"><tr>';
			$result = mysqli_query($con,"SELECT * FROM user");
			// 输出表格表头
			$row = mysqli_fetch_assoc($result);
			$keys=array_keys($row);
			for($index=0;$index<$num_fields;$index++)
			{
				print "<th>".$keys[$index]."</th>";
			}
			print "</tr><tr>";
            // 输出表格信息
			//mysqli_fetch_array每获取一条数据就将当前记录指向下一条,当到达记录末尾时,该函数返回false,退出while循环
			$num_rows=mysqli_num_rows($result);
			echo "<br />USER TABLE rows : ".$num_rows;
			for($index=0;$index<$num_rows;$index++)
			{
				echo '<tr>';
				echo '<td>' .$row['name'] . '</td><td>' . $row['address'] . '</td><td>' . $row['zip'] . '</td>';
				echo '</tr>';
				$row = mysqli_fetch_assoc($result);
			}
            echo '</tr></table>';


			mysqli_close($con);
		?> 
	</body>
</html>

完整版

<html>
    <head>
	    <meta charset="UTF-8">
	    <title>Welcome to book seller</title>
	</head>
	<body>
	    <form action="third2.php" method="post">
		<table width="1000" align="center">
			<tr>
				<td>
				<p><br /><h1>You are welcome</h1></p>
				<table>
				<tr>
				<td><p>Please input your name </td>    
				<td><label><input type="text" name="name"></label>    </td></p>
				</tr>
				<tr>
				<td><p>Please input your address </td> 
				<td><label><input type="text" name="address"></label> </td></p>
				</tr>
				<tr>
				<td><p>Please input your zip </td>     
				<td><label><input type="text" name="zip"></label>     </td> </p>
				</tr>
				</table><hr />
				</td>
			</tr>
		

            <tr>
				<td>
					<p><h1>please fill in the quantity field of the following form</h1></p>
					<table  border="1" cellspacing="1">
						<tr>
							<th>book</th> 
							<th>publisher</th> 
							<th>price</th> 
							<th>quantity</th>
						</tr>
						 <tr>
							<td>Web technology</td> 
							<td>Springer press</td> 
							<td>$5.0</td> 
							<td><input type="text" name="quantity1"> </td>
						</tr>
						 <tr>
							<td>mathematics</td> 
							<td>ACM press</td> 
							<td>$6.2</td>         
							<td><input type="text" name="quantity2"> </td>
						</tr>
						 <tr>
							<td>principle of OS</td> 
							<td>Science press</td> 
							<td>$10.0</td>  
							<td><input type="text" name="quantity3"> </td>
						</tr>
						<tr>
							<td>Theory of matrix</td> 
							<td>High education press</td> 
							<td>$7.8</td> 
							<td><input type="text" name="quantity4"> </td>
						</tr>
					</table><hr />
				</td>
			</tr>


			<tr>
				<td>
					<p><h1>payment method</h1></p>
					<p>
						<input type="radio" name="pm" value="cash">cash
						<input type="radio" name="pm" value="cheque">cheque
						<input type="radio" name="pm" value="creditcard">credit card
					</p><hr />
				</td>
			</tr>


            <tr>
				<td>
					<br />
					<input type="submit" value="submit" />
					<input type="reset" value="reset" />
					</table>
				</td>
		   </tr>

		</table>
		</form>
	</body>
</html>
<html>
    <head>
	    <meta charset="UTF-8">
		<title>Cashier</title>
	</head>

	<body>
        <table width="900" align="center">
		<tr><td>
		<?php
		
            $name=$_POST["name"];
			$address=$_POST["address"];
			$zip=$_POST["zip"];
			$quantity1=$_POST["quantity1"];
			$quantity2=$_POST["quantity2"];
			$quantity3=$_POST["quantity3"];
			$quantity4=$_POST["quantity4"];
			$payment=$_POST["pm"];

			if($quantity1=="") $quantity1=0;
			if($quantity2=="") $quantity2=0;
			if($quantity3=="") $quantity3=0;
			if($quantity4=="") $quantity4=0;

			$quantity1_cost=number_format(5.0*$quantity1,2);
			$quantity2_cost=number_format(6.2*$quantity2,2);
			$quantity3_cost=number_format(10.0*$quantity3,2);
			$quantity4_cost=number_format(7.8*$quantity4,2);
            
			// 链接sql
			$con=mysqli_connect("localhost","root","");
			if($con){
		     	echo "Connect database succeed.<br/>";
			} 
			else {
				echo "Connect database fail.<br/>";
			}
            // 创建数据库
			if (mysqli_query($con,"CREATE DATABASE IF NOT EXISTS con")){
			    echo "Database created.<hr />";
			}
			else{
			    echo "Error creating database: " . mysqli_error($con);
			}
			// 在数据库创建表格
			mysqli_select_db($con,"con");
			$sqluser = "CREATE TABLE IF NOT EXISTS user (
			name char(40) ,
			address char(40),
			zip int
			)";
			mysqli_query($con,$sqluser); 
			$sqlbook = "CREATE TABLE IF NOT EXISTS book (
			name char(40) primary key,
			publisher char(40),
			price numeric(10,1)
			)";
			mysqli_query($con,$sqlbook); 
			$sqlform = "CREATE TABLE IF NOT EXISTS form (
			name char(40),
			book char(40),
			quantity integer,
			sum numeric(10,1)
			)";
			mysqli_query($con,$sqlform); 


            //表格user插入买家资料
            $sql="INSERT INTO user (name, address, zip)
			      VALUES ('$name','$address','$zip')";
			if (!mysqli_query($con,$sql)){
			    echo('Error: ' . mysql_error());
			}

		    //表格book插入四本书的资料
            $sql = "insert into book (name, publisher, price)
			        values('Web technology','Springer press',5)";
		    mysqli_query($con,$sql);
			$sql = "insert into book (name, publisher, price)
			        values('mathematics','ACM press',6.2)";
	        mysqli_query($con,$sql);
			$sql = "insert into book (name, publisher, price)
			        values('principle of OS','Science press',10)";
			mysqli_query($con,$sql);
            $sql = "insert into book (name, publisher, price)
			        values('Theory of matrix','High education press',7.8)";    
			mysqli_query($con,$sql);

		    //表格form插入买家购物信息
			if($quantity1>0){
				$sql="INSERT INTO form (name, book, quantity,sum)
					  VALUES ('$name','Web technology','$quantity1','$quantity1_cost')";
				if (!mysqli_query($con,$sql)){
					echo('Error: ' . mysql_error());
				}
			}
            if($quantity2>0){
				$sql="INSERT INTO form (name, book, quantity,sum)
					  VALUES ('$name','mathematics','$quantity2','$quantity2_cost')";
				if (!mysqli_query($con,$sql)){
					echo('Error: ' . mysql_error());
				}
			}
			if($quantity3>0){
				$sql="INSERT INTO form (name, book, quantity,sum)
					  VALUES ('$name','principle of OS','$quantity3','$quantity3_cost')";
				if (!mysqli_query($con,$sql)){
					echo('Error: ' . mysql_error());
				}
			}
			if($quantity4>0){
				$sql="INSERT INTO form (name, book, quantity,sum)
					  VALUES ('$name','Theory of matrix','$quantity4','$quantity4_cost')";
				if (!mysqli_query($con,$sql)){
					echo('Error: ' . mysql_error());
				}
			}

            // 查询表格book
			echo '<table border="1" cellspacing="1"><tr>';
			$result = mysqli_query($con,"SELECT * FROM book");
			// 输出表格表头
			$row = mysqli_fetch_assoc($result);
			$num_fields=mysqli_num_fields($result);
			$num_rows=mysqli_num_rows($result);
			echo "<br />BOOK TABLE  ";
			$keys=array_keys($row);
			for($index=0;$index<$num_fields;$index++)
			{
				print "<th>".$keys[$index]."</th>";
			}
			print "</tr><tr>";
            // 输出表格信息
			//mysqli_fetch_array每获取一条数据就将当前记录指向下一条,当到达记录末尾时,该函数返回false,退出while循环
			for($index=0;$index<$num_rows;$index++)
			{
				echo '<tr><td>' .$row['name'] . '</td><td>' . $row['publisher'] . 
				'</td><td>' . $row['price'] . '</td></tr>';
				$row = mysqli_fetch_assoc($result);
			} 
            echo '</tr></table>';

			// 查询表格form
			echo '<table border="1" cellspacing="1"><tr>';
			$result = mysqli_query($con,"SELECT * FROM form");
			// 输出表格表头
			$row = mysqli_fetch_assoc($result);
			$num_fields=mysqli_num_fields($result);
			$keys=array_keys($row);
			for($index=0;$index<$num_fields;$index++)
			{
				print "<th>".$keys[$index]."</th>";
			}
			print "</tr><tr>";
            // 输出表格信息
			$num_rows=mysqli_num_rows($result);
			echo "<br />FORM TABLE ";
			for($index=0;$index<$num_rows;$index++)
			{
				echo '<tr><td>' .$row['name'] . '</td><td>' . $row['book'] . 
				'</td><td>' . $row['quantity'] . '</td><td>' . $row['sum'] .'</td></tr>';
				$row = mysqli_fetch_assoc($result);
			}
            echo '</tr></table>';

			// 查询表格user
			echo '<table border="1" cellspacing="1"><tr>';
			$result = mysqli_query($con,"SELECT * FROM user");
			// 输出表格表头
			$row = mysqli_fetch_assoc($result);
            $num_fields=mysqli_num_fields($result);
			$keys=array_keys($row);
			for($index=0;$index<$num_fields;$index++)
			{
				print "<th>".$keys[$index]."</th>";
			}
			print "</tr><tr>";
            // 输出表格信息
			//mysqli_fetch_array每获取一条数据就将当前记录指向下一条,当到达记录末尾时,该函数返回false,退出while循环
			$num_rows=mysqli_num_rows($result);
			echo "<br />USER TABLE  ";
			for($index=0;$index<$num_rows;$index++)
			{	
				echo '<tr><td>' .$row['name'] . '</td><td>' . $row['address'] . 
				'</td><td>' . $row['zip'] . '</td></tr>';
				$row = mysqli_fetch_assoc($result);
			}
            echo '</tr></table>';
			mysqli_close($con);
		?> 

        <form action="third3.php" method="post">
        <p>
		   <h3><hr />Your information has been entered in the background.</h3>
		   please input customer name: <input type="text" name="username"> 
		</p>
			<input type="submit" value="submit" />
			<input type="reset" value="reset" />
		</form>
        </td></tr>
		</table>
	
	</body>
</html>
<html>
    <head>
	    <meta charset="UTF-8">
		<title>Shopping details</title>
	</head>

	<body>
	    <table width="900" align="center">
		<tr><td>
		<?php
		    $username=$_POST["username"];
			// 链接sql
			$con=mysqli_connect("localhost","root","");
			if($con){
		     	echo "Connect database succeed.<hr />";
			} 
			else {
				echo "Connect database fail.";
			}
            //选择数据库con
			mysqli_select_db($con,"con");
            

			// 查询表格book
			echo '<table border="1" cellspacing="1"><tr>';
			$result = mysqli_query($con,"SELECT * FROM book");
			// 输出表格表头
			$row = mysqli_fetch_assoc($result);
			$num_fields=mysqli_num_fields($result);
			$num_rows=mysqli_num_rows($result);
			echo "<br />BOOK TABLE ";
			$keys=array_keys($row);
			for($index=0;$index<$num_fields;$index++)
			{
				print "<th>".$keys[$index]."</th>";
			}
			print "</tr><tr>";
            // 输出表格信息
			//mysqli_fetch_array每获取一条数据就将当前记录指向下一条,当到达记录末尾时,该函数返回false,退出while循环
			for($index=0;$index<$num_rows;$index++)
			{
				echo '<tr><td>' .$row['name'] . '</td><td>' . $row['publisher'] . '</td><td>' . $row['price'] . '</td></tr>';
				$row = mysqli_fetch_assoc($result);
			} 
            echo '</tr></table>';

			// 查询表格form
			echo '<table border="1" cellspacing="1"><tr>';
			$result = mysqli_query($con,"SELECT * FROM form");
			// 输出表格表头
			$row = mysqli_fetch_assoc($result);
			$keys=array_keys($row);
			for($index=0;$index<$num_fields;$index++)
			{
				print "<th>".$keys[$index]."</th>";
			}
			print "</tr><tr>";
            // 输出表格信息
			//mysqli_fetch_array每获取一条数据就将当前记录指向下一条,当到达记录末尾时,该函数返回false,退出while循环
			$num_rows=mysqli_num_rows($result);
			echo "<br />FORM TABLE  ";
			for($index=0;$index<$num_rows;$index++)
			{
				if($username==$row['name']){
				echo '<tr><td>' .$row['name'] . '</td><td>' . $row['book'] . '</td><td>' . $row['quantity'] . '</td></tr>';
				}
				$row = mysqli_fetch_assoc($result);
			}
            echo '</tr></table>';

			// 查询表格user
			echo '<table border="1" cellspacing="1"><tr>';
			$result = mysqli_query($con,"SELECT * FROM user");
			// 输出表格表头
			$row = mysqli_fetch_assoc($result);
			$keys=array_keys($row);
			for($index=0;$index<$num_fields;$index++)
			{
				print "<th>".$keys[$index]."</th>";
			}
			print "</tr><tr>";
            // 输出表格信息
			//mysqli_fetch_array每获取一条数据就将当前记录指向下一条,当到达记录末尾时,该函数返回false,退出while循环
			$num_rows=mysqli_num_rows($result);
			echo "<br />USER TABLE ";
			for($index=0;$index<$num_rows;$index++)
			{
				if($username==$row['name']){
				echo '<tr><td>' .$row['name'] . '</td><td>' . $row['address'] . 
				    '</td><td>' . $row['zip'] . '</td></tr>';
				}
				$row = mysqli_fetch_assoc($result);
			}
            echo '</tr></table>';


            echo '<hr /><h3>Your shopping details are as follows: </h3>';

			echo '<table border="1" cellspacing="1">';
            $result=mysqli_query($con,"
			select form.name ,form.book, book.publisher,form.quantity,form.sum
            from form , book
			where form.book=book.name 
            ");
			$row = mysqli_fetch_assoc($result);
			$keys=array_keys($row);
			for($index=0;$index<5;$index++)
			{
				print "<th>".$keys[$index]."</th>";
			}
			print "</tr><tr>";
			$num_rows=mysqli_num_rows($result);

            $total_item=0;
			$total_cost=0;

			for($index=0;$index<$num_rows;$index++)
			{
				if($username==$row['name']){
				echo '<tr><td>'.$row['name'] .'</td><td>' . $row['book']  .'</td><td>' . 
					$row['publisher']. '</td><td>' .$row['quantity'] . '</td><td>' .$row['sum'] .'</td></tr>';
				}
				$total_item=$total_item+$row['quantity'];
			    $total_cost=$total_cost+$row['sum'];
				$row = mysqli_fetch_assoc($result);
			}
			echo '</tr></table>';
            echo "<br /><strong>Number of books : </strong>" . $total_item . " <br /><strong>Your total cost :  $</strong>" . $total_cost;
			mysqli_close($con);
		?> 
		</td></tr>
		</table>
	</body>
</html>
  • 1
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值