dtv11049 2017-11-25 16:59
浏览 58

更新/编辑值时,从输入字段的占位符中获取数据库的值

In this code there is one input field which is used for update data. data is updating perfectly. but i want to show the previous data in my input field when i will click 'Add' button. So that when i click add button i can see my previous data in the input field and then i can update.

I want to show biotext in the input field from database when I update data. i used $biotext in the input field. but the previous data is not showed.The input field shows ->
Notice: Undefined variable: biotext in /Applications/XAMPP/xamppfiles/htdocs/Social/edit.php on line 76
I don't know what is the problem. please help me to find out the problem. thanks in advance.

<?php


// edit bio
// edit bio

if(isset($_GET['edit_id']) && !empty($_GET['edit_id']))
    {
        $id = $_GET['edit_id'];
        $stmt= $db->select('SELECT biotext FROM bio WHERE userid =:userid',array('userid'=>$id));

    }
    

    if(isset($_POST['update'])) 

  {
      $userid = $_SESSION['userid'];
      $biotext = $_POST['biotext'];

      if(empty($biotext))
    {
      $errMSG = "Please update your bio.";
    }

    else 
    {
       
      $user_bio = $db->update("UPDATE bio SET biotext=:biotext WHERE userid=:userid",array('biotext'=>$biotext,'userid'=>$userid));
          
    }
   
  }

?>
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>

    <?php 
if(isset($_GET['edit_id']) && !empty($_GET['edit_id']))
{


?>
          <div class="row main-row">
            <div class="col-md-11 ">
              <?php
      if(isset($errMSG))
      {
        echo $errMSG;
      }

      ?>

        <form method="post" name="form1" action="edit.php">
          <table width="25%" border="0">
            <tr> 
              <td>Add your bio</td>
              <td><input type="text" name="biotext" value="<?php echo $biotext; ?>"></td> 
            </tr>

            <tr> 
            <td></td>
            <td><input type="submit" name="update" value="Add"></td>
            </tr>
          </table>
        </form>
            
          </div>
          </div>
   <?php 

}


// edit bio
// edit bio
?>

</div>
  • 写回答

1条回答 默认 最新

  • duanquanzhi5560 2017-11-25 18:38
    关注

    Hope I was able to understand your concern.
    Here, you can use the below code I've written. Change the connection details and edit the query with the session variable you were fetching the user id from.

    <?php
    define('DB_SERVER', 'localhost');
    define('DB_USERNAME', 'root');
    define('DB_PASSWORD', 'root');
    define('DB_DATABASE', 'stackDB');
    $con = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
    
    if (!$con)
    {
        die('Could not connect: ' . mysqli_error());
    } 
    
    $result= mysqli_query($con, "SELECT biotext FROM bio WHERE userid = 1234");
    $fieldValDemo = mysqli_fetch_row($result);
    ?>
    
    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
    </head>
    <body>
    <div class="row main-row">
        <div class="col-md-11 ">
            <form method="post" name="form1" action="edit.php">
                <table width="25%" border="0">
                    <tr> 
                        <td>Add your bio</td>
                        <td><input type="text" name="biotext" value="<?php echo $fieldValDemo[0]; ?>"></td> 
                    </tr>
    
                    <tr> 
                        <td></td>
                        <td><input type="submit" name="update" value="Add"></td>
                    </tr>
                </table>
            </form>
        </div>
    </div>
    </body>
    </html>
    


    Stored in DB - enter image description here

    Fetched from DB and shown to user before editing enter image description here

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。