简单数据库更新程序页

1cklez4t  于 2021-06-24  发布在  Mysql
关注(0)|答案(0)|浏览(241)

这个问题在这里已经有答案了

我可以在php中混合mysql api吗(4个答案)
引用-这个错误在php中是什么意思(36个答案)
mysqli_fetch_assoc()需要参数/调用成员函数bind_param()时出错。如何获得实际的mysql错误并修复它(1个答案)
两年前关门了。
大家早上好我已经做了这个简单的网页,应该更新我的数据库以下一些教程在互联网上,但仍然没有更新数据时,我按下提交按钮,有人可以帮助和纠正代码?我只需要从这个表中更新id,我认为这个代码可以帮助很多人在那里。。。我要把代码贴在下面。非常感谢。

<?php
    $servername = "localhost";
    $username = "user";
    $password = "pass";
    $dbname = "database";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 

    $id_cus = $_GET["id_cus"];

    $sql = "SELECT `id_customer`, `firstname`, `lastname`, `email` FROM `customer` WHERE `id_customer` = $id_cus";

    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {

            $id = $row["id_customer"];
            $nome = $row["firstname"];
            $cognome = $row["lastname"];
            $email = $row["email"];

        }
    } else {
        echo "0 results";
    }

    ?>

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <title>Bootstrap Example</title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
      <link rel="stylesheet" href="./panel.css">
      <script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    </head>
    <body>
      <div class="container">
        <div class="row">
          <div class="col-sm-12">
             <main>
        <form action="" method="post">
        <table class="table table-bordered table-condensed">
          <thead>
            <tr>
              <th>Codice cliente</th>
              <th class="col">Nome</th>
              <th class="col">Cognome</th>
              <th class="col">Indirizzo email</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td><input type="text" name="id" value="<?php echo $id ?>"/></td>
              <td><?php echo $nome; ?></td>
              <td><?php echo $cognome; ?></td>
              <td><?php echo $email; ?></td>
            </tr>
          </tbody>
        </table>
        <input type="submit" name="submit" value="Aggiorna" class="pull-right">
        </form>
      </main>   
          </div>
        </div>
      </div>
    </body>
    </html>
    <?php
    // check if the form has been submitted. If it has, process the form and save it to the database

    if (isset($_POST['submit']))

    {

      // confirm that the 'id' value is a valid integer before getting the form data

      if (is_numeric($_POST['id']))

      {

        // get form data, making sure it is valid

        $id = $_POST['id'];

          // check that firstname/lastname fields are both filled in

          if ($id == '')

          {

          // generate error message

          echo 'ERROR: Please fill in all required fields!';

          }

          else

          {

            // save the data to the database

        $conn->query("UPDATE customer SET id_customer='$id'")

            or die(mysql_error());

            // once saved, redirect back to the view page

            header("Location: panel.php?id_cus=$id");

          }
      }
    }
   $conn->close();
    ?>

编辑代码尝试另一个解决方案仍然不更新为什么?这应该很简单!

<?php
error_reporting(E_ALL);
ini_set('display_errors',1);

$databaseHost = 'localhost';
$databaseName = 'dbname';
$databaseUsername = 'dbuser';
$databasePassword = 'pass';

$mysqli = mysqli_connect($databaseHost, $databaseUsername, $databasePassword, $databaseName); 

if(isset($_POST['update']))
{    
    $id = $_POST['id'];

    $firstname=$_POST['firstname'];
    $lastname=$_POST['lastname'];
    $email=$_POST['email'];    

    // checking empty fields
    if(empty($fistname) || empty($lastname) || empty($email)) {            
        if(empty($firstname)) {
            echo "<font color='red'>Name field is empty.</font><br/>";
        }

        if(empty($lastname)) {
            echo "<font color='red'>lastname field is empty.</font><br/>";
        }

        if(empty($email)) {
            echo "<font color='red'>Email field is empty.</font><br/>";
        }        
    } else {    
        //updating the table
        $result = mysqli_query($mysqli, "UPDATE customer SET firstname='$firstname',lastname='$lastname',email='$email' WHERE id_customer=$id");

        //redirectig to the display page. In our case, it is index.php
        header("Location: index.php");
    }
}
?>
<?php

//getting id from url
$id = $_GET['id'];

//selecting data associated with this particular id
$result = mysqli_query($mysqli, "SELECT `id_customer`, `firstname`, `lastname`, `email` FROM `customer` WHERE `id_customer` = $id");

while($res = mysqli_fetch_array($result))
{
    $firstname = $res['firstname'];
    $lastname = $res['lastname'];
    $email = $res['email'];
    $id_customer = $res['id_customer'];
}
?>
<html>
<head>    
    <title>Edit Data</title>
</head>

<body>
    <a href="index.php">Home</a>
    <br/><br/>

    <form name="form1" method="post" action="panel.php">
        <table border="0">
            <tr> 
                <td>Name</td>
                <td><input type="text" name="firstname" value="<?php echo $firstname;?>"></td>
            </tr>
            <tr> 
                <td>Lastname</td>
                <td><input type="text" name="lastname" value="<?php echo $lastname;?>"></td>
            </tr>
            <tr> 
                <td>Email</td>
                <td><input type="text" name="email" value="<?php echo $email;?>"></td>
            </tr>
            <tr> 
                <td>ID</td>
                <td><input type="text" name="id_customer" value="<?php echo $id_customer;?>"></td>
            </tr>
            <tr>
                <td><input type="hidden" name="id" value=<?php echo $_GET['id'];?>></td>
                <td><input type="submit" name="update" value="Update"></td>
            </tr>
        </table>
    </form>
</body>
</html>
</html>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题