phpmyadmin CRUD函数

时间:2019-03-21 13:47:08

标签: php database phpmyadmin crud

我必须在phpmyadmin上创建一个表,然后使用php来实现CRUD功能,我已经将每个功能作为单独的功能来完成,但是由于某些原因,它不会将我添加到表单中的数据添加到桌子。我认为这是唯一的错误

<!DOCTYPE html>
<html>
<body>
  <h1> Create</h1><br>
  <form method="post">
      id: <input type="number" name="id"><br>
      creator: <input type="text" name="creator"><br>
      title: <input type="text" name="title"><br>
      type: <select name ="type">
        <option value="Crime">Crime</option>
        <option value="Fiction">Fiction</option>
        <option value="Non-Fiction">Non-Fiction</option>
        <option value="Thriller">Thriller</option>
      </select><br>
      identifier: <input type="text" name="identifier" value ="ISBN"><br>
      date: <input type="text" name="date"><br>
      language: <select name ="langauge">
        <option value="en-GB">en-GB</option>
        <option value="en-US">en-US</option>
        <option value="fr-FR">fr-FR</option>
        <option value="fr-CA">fr-CA</option>
      </select><br>
      description: <input type="text" name="description"><br>
      <input type="submit">

  </form>

创建要插入表中的数据

<?php
function create()
{
  $servername = "localhost";
  $username = "root";
  $password = "";
  $dbname="dbname";

  //create connection
  $conn = mysqli_connect($servername, $username, $password, $dbname);

  // check connection
  if(!$conn)
  {
    die("Connection failed: " . mysqli_connect_error());
  }
  echo "Connected successfully";

  // assign variable names to take in the data and then be used
  $creator = $_POST['creator'];
  $title = $_POST['title'];
  $type = $_POST['type'];
  $identifier = $_POST['identifier'];
  $date = $_POST['date'];
  $language = $_POST['language'];
  $description = $_POST['description'];

  $sql = "INSERT INTO table (creator, title, type, identifier, date, language, description)
  VALUES ($creator', '$title', '$type', '$identifier', '$date', '$language', '$description')";

  if (mysql_query($conn, $sql))
  {
     echo "New record created successfully";
  }
   else
   {
      echo "Error: " . $sql . "<br>" . mysql_error($conn);
   }

   // Close the connection
   mysqli_close($conn);
}

  if(isset($_POST['submit']))
  {
      create();

  }
?>

从表中检索数据并显示在表输出中

<h1>Retrieve</h1><br>
<?php
function retrieve()
{
  $servername = "localhost";
  $username = "root";
  $password = "";
  $dbname="dbname";

  //create connection
  $conn = new mysqli($servername, $username, $password, $dbname);

  // check connection
  if($conn ->connect_error)
  {
    die("Connection failed: " . $conn->connect_error);
  }

  $sql = "SELECT * FROM table";
  $result = $conn->query($sql);

  if($result->num_rows >0)
  {
    // Outputs the table headers using echo.
    echo "<table id = 'eBookTable'><tr><th>id</th><th>creator</th><th>title</th><th>type</th><th>identifier</th><th>date</th><th>language</th><th>description</th></tr>";

    //output the data of each row..
    while($row = $result->fetch_assoc())
    {
      echo "<tr><td>".$row["id"]."</td><td>".$row["creator"]."</td><td>".$row["title"]."</td><td>".$row["type"]."</td><td>".$row["identifier"]."</td><td>".$row["date"]."</td><td>".$row["language"]."</td><td>".$row["description"]."</td></tr>";
    }

    // Outputs the entire table, headers and data...
    echo "</table>";
  }
    else
    {
      echo "0 results found";
    }

    // Close the connection
    $conn->close();
  }

  retrieve();
?>

这将更新表中的数据

<h1>Update</h1><br>
<!-- Use a from to update data-->
<form method ="post">
  Name of Col: <input type = "text" name = "name"><br>
  Replace with: <input type = "text" name = "replace"><br>
  Row: <input type = "text" name = "id"><br>
  <input type = "submit" value = "submit" name = "submit_Update">
</form>

<?php

function update()
{
  $servername = "localhost";
  $username = "root";
  $password = "";
  $dbname= "dbname";
  $replace = $_POST['replace'];
  $name = $_POST['name'];
  $id = $_POST['id'];

  //create connection
  $conn = new mysqli($servername, $username, $password, $dbname);

  // check connection
  if($conn ->connect_error)
  {
    die("Connection failed: " . $conn->connect_error);
  }

  $sql = "UPDATE table SET $name = '$replace' WHERE id = $id";
  if($conn->query($sql) === TRUE)
  {
    echo "Record sucessfully updated";
  }
  else
  {
    echo "Error updating record: " . $conn->error;
  }
  $conn->close();
}
  if(isset($_POST['submit_Update']))
  {
    update();
    echo "<br> Your Updated Table";
    retrieve();
  }
?>

这将根据输入到表单中的ID删除表中的数据

<h1>Delete</h1><br>
<!-- Use form to slect ID and delete the corresponding table row-->
<form method ="post">
  ID: <input type = "text" name = "id"><br>
      <input type = "submit" value "Delete" name = "submit_Delete">


<?php
function delete()
{
  $servername = "localhost";
  $username = "root";
  $password = "";
  $dbname= "dbname";

  //create connection
  $conn = new mysqli($servername, $username, $password, $dbname);

  // check connection
  if($conn->connect_error)
  {
    die("Connection failed: " . $conn->connect_error);
  }

  $id = $_POST['id'];
  //Sql to delete a row from the table
  $sql = "DELETE FROM table WHERE id=$id";

  if ($conn->query($sql) === TRUE)
  {
    echo "Selected record has been sucessfully deleted";
  }  else {
      echo "Error deleting record: " .$conn->error;
  }

    $conn->close();
}
if (isset($_POST['submit_Delete']))
{
  delete();
  echo "<br>Following deletion of selected row:<br>";
  retrieve();
}

?>

</body>
</html>

如果有帮助的话,我也在使用XAMPP

1 个答案:

答案 0 :(得分:0)

$creator字段之前,INSERT语句中缺少单引号,这将防止插入任何数据。应该是这样的:

$sql = "INSERT INTO table (creator, title, type, identifier, date, language, description)
  VALUES ('$creator', '$title', '$type', '$identifier', '$date', '$language', '$description')";

还请遵循其他用户给出的建议,例如不要混用mysql_mysqli_函数。