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

时间:2018-11-28 08:08:31

标签: php mysql sql record

我是php和mysql的新手。

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

我有一个工作的搜索页面和一个编辑页面,其中的链接中有$ _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']的位置,使之在查询的双引号内和之外,但注意工作。

1 个答案:

答案 0 :(得分:1)

在调用页面时,您需要将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'])){

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