donglengyuan6826 2018-02-18 06:53
浏览 15

值未正确发布到数据库[重复]

This question already has an answer here:

I have a simple issue that I can't solve. I've tried googling and looking on SO, but found nothing that helps.

Basically, I have a PHP page that has inputs, and the inputs are POSTed to another PHP file that in turn queries a few MySQL statements and updates the database with the information in the first PHP page.

This is my PHP with the form:

<!DOCTYPE html lang="en">
<head>
<meta charset="utf-8">
<title>form page</title>
</head>

<body>

<form action="mainfileupdatetest.php" method="POST">
    <p>
        <label for="test">A:</label>
        <input type="text" name="test" id="test">
        <label for="test2">B:</label>
        <input type="text" name="test2" id="test2">
        <label for="test3">A:</label>
        <input type="text" name="test3" id="test3">
        <label for="test4">B:</label>
        <input type="text" name="test4" id="test4">
    </p>
    <button type="submit" value="submit">
</form>

<?php
$mysqli->close();
?>

</body>
</html>

And this is the PHP file that updates the database:

<?php
$link = mysqli_connect("localhost", "root", "fakepassword", "fakedb");

if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

$test = mysqli_real_escape_string($link, $_REQUEST['test']);
$test2 = mysqli_real_escape_string($link, $_REQUEST['test2']);
$test3 = mysqli_real_escape_string($link, $_REQUEST['test3']);
$test4 = mysqli_real_escape_string($link, $_REQUEST['test4']);

$sql = "UPDATE mainfile SET A='$test', B='$test2' WHERE id='1'";
$sql = "UPDATE mainfile SET A='$test3', B='$test4' WHERE id='2'";
if(mysqli_query($link, $sql)){
    echo "Records added successfully.";
} else{
    echo "ERROR: Could not execute $sql. " . mysqli_error($link);
}

mysqli_close($link);
?>

There are no PHP or MySQL errors that pop up, but when I input information into the form, only certain ones get saved into the database by MySQL.

This is the table as seen on PHPMyAdmin:

enter image description here

And when I input information like this: enter image description here

(side question, why doesn't the submit button show the name I gave it? [value:submit])

I get no error: enter image description here

But on the database:

enter image description here

That happens. Anyone able to enlighten me?

</div>
  • 写回答

3条回答 默认 最新

  • douneiben2240 2018-02-18 07:15
    关注

    As you use the same variable $sql, like :

    1) $sql = "UPDATE mainfile SET A='$test', B='$test2' WHERE id='1'";

    2) $sql = "UPDATE mainfile SET A='$test3', B='$test4' WHERE id='2'";

    and execute $sql variable on mysqli_query() like:

    if(mysqli_query($link, $sql)){ echo "Records added successfully."; } 
    else{ echo "ERROR: Could not execute $sql. " . mysqli_error($link); }
    

    so second udate query will work.

    2) $sql = "UPDATE mainfile SET A='$test3', B='$test4' WHERE id='2'";

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀