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

时间:2017-11-25 16:59:04

标签: php

在此代码中,有一个输入字段用于更新数据。数据完美更新。但是当我点击“添加”按钮时,我想在输入字段中显示以前的数据。因此,当我单击添加按钮时,我可以在输入字段中看到我以前的数据,然后我可以更新。

我想在更新数据时在数据库的输入字段中显示生物文本。我在输入字段中使用了$ biotext。 但是没有显示先前的数据。输入字段显示 - >  
注意:未定义的变量: /Applications/XAMPP/xamppfiles/htdocs/Social/edit.php 中的生物文字 76 <登记/> 我不知道是什么问题。 请帮我找出问题所在。 提前谢谢。

<?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
?>

1 个答案:

答案 0 :(得分:1)

希望我能理解你的担忧。
在这里,您可以使用以下我编写的代码。 更改连接详细信息,并使用从中获取用户ID的会话变量编辑查询。

<?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>


存储在数据库中 - enter image description here

从数据库中提取并在编辑前显示给用户 enter image description here