更新记录时出错:您的 SQL 语法有误,我该如何解决?

Error updating record: You have an error in your SQL syntax how can i solve it?

我是 php 和 mysql 的新手。

我尝试从 html 表单更新数据库中的记录。

我有一个可用的搜索页面和一个编辑页面,在 link 中有一个 $_GET,其中是我必须在数据库中编辑的元素的 ID。

表格的所有工作都显示正确的数据,我可以在表格中编辑它们。但是当我单击编辑按钮时,我收到此错误消息:

Error updating record: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1

我的php编辑页面的代码是这样的:

<?php
require('includes\dbconn.php');
require('includes\dbfunction.php');
session_start();


if(isset($_SESSION["username"])){
  include('includes\theme\head.php');
  include('includes\theme\topbar.php');
  include('includes\theme\sidebar.php');
?>

<!-- !PAGE CONTENT! -->
<div class="w3-main" style="margin-left:300px;margin-top:43px;">

  <div class="w3-container">
    <div class="w3-row">
      <div class="w3-col s12 l3">
        <br>
        <?php
        //RICEVI DATI ARTICOLO DAL DATABASE
        if(isset($_GET['art_id'])){
          $sql = "SELECT * FROM articoli WHERE id =". $_GET['art_id'];
          $result = $conn->query($sql);
          $row = $result->fetch_assoc();
          }

        //AGGIORNA DATI ARTICOLO NEL DATABASE
        if(isset($_POST['btn-aggiorna'])){
          $codiceArt = $_POST['codicearticolo'];
          $descrizioneArt = $_POST['nomearticolo'];
          $barcodeArt = $_POST['barcode'];
          $prezzoAcquistoArt = $_POST['prezzoacquisto'];
          $prezzoVenditaArt = $_POST['prezzovendita'];
          $quantitaArt = $_POST['quantita'];
          $scontoArt = $_POST['sconto'];

          $update = "
UPDATE articoli 
   SET codiceArticolo='$codiceArt'
     , nomeArticolo='$descrizioneArt'
     , barcode='$barcodeArt'
     , prezzoAcquisto='$prezzoAcquistoArt'
     , prezzoVendita='$prezzoVenditaArt'
     , quantita='$quantitaArt'
     , scontoPercentuale='$scontoArt' 
 WHERE id =". $_GET['art_id'];
          if ($conn->query($update) === TRUE) {
            ?>
            <script type="text/javascript">
              window.location.href = 'articolomodificato.php';
            </script>
            <?php
          } else {
            echo "Error updating record: " . $conn->error;
          }


        }

         ?>
      </div>
      <div class="w3-col s12 l5">

         <form class="w3-container" action="modificaarticolo.php" method="post">
           <label class="w3-text-blue"><b>Codice articolo:</b></label>
           <input class="w3-input w3-border" name="codicearticolo" type="text" value="<?php echo $row['codiceArticolo'];?>"><br>
           <label class="w3-text-blue"><b>Descrizione articolo:</b></label>
           <input class="w3-input w3-border" name="nomearticolo" type="text" value="<?php echo $row['nomeArticolo']; ?>"><br>
           <label class="w3-text-blue"><b>Barcode:</b></label>
           <input class="w3-input w3-border" name="barcode" type="text" value="<?php echo $row['barcode']; ?>"><br>
           <label class="w3-text-blue"><b>Prezzo Acquisto:</b></label>
           <input class="w3-input w3-border" name="prezzoacquisto" type="text" value="<?php echo $row['prezzoAcquisto']; ?>"><br>
           <label class="w3-text-blue"><b>Prezzo Vendita:</b></label>
           <input class="w3-input w3-border" name="prezzovendita" type="text" value="<?php echo $row['prezzoVendita']; ?>"><br>
           <label class="w3-text-blue"><b>Quantità:</b></label>
           <input class="w3-input w3-border" name="quantita" type="text" value="<?php echo $row['quantita']; ?>"><br>
           <label class="w3-text-blue"><b>Sconto %:</b></label>
           <input class="w3-input w3-border" name="sconto" type="text" value="<?php echo $row['scontoPercentuale']; ?>"><br>
           <button class="w3-btn w3-blue" name="btn-aggiorna" type="submit">Modifica</button>
           <a class="w3-btn w3-blue" href="/bk2/magazzino.php">Indietro<a>
         </form>

      </div>
      <div class="w3-col s12 l3">
        <br>
      </div>
    </div>




  </div>
</div>











<?php
  include('includes\theme\script.php');
  ?>

</body>
</html>
<?php
}
else{
  header("location: accessonegato.php");
}
?>

我从错误消息中知道错误在我的查询代码中,但我不明白在哪里。我曾多次尝试更改查询双引号内外的 &_GET['art_id'] 位置,但没有注意到工作。

调用页面时需要在表单中添加ID,否则传不进去

<form class="w3-container" action="modificaarticolo.php?art_id=<?php echo $row['id'];?>" method="post">

您可能需要更改 $row['id'] 以获得正确的列名称。

虽然这会导致一个小问题,因为数据是在

中获取的
//RICEVI DATI ARTICOLO DAL DATABASE
if(isset($_GET['art_id'])){

这是更新之前的代码,因此您可能需要将此代码移到更新下方以确保它检索到最新数据。