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

ugmeyewa  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(230)

我不熟悉php和mysql。
我尝试从html表单更新数据库中的记录。
我有一个搜索页面和一个编辑页面,在链接中有一个$\u 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");
}
?>

我从错误消息中知道错误在我的查询代码中,但我不知道在哪里。我曾多次尝试在查询的双引号中更改&\u get['art\u id']位置,但都没有效果。

i5desfxk

i5desfxk1#

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

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

相关问题