View Code

修改

上一篇,我们讲了一张表如何删除和添加数据,下面讲一下如何修改一张表

还是使用那张水果表,多加上一列,修改

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<h1>水果表</h1>
<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
        <td>代号</td>
        <td>名称</td>
        <td>价格</td>
        <td>产地</td>
        <td>库存</td>
        <td>操作</td>
</tr>
 <?php
    //造连接对象
    $db = new MySQLi("localhost","root","726","text11");
    //写SQL语句
    $sql = "select * from fruit";
    
    //执行
    $result = $db->query($sql);
    
    //取数据
    /*$attr = $result->fetch_all();
    
    foreach($attr as $v)
    {
        echo "<tr><td>{$v[0]}</td><td>{$v[1]}</td><td>{$v[2]}</td><td>{$v[3]}</td><td>{$v[4]}</td></tr>";
    }*/         
    
    while($attr = $result->fetch_row())
    {
        echo "<tr><td>{$attr[0]}</td><td>{$attr[1]}</td><td>{$attr[2]}</td><td>{$attr[3]}</td><td>{$attr[4]}</td><td>
        
        <a href='shanchu.php?code={$attr[0]}' onclick=\"return confirm('确定删除么')\">
        删除
        </a>
        <a href='xiugai.php?code={$attr[0]}'>修改</a>      
        </td></tr>";
    }
    //上述显示数据的两种方法都可以用  
    
    ?> 
</table>

<a href="tianjia.php">添加数据</a>
</body>
<script type="text/javascript">

</script>
</html>

使用的a 标签,href地址是xiugai.php    也就是说待会得新建一个网页,根据索引来修改

这样后面就多了一个修改列:

点击修改,它会自动跳转到xiugai.php页面

建一个新的页面,里面就要写xiugai.php  的内容了

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<h1>修改水果</h1>
<?php
$code = $_GET["code"];
//造连接对象
$db = new MySQLi("localhost","root","726","text11");
//写SQL语句
$sql = "select * from fruit where ids='{$code}'";
//执行
$result =$db->query($sql);
//取数据
$attr = $result->fetch_row();



?>
<form action="xiugaichuli.php" method="post">
<input type="hidden" name="ids" value="<?php echo $attr[0] ?>" />   
<div>名称:<input type="text" name="name" value="<?php echo $attr[1] ?>"/></div>
<div>价格:<input type="text" name="price" value="<?php echo $attr[2] ?>"/></div>
<div>产地:<input type="text" name="source" value="<?php echo $attr[3] ?>"/></div>
<div>库存:<input type="text" name="numbers" value="<?php echo $attr[4] ?>"/></div>
<div><input type="submit" name="修改" /></div>
</form>

</body>
</html>

 

显示的是原先默认的值

 

可以在这个基础上改

点击提交后还要把这些数据提交到一个页面,xiugaichuli.php

所以再建一个网页

<?php
$ids = $_POST["ids"];
$name = $_POST["name"];
$price = $_POST["price"];
$source = $_POST["source"];
$numbers = $_POST["numbers"];
//造连接对象
$db = new MySQLi("localhost","root","726","text11");
//写SQL语句
$sql = "update fruit set name='{$name}',price='{$price}',source='{$source}',numbers='{$numbers}' where ids='{$ids}'";
//执行
$r=$db->query($sql);
if($r)
{
    header("location:shuiguobiao1.php");
    }
else
{
    echo "修改失败!";
    }

修改成功的话直接返回水果表

来看一下,能否修改成功?

 

 

显然成功了

下面再来写两张表,一张主表一张从表,有外键关系的两张表

一张info表

一张nation表

做个info表的网页

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
 <td>代号</td>
 <td>姓名</td>
 <td>性别</td>
 <td>民族</td>
 <td>生日</td>
 <td>操作</td>
</tr>
<?php
$db = new MySQLi("localhost","root","726","shuang");
$sql = "select * from info";
$result = $db->query($sql);
$attr=$result->fetch_all();
foreach($attr as $v)
{
    $sex = $v[2];
    $sex = $sex?"男":"女";     //性别改成汉字
    $nation = $v[3];
    $sqln = "select name from nation where code='{$nation}'";     //显示民族
    $rn = $db->query($sqln);
    $an = $rn->fetch_row();
    echo "<tr><td>{$v[0]}</td><td>{$v[1]}</td><td>{$sex}</td><td>{$an[0]}</td><td>{$v[4]}</td><td><a href='xiugaiinfo.php?code={$v[0]}'>修改</a></td></tr>";
    
    
    }
?>
</body>
</html>

运行起来是这样的一张表:

点击修改会跳转到xiugaiinfo.php  的页面,所以要建一个修改页面

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<?php
$code = $_GET["code"];
$db = new MySQLi("localhost","root","726","shuang");
$sql = "select * from info where code='{$code}'";

$result = $db->query($sql);
$attr = $result->fetch_row();
?>
<h1>修改人员信息</h1>
<form action="infochuli.php" method="post">
<div>
  <input type="hidden" value="<?php echo $attr[0]
  ?>" name="code" />
</div>
<div>
  姓名:
  <input type="text" name="name" value="<?php echo $attr[1]
  ?>" />
</div>
<div>
  性别:
  <input <?php echo $attr[2]?"checked='checked'":
  ""; ?>type="radio" name="sex" value="1" /><input <?php echo $attr[2]?"checked='checked'":
  ""; ?>type="radio" name="sex" value="0" /></div>
<div>
  民族:
 <select name="nation">
 <?php
 $sqln = "select * from nation";
 $rn = $db->query($sqln);
 $an = $rn->fetch_all();
 foreach ($an as $vn)
 {   if($attr[3]==$vn[0])
     {
         echo "<option selected='selected' 
         value='{$vn[0]}'>{$vn[1]}</option>";
         
     }
     else
     {
     echo "<option value='{$vn[0]}'>{$vn[1]}</option>";
     }
 }
 
 ?>
 </select>
</div>
<div>
  生日:
  <input type="text" name="birthday" value="<?php echo $attr[4]
  ?>" />
</div>
<div>
  <input type="submit" value="修改" />
</div>
</form>
</body>
</html>

把原先的数据设为默认值,在这个基础上改

最后点击修改跳转到infochuli.php

所以要建一个infochuli.php的页面

<?php
$code = $_POST["code"];
$name = $_POST["name"];
$sex = $_POST["sex"];
$nation = $_POST["nation"];
$birthbay = $_POST["birthday"];
$db = new MySQLi("localhost","root","726","shuang");
$sql = "update info set name='{$name}',sex={$sex},nation='{$nation}',birthday='{$birthbay}' where code='{$code}'";
echo $sql;
$r = $db->query($sql);
if($r)
{
    header("location:info.php");
    }
else
{
    echo "修改失败!";
    }

如果修改成功的话它会返回info.php页面

来看一下能否修改成功

 

修改成功了!

 

posted @ 2016-12-20 19:52  风中摇曳的小花朵  阅读(178)  评论(0编辑  收藏  举报