如何使用 Php 删除 Mysql 中的数据

How to delete data in Mysql using Php

大家好,我很难弄清楚如何使用 php 我使用 MYSQLI 删除数据库中的数据,因为 mysql 给了我警告,我尝试了很多次,但失败的是 code.HERE 图片是:PICTURE ERROR

<!DOCTYPE HTML>
<html>
    <head>
    <style>
       .wrap{
           width:800px;
           margin:0px auto;

       }
      form{
          margin-left:200px;


      }
      a,a:visited{
          text-decoration:none;
          color:black;
      }
    </style>    
    </head>
    <body>
    <?php
    require('connect.php');
        if(isset($_POST['Submit'])){
            $sid=$_POST['name'];
            $sql = "DELETE scholars ". "WHERE sid = $sid";
             $retval = $mysqli->query($sql,$mysqli );

            if(!$retval )
            {
               die('Could not delete data: ' . $mysqli->error);
            }

            echo "Deleted data successfully\n";

            $mysqli->close( );
         }  



    ?>
    <div class="wrap">
    <form action="dscholar.php" method="POST">
     <table>
        <tr>
        <td>ScholarID:</td> 
        <td><input type="text" name="name" required><td>
        </tr>
        <td><input type="submit" name="Submit" value="Delete Scholar"></td>
        <td><button><a href="index.html">Back to Homepage!</a></button></td>
        </tr>

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

这是连接

<?php
$mysqli=new mysqli("localhost","root","","dblogin");

?>

您的查询中缺少 FROM

$sql = "DELETE FROM scholars WHERE sid = '$sid'";

并将变量放入 '

'$sid'

这是经过多次尝试后的答案,我忘记了来源!!!

<!DOCTYPE HTML>
<html>
    <head>
    <style>
       .wrap{
           width:800px;
           margin:0px auto;

       }
      form{
          margin-left:200px;


      }
      a,a:visited{
          text-decoration:none;
          color:black;
      }
    </style>    
    </head>
    <body>
    <?php
    require('connect.php');
        if(isset($_POST['Submit'])){
            $sid=$_POST['name'];
            $sql ="DELETE FROM scholars ". "WHERE sid = $sid";
             if ($mysqli->query($sql) === TRUE) {
    echo "Record deleted successfully";
} else {
    echo "Error deleting record: ";
}

$mysqli->close();

            }



    ?>
    <div class="wrap">
    <form action="dscholar.php" method="POST">
     <table>
        <tr>
        <td>ScholarID:</td> 
        <td><input type="number" name="name" required><td>
        </tr>
        <td><input type="submit" name="Submit" value="Delete Scholar"></td>
        <td><button><a href="index.html">Back to Homepage!</a></button></td>
        </tr>

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