• php数据访问(修改)


    修改:跟添加相似,需要显示默认值

    先嵌入php代码  查询数据库

    $code = $_GET["c"];
    
    $db = new MySQLi("localhost","root","159357","renyuanxinxi");
    
    $ainfo = "select * from info where code='{$code}'";
    
    $binfo = $db->query($ainfo);
    
    $cinfo = $binfo->fetch_row();

    在网页中显示所查询的值(在表单中嵌入相应的简短php代码,进行赋值设定)

    <form action="xiugaichuli.php" method="post">
        <div><input type="hidden" name="code" value="<?php echo $ainfo[0]; ?>" /></div>
        <div>姓名:<input type="text" name="name" value="<?php echo $ainfo[1]; ?>" /></div>
        
        
        
        <div>性别:                           //true   false判定
        <input type="radio" name="sex" value="1" <?php echo $ainfo[2]?"checked='checked'":"" ?> />&nbsp;
        <input type="radio" name="sex" value="0" <?php echo $ainfo[2]?"":"checked='checked'" ?> /></div>
        
        
        
        <div>民族:
        <select name="nation">
            <?php
            
            $sql = "select * from nation";
            $result = $db->query($sql);
            $attr = $result->fetch_all();
            
            foreach($attr as $v)               //下拉列表设默认
            {
                if($v[0]==$ainfo[3])
                {
                    echo "<option selected='selected' value='{$v[0]}'>{$v[1]}</option>";
                }
                else
                {
                    echo "<option value='{$v[0]}'>{$v[1]}</option>";
                }
            }
            ?>
        </select>
        </div>
        
        
        
        <div>生日:<input type="text" name="birthday" value="<?php echo $ainfo[4]; ?>" /></div>
        
        <div><input type="submit" value="修改" /></div>
    </form>        

    建立修改处理页面(先取值、再用修改语句 根据主键值 选择所要修改的列、然后再执行)

    <?php
    
    $code = $_POST["code"];                         //取值
    $name = $_POST["name"];
    $sex = $_POST["sex"];
    $nation = $_POST["nation"];
    $birthday = $_POST["birthday"];
    
    
    $db = new MySQLi("localhost","root","159357","renyuanxinxi");
    
    $sql = "update info set name='{$name}',sex={$sex},nation='{$nation}',birthday='{$birthday}' where code='{$code}'";//用修改语句 根据主键值 选择所要修改的列
    
    $db->query($sql);                 //执行
    
    header("location:main.php");
  • 相关阅读:
    Python引入pandas报错ValueError: numpy.ufunc has the wrong size, try recompiling
    Oracle TNS无法解析ORA-12154报错
    python两个一维list列表合并
    SQL数据表加索引CREATE INDEX
    Python 格式化输出
    Python中三个双引号的作用
    2.认识素描
    如何快速学习Tableau Desktop
    1.怎样学习素描
    正点原子嵌入式Linux笔记3——Ubuntu软件安装
  • 原文地址:https://www.cnblogs.com/u1020641/p/6014673.html
Copyright © 2020-2023  润新知