如何让用户使用php删除个人资料?

时间:2019-02-07 12:16:07

标签: php jquery html

我目前有一个网站,用户可以在该网站上在联系人页面上添加他们认识的人的个人资料。我现在也试图让他们删除特定的个人资料,但我无法使其正常工作。这就是我到目前为止所拥有的。

反馈后的2.0版(现在可以正常使用):

   

  <form action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "POST">
    profile = <input type = "text" name = "profile" required />
    <input type = "submit" />
  </form>

<?php

  $servername = "localhost";
  $username = "root";
  $password = "root";
  $dbname = "registration";

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

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

  $prepped = $conn->prepare ('DELETE FROM users WHERE id = ?'); 
  $prepped->bind_param ('i', $_POST["profile"]); 
    if ($prepped->execute()) {
      // success
    } else {
      // failure
    }

  var_dump($_POST);

?>

3 个答案:

答案 0 :(得分:2)

您正在执行的查询实际上是在数据库中查找ID“配置文件”。假定ID列是数字列,因此它不可能包含ID为“配置文件”的任何行。

您需要将要删除的ID作为参数传递给查询

$prepped = $conn->prepare ('DELETE FROM uses WHERE id = ?'); // The "?" is a placeholder that MySQL will replace with the specified parameter
$prepped->bind_param ('i', $_POST["profile"]); // 'i' indicates we're expecting an integer.  See PHP docs for other options
if ($prepped->execute()) {
    // The query executed without any errors (though you should also check how many rows were affected at this point to determine if anything was actually deleted)
} else {
    // Failure
}

此外,您的表单标记不正确,因此可能无法发布到正确的位置。

<form action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "POST">
    Profile = <input type = "text" name = "profile" required />
    <input type = "submit" />
</form>

答案 1 :(得分:0)

您的查询应类似于:

$query = sprintf("DELETE FROM uses WHERE id = '%s'", $profile);

或不包含sprintf()

$query = "DELETE FROM uses WHERE id = '" . $profile . "'";

评论后编辑

$profile是一个变量,其中包含名称/ id /用来配置用户的字符串。从您的表单中,您应该以这种方式定义它:

$profile = $conn->real_escape_string($_POST["profile"]);

当然在查询之前。

答案 2 :(得分:-1)

  • 表单操作应为<?php echo $_SERVER['PHP_SELF']; ?>

  • 另请参见代码。.您正在做的是在if语句$sql中传递未知的if ($conn->query($sql) === TRUE)变量 您必须传递此语句中上面定义的$query变量

请用此代码替换您的代码..希望它对您有用:)好运

 <form action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "POST">
     Profile = <input type = "text" name = "profile" required />
     <input type = "submit" />
  </form>

<?php

  $servername = "localhost";
  $username = "root";
  $password = "root";
  $dbname = "registration";

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

   if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
   }
   ///////////NEW CODE///////////
    $profile_id = mysql_real_escape_string($_POST['profile']);

    $query = "DELETE FROM users WHERE id= '".$profile_id."'";

    if ($conn->query($query) === TRUE) {
      echo "Profile deleted successfully";
    } else {
      echo "Error deleting profile: " . $conn->error;
    }
   //////////////////////////////


?>