Php 使用MySQLi从浏览器到服务器上的数据库编辑、添加、删除记录

Php 使用MySQLi从浏览器到服务器上的数据库编辑、添加、删除记录,php,mysql,mysqli,Php,Mysql,Mysqli,我修改了本教程,以打印5个主列:ID、名字、姓氏、职位和衬衫,以及其他2个用于编辑和删除按钮的列。编辑代码后,表格将打开,但当我编辑或记录新条目时,我的新条目不会保存到数据库中。这是代码,请看一下,帮我找出错误。代码的格式不正确,所以我把它放在github上,并试图让它可读 编辑: 为什么在试图通过浏览器编辑或添加新记录时,数据不会保存到MySQL数据库中,以便在刷新后显示 <?php /* Allows the user to both create new records and ed

我修改了本教程,以打印5个主列:ID、名字、姓氏、职位和衬衫,以及其他2个用于编辑和删除按钮的列。编辑代码后,表格将打开,但当我编辑或记录新条目时,我的新条目不会保存到数据库中。这是代码,请看一下,帮我找出错误。代码的格式不正确,所以我把它放在github上,并试图让它可读

编辑:

为什么在试图通过浏览器编辑或添加新记录时,数据不会保存到MySQL数据库中,以便在刷新后显示

<?php
/*
Allows the user to both create new records and edit existing records
*/
// connect to the database
include("connect-db.php");
// creates the new/edit record form
// since this form is used multiple times in this file, I have made it a function that is easily reusable
function renderForm($first = '', $last ='', $pos = '', $shirt = '', $error = '', $id = '')
{ ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
      <head>
        <title>
          <?php if ($id != '') { echo "Edit Record"; } else { echo "New Record"; } ?>
        </title>
          <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
      </head>
          <body>
             <h1><?php if ($id != '') { echo "Edit Record"; } else { echo "New Record"; } ?></h1>
             <?php if ($error != '') {
              echo "<div style='padding:4px; border:1px solid red; color:red'>" . $error
              . "</div>";
 } ?>

<form action="" method="post">
<div>
<?php if ($id != '') { ?>
<input type="hidden" name="id" value="<?php echo $id; ?>" />
<p>ID: <?php echo $id; ?></p>
<?php 

} ?>

<strong>First Name: *</strong> <input type="text" name="firstname"
value="<?php echo $first; ?>"/><br/>

<strong>Last Name:   *</strong> <input type="text" name="lastname"
value="<?php echo $last; ?>"/><br/>

<strong>Position: </strong> <input type="text" name="position"
value="<?php echo $pos; ?>"/><br/r>

<strong>Shirt #:  </strong> <input type="number" name="shirt"
value="<?php echo $shirt; ?>"/>
<p>* required</p>

<input type="submit" name="submit" value="Submit" />

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

<?php }
/*
EDIT RECORD
*/
// if the 'id' variable is set in the URL, we know that we need to edit a record
if (isset($_GET['id']))
{
    // if the form's submit button is clicked, we need to process the form
    if (isset($_POST['submit']))
{
  // make sure the 'id' in the URL is valid
if (is_numeric($_POST['id']))
{
    // get variables from the URL/form
    $id = $_POST['id'];
    $firstname = htmlentities($_POST['firstname'], ENT_QUOTES);
    $lastname = htmlentities($_POST['lastname'], ENT_QUOTES);
    $position = htmlentities($_POST['position'], ENT_QUOTES);
    $shirt = htmlentities($_POST['shirt'], ENT_QUOTES);
// check that firstname and lastname are both not empty
if ($firstname == '' || $lastname == '')
{
    // if they are empty, show an error message and display the form
    $error = 'ERROR: Please fill in all required fields!';
    renderForm($firstname, $lastname, $pos, $shirt, $error, $id);
}
else
{
    // if everything is fine, update the record in the database
    if ($stmt = $mysqli->prepare("UPDATE players SET firstname = ?, lastname = ?,
    position = ?, shirt = ?
    WHERE id=?"))
{
$stmt->bind_param("ssi", $firstname, $lastname, $position, $shirt, $id);
$stmt->execute();
$stmt->close();
}
// show an error message if the query has an error
else
{
    echo "ERROR: could not prepare SQL statement.";
}
// redirect the user once the form is updated
    header("Location: view.php");
}
}
// if the 'id' variable is not valid, show an error message
else
{
        echo "Error!";
}
}
// if the form hasn't been submitted yet, get the info from the database and show the form
else
{
      // make sure the 'id' value is valid
      if (is_numeric($_GET['id']) && $_GET['id'] > 0)
{ 
      // get 'id' from URL
      $id = $_GET['id'];
      // get the recod from the database
if($stmt = $mysqli->prepare("SELECT * FROM players WHERE id=?"))
{
          $stmt->bind_param("i", $id);
          $stmt->execute();
          $stmt->bind_result($id, $firstname, $lastname, $position, $shirt);
          $stmt->fetch();
                // show the form
        renderForm($firstname, $lastname, $position, $shirt, NULL, $id);
        $stmt->close();
}
            // show an error if the query has an error
else
{
          echo "Error: could not prepare SQL statement";
}
}
          // if the 'id' value is not valid, redirect the user back to the view.php page
else
{
          header("Location: view.php");
}
}
}
/*
NEW RECORD
*/
// if the 'id' variable is not set in the URL, we must be creating a new record
else
{
          // if the form's submit button is clicked, we need to process the form
if (isset($_POST['submit']))
{
          // get the form data
        $firstname = htmlentities($_POST['firstname'], ENT_QUOTES);
        $lastname = htmlentities($_POST['lastname'], ENT_QUOTES);
        $position = htmlentities($_POST['position'], ENT_QUOTES);
        $shirt = htmlentities($_POST['shirt'], ENT_QUOTES);
// check that firstname and lastname are both not empty
if ($firstname == '' || $lastname == '')
{
        // if they are empty, show an error message and display the form
        $error = 'ERROR: Please fill in all required fields!';
        renderForm($firstname, $lastname, $pos, $shirt, $error);
}
else
{
        // insert the new record into the database
if ($stmt = $mysqli->prepare("INSERT players (firstname, lastname, position, shirt) VALUES (?, ?, ?, ?)"))
{
        $stmt->bind_param("ss", $firstname, $lastname, $position, $shirt);
        $stmt->execute();
        $stmt->close();
}
// show an error if the query has an error
else
{
        echo "ERROR: Could not prepare SQL statement.";
}
// redirec the user
        header("Location: view.php");
}
}
        // if the form hasn't been submitted yet, show the form
else
{
        renderForm();
}
}
// close the mysqli connection
          $mysqli->close();
?>

再次仔细查看代码后,我改变了主意

else
{
              // if everything is fine, update the record in the database
    if ($stmt = $mysqli->prepare("UPDATE players SET firstname = ?, lastname = ?,
    position = ?, shirt = ?
    WHERE id=?"))
{
$stmt->bind_param("ssi", $firstname, $lastname, $position, $shirt, $id);
进入

然后

进入

现在,它正在按预期工作。
这是我第一次使用php和MySQL,如果这些不应该被称为占位符,请纠正我。

您应该在问题中添加源代码,而不是在某一天被删除的链接后面。你还没有问过一个问题,你应该问。另外,请阅读为什么你不应该问一些普通的问题,比如“帮帮我,怎么了,这太宽泛了”。
else
{
          // if everything is fine, update the record in the database
    if ($stmt = $mysqli->prepare("UPDATE players SET firstname = ?, lastname = ?,
    position = ?, shirt = ?
    WHERE id=?"))
{
            /// added two more s here
$stmt->bind_param("ssssi", $firstname, $lastname, $position, $shirt, $id);
// insert the new record into the database
if ($stmt = $mysqli->prepare("INSERT players (firstname, lastname, position, shirt) VALUES (?, ?, ?, ?)"))
{
        $stmt->bind_param("ss", $firstname, $lastname, $position, $shirt);
        $stmt->execute();
        $stmt->close();
}
// insert the new record into the database
if ($stmt = $mysqli->prepare("INSERT players (firstname, lastname, position, shirt) VALUES (?, ?, ?, ?)"))
{
                            /// here, with the place holders 's'
        $stmt->bind_param("ssss", $firstname, $lastname, $position, $shirt);
        $stmt->execute();
        $stmt->close();
}