Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从PHP更新时遇到的问题_Php_Html - Fatal编程技术网

从PHP更新时遇到的问题

从PHP更新时遇到的问题,php,html,Php,Html,我有一个表单,我想让记录更改更新我的SQL数据库表 在我的index.php文件中,我有一个f.ex: <?php $servername = "localhost"; $username = "root"; $password = "password"; $dbname = "myDB"; $tbname = "myVis"; // Create connection $conn = mysqli_connect($servername, $username, $password, $d

我有一个表单,我想让记录更改更新我的SQL数据库表

在我的index.php文件中,我有一个f.ex:

<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "myDB";
$tbname = "myVis";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT * FROM " . $tbname . " WHERE id = '$_POST[id]'";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {

    while($row = mysqli_fetch_assoc($result)) {
      $id = $row['id'];
      $firstname = $row['firstname'];
      $lastname = $row['lastname'];
      $image = $row['image'];
      $course = $row['course'];
      $frdate = $row['frdate'];
      $todate = $row['todate'];
      $email = $row['email'];
      $checkout = $row['checkout'];
    }
  }
  mysqli_close($conn);
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
...
  </head>
  <body>

    <form action='' method='post'>
    <table>
...
        <tr>
          <td>Efternamn:</td>
           <td>
            <input id="lastname2" type="text" value='<?php echo  $lastname; ?>' /></td>
        </tr>
...
      </table>

      <?php
           if ($_SERVER["REQUEST_METHOD"] == "POST") {
          if (isset($_POST['submit2'])) {
...
            if (isset($_POST['lastname2'])) {
              $lastname  = $_POST['lastname2'];
            }
...
            $servername = "localhost";
            $username = "root";
            $password = "password";
            $dbname = "myDB";
            $tbname = "myVis";
            // Create connection
            $conn = mysqli_connect($servername, $username, $password, $dbname);
            // Check connection
            if (!$conn) {
              die("Connection failed: " . mysqli_connect_error());
            }
            $sql2 = "UPDATE " . $tbname . " SET firstname='$firstname', lastname='$lastname', image='$image', course='$course', frdate='$frdate', todate='$todate', email='$email', checkout='$checkout' WHERE id ='".$_POST['id']."'";
            $result = mysqli_query($conn, $sql2);
            if ($result) {
               echo "Record updated successfully";
            } else {
               echo "Error updating record: " . mysqli_error($conn);
            }

            mysqli_close($conn);
          }
        }
      ?>

      <input type="submit" name="submit2" id="submit2" value="Spara" />
      <input type="button" name="back" id="back" value="Tillbaka" onclick="history.back()" />

    </form>
  </body>
</html>

...
...
Efternamn:

在数据库连接后,将提交代码放在页面顶部。然后在提交表单时,首先将表列与提交的post值进行匹配。如果任何列具有不同的post值,则更新该列。

您的html看起来无效,为什么每次都要创建一个新的数据库连接?这些问题在这里是离题的。我们这里不做代码审查。特别是如果大部分代码都是从示例中剥离出来的,那么我从另一个页面进入这个页面,在那里我发布了一个提交id的页面,这就是为什么我选择在顶部。