php 如何在提交时用sql数据刷新预填充的html输入值

shstlldc  于 2023-08-02  发布在  PHP
关注(0)|答案(1)|浏览(93)

我对PHP很陌生,对SQL也很陌生。
我有个建议

Namebox: <input type="text" name="n" value="<?php echo $row["n"];?>" list="names">

字符串
我用它来更新SQL数据库

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$inputnames = array("n"); 
foreach ($inputnames as $value) {
    $inputvar = $value;
    $var =  $_POST["$inputvar"];
        $conn->query("UPDATE p_intro1 SET $inputvar = '$var' WHERE par=$par");
}
}


(the$par变量是用另一个html输入设置的,它是一个段落的缩写,但基本上与一个id号相同)
所以我的问题是,当我改变Namebox,让我们说,从“史蒂夫”到“鲍勃”,数据库将更新为鲍勃,但预填充的输入保持为“史蒂夫”,直到我再次点击提交,在这一点上,预填充的输入更改为“鲍勃”,但数据库更改回史蒂夫。这意味着每次我点击提交它只是不断变化,从鲍勃到史蒂夫到鲍勃到史蒂夫等。
有办法解决吗?理想情况下,如果我添加一个

onchange='this.form.submit()'


标签上的输入?
我试过重新排序代码,但没有任何运气。

编辑1这里有更多的代码,希望能让事情变得更清晰。

<link rel="stylesheet" type="text/css" href="style.css">
<link rel="stylesheet" type="text/css" href="audioplayer.css">
<link rel="stylesheet" type="text/css" href="../Game/src/CSS/Animation.css">
<script src="jquery.min.js"></script>
<script src="general.js"></script>
</head>
<body>
<?php include 'connectsql.php'; ?>

<div class = "container">
<?php # REFRESH VARIABLE SETUP
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    
# INPUT REFRESH (1, DOESN'T CHANGE ON APPEND)   
$inputnames = array("par"); 
foreach ($inputnames as $value) {
    $inputvar = $value;
    if (empty($_POST["$inputvar"]) or isset($_POST['save']) or isset($_POST['reset'])) {
        $$inputvar = "1";
    } else {
        $$inputvar = $_POST["$inputvar"];
    }
}
}
?>

<form method="post" id="form1"  action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
<div class = "header">
  Paragraph: <input  type="number" name="par" onchange='this.form.submit()' value="<?php 
  if (isset($_POST['append']) && $parbox != "") {
    echo $par + 1;
  }else{
     echo $par; 
  }
  ?>"> &emsp;
  
<?php
$sql = "SELECT * FROM p_intro1";
$row = $conn->query($sql)->fetch_assoc();
?>

Namebox: <input type="text" onchange='this.form.submit()' name="n" value="<?php echo $row["n"];?>" list="names">
</textarea>
<input type="submit" name="render" value="RENDER PARAGRAPH"> 

</form>
<?php # REFRESH VARIABLE SETUP
if ($_SERVER["REQUEST_METHOD"] == "POST") {

$inputnames = array("n"); 
foreach ($inputnames as $value) {
    $inputvar = $value;
    $var =  $_POST["$inputvar"];
        $conn->query("UPDATE p_intro1 SET $inputvar = '$var' WHERE par=$par");
}

}
?>
</div>
</body>
</html>


我在Xampp工作,所以connectsql.php只是:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "Game1";

// Create a connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check the connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>

6yt4nkrj

6yt4nkrj1#

在获取数据和窗体之前,需要使用更新查询代码。你可以试试这个

<html>
        <link rel="stylesheet" type="text/css" href="style.css">
        <link rel="stylesheet" type="text/css" href="audioplayer.css">
        <link rel="stylesheet" type="text/css" href="../Game/src/CSS/Animation.css">
        <script src="jquery.min.js"></script>
        <script src="general.js"></script>
        </head>
        <body>
        <?php include 'connectsql.php'; ?>
        
        <div class = "container">
          //Update Query Code Here.. 
          <?php # REFRESH VARIABLE SETUP
              if ($_SERVER["REQUEST_METHOD"] == "POST") {
                 $inputnames = array("n"); 
                 foreach ($inputnames as $value) {
                    $inputvar = $value;
                    $var =  $_POST["$inputvar"];
                    $conn->query("UPDATE p_intro1 SET $inputvar = '$var' WHERE par=$par");
                 }
        
              }
          ?>
        <?php # REFRESH VARIABLE SETUP
        if ($_SERVER["REQUEST_METHOD"] == "POST") {                
        # INPUT REFRESH (1, DOESN'T CHANGE ON APPEND)   
        $inputnames = array("par"); 
        foreach ($inputnames as $value) {
            $inputvar = $value;
            if (empty($_POST["$inputvar"]) or isset($_POST['save']) or isset($_POST['reset'])) {
                $$inputvar = "1";
            } else {
                $$inputvar = $_POST["$inputvar"];
            }
        }
        }
        ?>
        
        <form method="post" id="form1"  action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
        <div class = "header">
          Paragraph: <input  type="number" name="par" onchange='this.form.submit()' value="<?php 
          if (isset($_POST['append']) && $parbox != "") {
            echo $par + 1;
          }else{
             echo $par; 
          }
          ?>"> &emsp;
          
        <?php
        $sql = "SELECT * FROM p_intro1";
        $row = $conn->query($sql)->fetch_assoc();
        ?>
        
        Namebox: <input type="text" onchange='this.form.submit()' name="n" value="<?php echo $row["n"];?>" list="names">
        </textarea>
        <input type="submit" name="render" value="RENDER PARAGRAPH"> 
        
        </form>
        </div>
        </body>
        </html>

字符串

相关问题