通过更新单个单元来更新 mysql 数据库

问题描述 投票:0回答:1

嗨,我正在本地开发一个项目,并从这里开始 https://codepen.io/fjfalah/pen/WEppVa 创建链接到我的 mysql 数据库的表。我在显示数据时没有遇到问题,但在更新数据时遇到问题。编辑和更新表后,我会显示消息“记录已成功更新”,但它实际上并没有更新任何内容,并且控制台和日志中都没有任何错误消息。我试图只更新两列,但实际上所有列都必须修改。我将与您分享代码,感谢您的任何建议。

<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Admin</title>
        <!-- Bootstrap 5 CSS -->
        <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css">
        <link rel="stylesheet" href="style/style.css">
        
        <script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
        <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script> 
        
        <script src="https://markcell.github.io/jquery-tabledit/assets/js/tabledit.min.js"></script>
    </head>
    <body>
        <div class="main">
            <div class="title-container">
                <h1>Aggiornamento Prezzi</h1>
                <h2> <?= $user_name ?></h2>
            </div>
            <div class="users-container">
                <h2></h2>
                <div class="table-responsive">
                    <div class="position-relative content">
                        <div class="position-absolute w-100 row" style="z-index: 50;">
                            <div class="col-6">
                                <div class="filter-wrapper">
                                    <input type="radio" class="filter-checkbox" name="filter" data-filter="" value="Tutto" checked="checked" />
                                    Tutto
                                    <input type="radio" class="filter-checkbox" name="filter" data-filter="Antintrusione" value="Antintrusione" />
                                    Antintrusione
                                    <input type="radio" class="filter-checkbox" name="filter" data-filter="Antincendio" value="Antincendio" />
                                    Antincendio
                                </div>
                            </div>
                        </div>
                    </div>
                    <br>
                    <table id="sample_data" class="table table-hover">
                        <thead>
                            <tr>
                                <th scope="col"> Prodotti ID</th>
                                <th scope="col">Nome Prodotto</th>
                                <th scope="col">Gamma</th>
                                <th scope="col">Alfanumerico</th>
                                <th scope="col">Descrizione</th>
                                <th scope="col">Assorbimento</th>
                                <th scope="col">Icona 1</th>
                                <th scope="col">Icona 2</th>
                                <th scope="col">Icona 3</th>
                                <th scope="col">Icona 4</th>
                                <th scope="col">Icona 5</th>
                                <th scope="col">Immagine</th>
                                <th scope="col">Garanzia</th>
                                <th scope="col">Riparazione</th>
                                <th scope="col">Costo Acquisto</th>
                                <th scope="col">Prezzo Vendita</th>
                                <th scope="col">Stato</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php 
                            $var = @$_POST['ProdottiID'] ;
                            $stmt = $conn->prepare("SELECT * FROM `prodotti`");
                            $stmt->execute();
                            $result = $stmt->fetchAll();

                            foreach ($result as $row) {
                                $ProdottiID = $row['ProdottiID'];
                                $NomeProdotto = $row['NomeProdotto'];
                                $Gamma = $row['Gamma'];
                                $Alfanumerico = $row['Alfanumerico'];
                                $Descrizione = $row['Descrizione'];
                                $Assorbimento = $row['Assorbimento'];
                                $Icona1 = $row['Icona1'];
                                $Icona2 = $row['Icona2'];
                                $Icona3 = $row['Icona3'];
                                $Icona4 = $row['Icona4'];
                                $Icona5 = $row['Icona5'];
                                $Immagine = $row['Immagine'];
                                $Garanzia = $row['Garanzia'];
                                $Riparazione = $row['Riparazione'];
                                $CostoAcquisto = $row['CostoAcquisto'];
                                $PrezzoVendita = $row['PrezzoVendita'];
                                $Stato = $row['Stato'];
                            ?>
                          <tr>
                            <td id="ProdottiID" scope="col"><span class="value"><?= $ProdottiID ?></span><div></div></td>
                            <td id="NomeProdotto" scope="col"><span class="value"><?= $NomeProdotto ?></span><div></div></td>
                            <td scope="col"><span class="value"><?= $Gamma ?></span><div></div></td>
                            <td id="Alfanumerico" scope="col"><span class="value"><?= $Alfanumerico ?></span><div></div></td>
                            <td scope="col"><span class="value"><?= $Descrizione ?></span><div></div></td>
                            <td scope="col"><span class="value"><?= $Assorbimento ?></span><div></div></td>
                            <td scope="col"><span class="value"><?= $Icona1 ?></span><div></div></td>
                            <td scope="col"><span class="value"><?= $Icona2 ?></span><div></div></td>
                            <td scope="col"><span class="value"><?= $Icona3 ?></span><div></div></td>
                            <td scope="col"><span class="value"><?= $Icona4 ?></span><div></div></td>
                            <td scope="col"><span class="value"><?= $Icona5 ?></span><div></div></td>
                            <td scope="col"><span class="value"><?= $Immagine ?></span><div></div></td>
                            <td scope="col"><span class="value"><?= $Garanzia ?></span><div></div></td>
                            <td scope="col"><span class="value"><?= $Riparazione ?></span><div></div></td>
                            <td scope="col"><span class="value"><?= $CostoAcquisto ?></span><div></div></td>
                            <td scope="col"><span class="value"><?= $PrezzoVendita ?></span><div></div></td>
                            <td scope="col"><span class="value"><?= $Stato ?></span><div></div></td>
                        </tr>
                                                      <?php
                            }
                            ?>
                      </tbody>
                    </table>
                </div>
            </div>
        </div>
      <script>
        $('table tbody').on('dblclick', 'td', function(){
    var valCell = $(this).find('span').text();
    
    $(this).find('span').hide();
    $(this).find('div').show().html('\
      <div class="input-group">\
        <input type="text" class="form-control" value="'+valCell+'">\
        <span class="input-group-btn">\
          <button id="save" class="btn btn-default" type="button"><i class="bi bi-check2"></i></button>\
          <button id="close" class="btn btn-default" type="button"><i class="bi bi-x-circle"></i></button>\
        </span>\
      </div>');
    $(this).find('input').focus();
    $(this).closest('tbody').find('td').not($(this)).each(function(){
      $(this).find('span').show();
      $(this).find('div').hide();
    });
  }); 
  
  $('table').on('click','#close',function(){
    $(this).closest('td').find('span.value').show();
    $(this).closest('div').hide();
  });
    $('table').on('click','#close',function(){
    $(this).closest('td').find('span.value').show();
    $(this).closest('div').hide();
  });
  $('table').on('click','#save', function(){
     var getValueInput = $(this).closest('div').find('input').val();
    $(this).closest('td').find('span.value').text(getValueInput);
    
    $(this).closest('td').find('span.value').show();
    $(this).closest('div').hide();

    jQuery.ajax({
        url: "conn/action2.php",
        method: "POST",
        data: {ProdottiID: $('#ProdottiID').val(), NomeProdotto: $('#NomeProdotto').val(), Alfanumerico: $('#Alfanumerico').val()},
        cache: false,
        success: function(response)
        {
          alert("Record successfully updated");
        }
    });
  });
      </script>
    </body>
</html>

这是更新数据库的php文件

<?php

//action2.php


$NomeProdotto = $_POST['NomeProdotto']; 
$Alfanumerico = $_POST['Alfanumerico'];
$ProdottiID = $_POST['ProdottiID'];

//You need create a database connection
$dbhost = 'localhost';
$dbuser = '11111';
$dbpass = '22222';

$conn = mysqli_connect($dbhost, $dbuser, $dbpass, "lslistini2");

if(! $conn ){
   die('Could not connect: ' . mysqli_error());
}

$retval = mysqli_query($conn,"UPDATE prodotti SET ProdottiID = '$ProdottiID', NomeProdotto = '$NomeProdotto', Alfanumerico = '$Alfanumerico' WHERE ProdottiID = '$ProdottiID'");

if(! $retval ) {
   die('Could not update data: ' . mysqli_error());
}
echo "Updated data successfully\n";
mysqli_close($conn);


?>

再次感谢您的帮助

php jquery mysql
1个回答
0
投票

我在前端和后端的注释中插入了解释

ps。考虑使用参数查询,因为action2.php充满漏洞

<html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>Admin</title>
            <!-- Bootstrap 5 CSS -->
            <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
            <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css">
            <link rel="stylesheet" href="style/style.css">
            
            <script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
            <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script> 
            
            <script src="https://markcell.github.io/jquery-tabledit/assets/js/tabledit.min.js"></script>
        </head>
        <body>
            <div class="main">
                <div class="title-container">
                    <h1>Aggiornamento Prezzi</h1>
                    <h2> <?= $user_name ?></h2>
                </div>
                <div class="users-container">
                    <h2></h2>
                    <div class="table-responsive">
                        <div class="position-relative content">
                            <div class="position-absolute w-100 row" style="z-index: 50;">
                                <div class="col-6">
                                    <div class="filter-wrapper">
                                        <input type="radio" class="filter-checkbox" name="filter" data-filter="" value="Tutto" checked="checked" />
                                        Tutto
                                        <input type="radio" class="filter-checkbox" name="filter" data-filter="Antintrusione" value="Antintrusione" />
                                        Antintrusione
                                        <input type="radio" class="filter-checkbox" name="filter" data-filter="Antincendio" value="Antincendio" />
                                        Antincendio
                                    </div>
                                </div>
                            </div>
                        </div>
                        <br>
                        <table id="sample_data" class="table table-hover">
                            <thead>
                                <tr>
                                    <th scope="col"> Prodotti ID</th>
                                    <th scope="col">Nome Prodotto</th>
                                    <th scope="col">Gamma</th>
                                    <th scope="col">Alfanumerico</th>
                                    <th scope="col">Descrizione</th>
                                    <th scope="col">Assorbimento</th>
                                    <th scope="col">Icona 1</th>
                                    <th scope="col">Icona 2</th>
                                    <th scope="col">Icona 3</th>
                                    <th scope="col">Icona 4</th>
                                    <th scope="col">Icona 5</th>
                                    <th scope="col">Immagine</th>
                                    <th scope="col">Garanzia</th>
                                    <th scope="col">Riparazione</th>
                                    <th scope="col">Costo Acquisto</th>
                                    <th scope="col">Prezzo Vendita</th>
                                    <th scope="col">Stato</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php 
                                
                                $var = @$_POST['ProdottiID'] ;
                                $stmt = $conn->prepare("SELECT * FROM `prodotti`");
                                $stmt->execute();
                                $result = $stmt->fetchAll();
                                
                                foreach ($result as $row) {
                                    $ProdottiID = $row['ProdottiID'];
                                    $NomeProdotto = $row['NomeProdotto'];
                                    $Gamma = $row['Gamma'];
                                    $Alfanumerico = $row['Alfanumerico'];
                                    $Descrizione = $row['Descrizione'];
                                    $Assorbimento = $row['Assorbimento'];
                                    $Icona1 = $row['Icona1'];
                                    $Icona2 = $row['Icona2'];
                                    $Icona3 = $row['Icona3'];
                                    $Icona4 = $row['Icona4'];
                                    $Icona5 = $row['Icona5'];
                                    $Immagine = $row['Immagine'];
                                    $Garanzia = $row['Garanzia'];
                                    $Riparazione = $row['Riparazione'];
                                    $CostoAcquisto = $row['CostoAcquisto'];
                                    $PrezzoVendita = $row['PrezzoVendita'];
                                    $Stato = $row['Stato'];
                                ?>
                                
                              <tr>
                                <?php 
                                foreach($row as $k=>$v){ 
                                  /* 
                                  put your data as attribute on td so you can retrieve them inside your dbl click event 
                                  
                                  i've used:
                                  data-row-id => the id to use in your "where condition" 
                                  data-name   => the field name to edit
                                  data-value  => the value to set
                                  */
                                ?>
                                  <td data-row-id='<?= $ProdottiID ?>' data-name='<?= $k ?>' data-value='<?= $v ?>' scope="col"><span class="value"><?= $v ?></span><div></div></td>
                                <?php } ?>
                              </tr>
                              <?php } ?>
                          </tbody>
                        </table>
                    </div>
                </div>
            </div>
          <script>
            $('table tbody').on('dblclick', 'td', function(){
            var valCell = $(this).find('span').text();
            
            $(this).find('span').hide();
            $(this).find('div').show().html('\
              <div class="input-group">\
                <input type="text" class="form-control" value="'+valCell+'">\
                <span class="input-group-btn">\
                  <button id="save" class="btn btn-default" type="button"><i class="bi bi-check2"></i></button>\
                  <button id="close" class="btn btn-default" type="button"><i class="bi bi-x-circle"></i></button>\
                </span>\
              </div>');
            $(this).find('input').focus();
            $(this).closest('tbody').find('td').not($(this)).each(function(){
              $(this).find('span').show();
              $(this).find('div').hide();
            });
          }); 
          
          $('table').on('click','#close',function(){
            $(this).closest('td').find('span.value').show();
            $(this).closest('div').hide();
          });
            $('table').on('click','#close',function(){
            $(this).closest('td').find('span.value').show();
            $(this).closest('div').hide();
          });
          $('table').on('click','#save', function(){
            //WARNING WARNING WARNING WARNING WARNING WARNING WARNING 
            // theoretically it is possible to also change the value of the id, but I don't think it is wise
            //WARNING WARNING WARNING WARNING WARNING WARNING WARNING 
            var getValueInput = $(this).closest('div').find('input').val();
            const hero=$(this).closest('td');
            hero.find('span.value').text(getValueInput);
            hero.find('span.value').show();
            $(this).closest('div').hide();
            
            
            
       
            jQuery.ajax({
                url: "conn/action2.php",
                //url:"test_res.php",
                method: "POST",
                //here you retrive what you need from attributes if you need more info you can pass them in the same way
                data: {ProdottiID: hero.attr("data-row-id"), NomeCampo: hero.attr("data-name"), ValoreCampo: getValueInput, VecchioValoreCampo: hero.attr("data-value")},
                cache: false,
                success: function(response)
                {
                  console.dir(response);
                  alert("Record successfully updated");
                }
            });
            
          });
          </script>
        </body>
    </html>

action2.php:

<?php
//action2.php

//since on your frontend  you are sending data "on col change" you can edit  a single field at time
//if you want update the full row you need to rethink the frontend for edit the whole row before you send ajax data



$NomeCampo = $_POST['NomeCampo'] ?? ""; 
$ValoreCampo = $_POST['ValoreCampo'] ?? "";
$ProdottiID = $_POST['ProdottiID'] ?? "";

if(!is_numeric($ProdottiID)){
  die("something wrong with ID");
}

if(!preg_match("/[a-z_0-9]+/i", $NomeCampo)){
  die("something wrong with NomeCampo");
}

//die(json_encode($_POST));


//You need create a database connection
$dbhost = 'localhost';
$dbuser = '11111';
$dbpass = '22222';

$conn = mysqli_connect($dbhost, $dbuser, $dbpass, "lslistini2");

if(! $conn ){
   die('Could not connect: ' . mysqli_error());
}

//DANGER DANGER DANGER DANGER DANGER DANGERDANGER DANGER DANGER
//DANGER DANGER DANGER DANGER DANGER DANGERDANGER DANGER DANGER

//careful here because i see injection everywhere===========================================================================================
$retval = mysqli_query($conn,"UPDATE prodotti SET ProdottiID = '$ProdottiID', $NomeCampo = '$ValoreCampo' WHERE ProdottiID = '$ProdottiID'");
//careful here because i see injection everywhere===========================================================================================

//DANGER DANGER DANGER DANGER DANGER DANGERDANGER DANGER DANGER
if(! $retval ) {
   die('Could not update data: ' . mysqli_error());
}
echo "Updated data successfully\n";
mysqli_close($conn);


?>
© www.soinside.com 2019 - 2024. All rights reserved.