PHP 简单案例[5]

  • 本系列PHP 简单案例通过“问题-代码”的方式介绍各类方法,每篇设置2个PHP综合问题,并给出解决方案。

问题1

现有商品表goods(id,name,num,price,lasttime)和订单表orders(oid, gid,quantity,total), 其中商品表的字段名称分别 是: 商品编号、商品名称、库存数量、单价和最后一次购买日期, 订单表的字段名称分别是: 订单编号、商品编 号、购买数量和总金额。

存有一个sj3.php的页面中, 在两个表格中显示了所有商品和订单信息, 如图所示。点击超链接 Buy, 将购买一件对应的商品, 增加ordersi记录, 修改goods 表的商品剩余数目。
在这里插入图片描述

问题1代码

<?php
	$con=mysql_connect("localhost:3306","root","")
   		 or die("no database!<br>");
//**********found**********
 	mysql_select_db ("db_exam",$con) or die( "database fail!<br>");
	if(isset($_GET['buy']))
	{
		$id=$_GET['buy'];
		$placeorder='insert into orders(gid,quantity) value('.$id.',1)';
		mysql_query($placeorder,$con);
		$subtractgoods='update goods set num=num-1 where id='.$id;
		mysql_query($subtractgoods,$con);
		
	}
	$sql_goods="SELECT id,name,num FROM goods";
    $result_allgoods=mysql_query($sql_goods,$con);
	$sql_orders="SELECT oid,gid,quantity FROM orders";
    $result_allorders=mysql_query($sql_orders,$con);
?>
<table border='1px'>
<caption>All Goods</caption>
  <tr>
    <td>id</td>
    <td>name</td>
    <td>num</td>
    <td>OP</td>
  </tr>
<?php
  while($rows_goods=mysql_fetch_array($result_allgoods)){
?>
  <tr> 

    <td><?php echo $rows_goods[0];?></td>
    <td><?php echo $rows_goods[1];?></td>
    <td><?php echo $rows_goods[2];?></td>
<!--//**********found**********-->
    <td><a href='sj3.php?buy=<?php echo $rows_goods[0];?>'>Buy</a></td>
  </tr>
<?php
  }
 ?> 
 </table><br/><br/>
  <table border='1px'>
  <caption>All Orders</caption>
  <tr>
    <td>oid</td>
    <td>gid</td>
    <td>quantity</td>
  </tr>
<?php
  while($rows_orders=mysql_fetch_array($result_allorders)){
?>
  <tr> 
    <td><?php echo $rows_orders[0];?></td>
    <td><?php echo $rows_orders[1];?></td>
    <td><?php echo $rows_orders[2];?></td>
  </tr>
<?php
//**********found**********
  mysql_free_result($con) //释放资源
 ?> 
  </table>
<?php mysql_close($con);
?>

问题2

在数据库newtest中, 现有学生表、成绩表:
Student(stuid, name, sex) (说明: 学号, 姓名, 性别)
Score(id, stuid, cname, grade) (说明: 编号, 学号, 课程名, 分数)
si3.php的页面中, 在两个表格中显示了所有的学生信息, 如图所示。点击超链接Detail, 将显示该学生的各课程 成绩, 点击Back可返回学生列表页面。

在这里插入图片描述

问题2代码

<?php
//**********found**********
	$con=mysql_connect ("localhost:3306","root","")
   		 or die("no database!<br>");
 		mysql_select_db ("newtest",$con) or die( "database fail!<br>");
		mysql_query("SET NAMES gbk");   
	if(isset($_GET['detail']))
	{
		$id=$_GET['detail'];
		$sql_score='select * from score where stuid='.$id;
//**********found**********
		$result_allscores=mysql_query ($sql_score,$con); //mysql_query
?>
		<table border='1px'>
		  <caption>All Scores</caption>
		  <tr>
			<td>stuid</td>
			<td>cname</td>
			<td>grade</td>
		  </tr>
<?php

  while($rows_orders=mysql_fetch_array($result_allscores)){
?>
  <tr> 
    <td><?php echo $rows_orders[1];?></td>
    <td><?php echo $rows_orders[2];?></td>
    <td><?php echo $rows_orders[3];?></td>
  </tr>
<?php
  }
 ?> 
  </table>
  <a href="sj3.php">Back</a>
	<?php
	}
	else{
		$sql_student="SELECT * FROM student";
		$result_allstudents=mysql_query($sql_student,$con);
?>
		<table border='1px'>
		<caption>All Students</caption>
		  <tr>
			<td>id</td>
			<td>name</td>
			<td>sex</td>
			<td>OP</td>
		  </tr>
<?php

  while ($rows_student=mysql_fetch_array($result_allstudents)){
?>
  <tr> 
    <td><?php echo $rows_student[0];?></td>
    <td><?php echo $rows_student[1];?></td>
    <td><?php echo $rows_student[2];?></td>
    <td><a href='sj3.php?detail=<?php echo $rows_student[0];?>'>Detail</a></td>
  </tr>
<?php
	}
	?> 
	</table><br/><br/>
<?php
  }
//**********found**********
 mysql_close($con);
?>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

高山莫衣

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

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

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

打赏作者

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

抵扣说明:

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

余额充值