如何在一个查询中创建两个更新语句?

时间:2017-03-16 10:54:55

标签: php forms mysqli phpmyadmin

我正在创建一个表单,您可以从phpmyadmin插入,更新和删除数据库welkom中的数据。这是我的档案Formmenu.php。 PS:此代码包含荷兰语。

PHP

<?php
    if ( ! empty($_POST))
     {
            $mysqli = new mysqli('localhost','root','','welkom');

            if ($mysqli ->connect_error) 
            {
                die('connect error: '. $mysqli->connect_errno . ': ' . $mysqli->connect_error);
            }

                if (isset($_POST['insert'])) {
                     $sql = "INSERT INTO abdijbieren (naam, prijs) VALUES ('{$mysqli->real_escape_string($_POST['naam'])}','{$mysqli->real_escape_string($_POST['prijs'])}');";

                } else if (isset($_POST['delete'])) {
                    $sql = "DELETE FROM abdijbieren WHERE naam ='{$mysqli->real_escape_string($_POST['naam'])}';";
                }
                  else if (isset($_POST['update'])) {
                    $sql = "UPDATE abdijbieren SET id='{$mysqli->real_escape_string($_POST['id'])}' WHERE naam='{$mysqli->real_escape_string($_POST['naam'])}'; UPDATE abdijbieren SET prijs='{$mysqli->real_escape_string($_POST['prijs'])}' WHERE naam='{$mysqli->real_escape_string($_POST['naam'])}';";
                }

                  else {
                      /*nothing*/
                  }



           $insert = $mysqli->query($sql);

            if ($insert)
            {
                echo "Success! Keer terug naar de volgende pagina om te updaten.";
            }else
            {
                die("Error: {$mysqli->errno} : {$mysqli->error}");
            }



            $mysqli->close();
        }

    ?>   

表格

<form method="post" action="">
              <input name="naam" type="text" placeholder="naam drank" required><br>
              <input name="prijs" type="text" placeholder="prijs" required><br>
              <input name="id" type="text" placeholder="tracking number*"><br><br>

        <input type="submit" name="insert" value="insert">
        <input type="submit" name="delete" value="delete">
        <input type="submit" name="update" value="update"><br>
        *= niet nodig bij "insert".
        </form>

这是我关注的重点:

$sql = "UPDATE abdijbieren SET id='{$mysqli->real_escape_string($_POST['id'])}' WHERE naam='{$mysqli->real_escape_string($_POST['naam'])}'; UPDATE abdijbieren SET prijs='{$mysqli->real_escape_string($_POST['prijs'])}' WHERE naam='{$mysqli->real_escape_string($_POST['naam'])}';";

当我尝试代码时,我在提交表单后收到错误#1064。错误回响;

  

错误:1064:您的SQL语法出错;检查手册   对应于您的MariaDB服务器版本以获得正确的语法   使用附近&#39;更新abdijbieren SET prijs =&#39; 5.00 EUR&#39; WHERE id =&#39; 9&#39 ;;   更新一个&#39;在第2行

我不确定它是拼写错误还是real_escape_string。我试着寻找一个解决方案:

第二个链接的问题仍然没有解决。我检查了我的代码,看起来很好,但收到错误#1064后,我对代码感到困惑。我需要帮助解决这个问题。

谢谢。

3 个答案:

答案 0 :(得分:2)

您可以在一个语句中更新多个列:

$sql = "UPDATE abdijbieren 
           SET id = '" . $mysqli->real_escape_string($_POST['id']) . "',
               prijs = '" . $mysqli->real_escape_string($_POST['prijs']) . "' 
         WHERE naam='" . $mysqli->real_escape_string($_POST['naam']) . "'"

答案 1 :(得分:0)

试试这个:

 $sql = "UPDATE abdijbieren SET id='{$mysqli->real_escape_string($_POST['id'])}' , prijs='{$mysqli->real_escape_string($_POST['prijs'])}' WHERE naam='{$mysqli->real_escape_string($_POST['naam'])}'";

答案 2 :(得分:-2)

无需为每列执行更新查询;你可以通过使用这样的查询来实现这一点:

'update table_name set col_1="col_1_value", col_2 ="col_2_value" where your_condition = abc';