首页 > 代码库 > 如何修改数据 练习

如何修改数据 练习

    
    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=http://www.mamicode.com/‘shanchu.php?code={$attr[0]}‘ onclick=\"return confirm(‘确定删除么‘)\">删除</a>
    <a href=http://www.mamicode.com/‘xiugai.php?code={$attr[0]}‘>修改</a>
    </td></tr>";//用了一个转义字符输出

技术分享

技术分享

如何做修改页面

<body>
<h1>修改水果</h1>
<?php
$code = $_GET["code"];
//造连接对象
$db = new MySQLi("localhost","root","15864477048","test");

//写SQL语句
$sql ="select* from shuiguobiao where ids=‘{$code}‘";

//执行
$result = $db->query($sql);

//取数据
$attr = $result->fetch_row();

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

技术分享

技术分享

<?php
$ids = $_POST["ids"];
$name = $_POST["name"];
$price = $_POST["price"];
$chandi = $_POST["chandi"];
$numbers = $_POST["numbers"];

//造连接对象
$db = new MySQLi("localhost","root","15864477048","test");

//写SQL语句
$sql ="update shuiguobiao set
name=‘{$name}‘,price=‘{$price}‘,source=‘{$chandi}‘,numbers=‘{$numbers}‘ where ids=‘{$ids}‘";

//执行
$r = $db->query($sql);

if($r)
{
    header("location:zengshangai.php");
}
else
{
   echo "修改失败!";    
}

技术分享

技术分享

 

民族修改例题

如何把性别和民族进行用户可观看

<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","15864477048","test");
$sql ="select * from mingzibiao";

$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=http://www.mamicode.com/‘xiugairenyuan.php?code={$v[0]}‘>修改";
}
?>

</table>
</body>

技术分享

如何做修改

  echo "<tr><td>{$v[0]}</td><td>{$v[1]}</td><td>{$sex}</td><td>{$an[0]}</td><td>{$v[4]}</td><td><a href=http://www.mamicode.com/‘xiugaiminzu.php?code={$v[0]}‘>修改</a></td></tr>";

技术分享

技术分享

 如何做性别,民族的默认选中

<div>
      性别:
      <input <?php echo $attr[2]?"checked=‘checked‘":"" ;?>type="radio" name="sex"  value="http://www.mamicode.com/1"/><input <?php echo $attr[2]?"":"checked=‘checked‘" ?>type="radio" name="sex" value="http://www.mamicode.com/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=http://www.mamicode.com/‘{$vn[0]}‘>{$vn[1]}</option>"; 
           }
           else
           {
               
               echo "<option value=http://www.mamicode.com/‘{$vn[0]}‘>{$vn[1]}</option>";
           }
      }
      ?>
      </select>
      
</div>

 

 技术分享

技术分享

如何进行修改

<?php
$code =$_POST["code"];
$name =$_POST["name"];
$sex =$_POST["sex"];
$nation =$_POST["nation"];
$birthday =$_POST["birthday"];

$db = new MySQLi("localhost","root","15864477048","test");
$sql = "update mingzibiao set name=‘{$name}‘,sex=‘{$sex}‘,nation=‘{$nation}‘,birthday=‘{$birthday}‘ where code=‘{$code}‘";

$r = $db->query($sql);

if($r)
{
   header("location:mingzi.php");
}
else
{
   echo "修改失败!";    
}

技术分享

技术分享

 

如何修改数据 练习