简体   繁体   English

MySQL用PHP删除行

[英]MySQL Delete row with PHP

I'm having a problem in deleting a row from one of my table in my mySQL DB. 我在从mySQL DB中的一个表中删除一行时遇到问题。 Below you can see the code I am using (This is the php file, which is calling the SQL query from a form): 在下面,您可以看到我正在使用的代码(这是php文件,它从表单调用SQL查询):

                   <?php 
                  echo '
<form style="padding-left:30px; padding-bottom:40px; padding-top:10px; clear:both;" action="deletingcontactgroups.php">
<label><strong>Please Select A Group to Delete!</strong></label>
<select name="dropdown" style="float:left;">
<option value="">Select a Contact Group:</option>';

$con=mysqli_connect("localhost","username","password","my_db");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM ContactsGroup");


while($row = mysqli_fetch_array($result)) {
  echo '<option value="' . $row["GroupName"] . '">' . $row["GroupName"] . '</option>';
  echo "<br>";
}
echo '<input type="submit" style="clear:both; float:left; margin-left: 300px;">';
echo '</form>';

mysqli_close($con);

                   ?>

This is the data in the file deletingcontactgroups.php : 这是文件deletecontactgroups.php中的数据:

<?php
$con=mysqli_connect("localhost","username","password","my_db");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$groupname = mysqli_real_escape_string($con, $_POST['dropdown']);
$sql = "DELETE FROM `bulletproofaccounting`.`ContactsGroup` WHERE `ContactsGroup`.`GroupName` = '" . $groupname . "' LIMIT 1;";
mysqli_query($sql);

echo "1 record has been deleted successully!!!";

mysqli_close($con);
?>

I looked into various tutorials, but nothing is helping. 我研究了各种教程,但没有任何帮助。 Any help would be highly appreciated! 任何帮助将不胜感激!

EDIT!!! 编辑!!! I'm extremely sorry for the typo, but the quotation marks were actually placed during concatenation of the variable $sql. 我为错别字感到非常抱歉,但是引号实际上是在变量$ sql的串联过程中放置​​的。 Sorry for the trouble. 抱歉,添麻烦了。

Change your delete statement to this 将您的删除语句更改为此

  $sql = sprintf("delete from contactsgroup where groupname = '%s'", $groupname);

Aside, the above is very bad code as it is open to SQL injection attack. 另外,上面的代码很糟糕,因为它很容易受到SQL注入攻击。 You should never string concatenate SQL commands. 您绝对不应使用字符串连接SQL命令。 Use parameterized queries instead: 改用参数化查询:

$stmt = $con->prepare('delete from contactsgroup where groupname = ?');
$stmt->bind_param('s', $groupname);
$stmt->execute();

You open this string in a way (") and you close this in another way (') 您以一种方式(“)打开此字符串,而以另一种方式(')关闭此字符串

  "DELETE FROM `bulletproofaccounting`.`ContactsGroup` WHERE `ContactsGroup`.`GroupName` = '

try to change from this: 尝试从此更改:

"DELETE FROM `bulletproofaccounting`.`ContactsGroup` WHERE `ContactsGroup`.`GroupName` = "

First, you should consider a prepared statement for this one. 首先,您应该考虑为此准备一份声明。 Why? 为什么? Safety reasons. 安全原因。

Second of all you should use some debugging on the queries to see where is the actual problem. 第二,您应该对查询进行一些调试,以查看实际问题出在哪里。

Here is a example of debug, prepared statement for tests: 这是调试的,准备好的测试语句的示例:

if($stmt=$con->prepare("DELETE FROM bulletproofaccounting.ContactsGroup WHERE ContactsGroup.GroupName = ? LIMIT 1;")){
    if (!$stmt->bind_param("s", $groupname)) {
        echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
    }
    if (!$stmt->execute()) {
        echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
    }
    $stmt->close();
}else{
    echo "Prepare failed: (" . $con->errno . ") " . $con->error;
}

And here you can find a detailed explanation as to how to evade sql injections. 在这里,您可以找到有关如何逃避sql注入的详细说明。

EDIT: 编辑:

As pointed out in the comments, it's questionable if the solution is complete. 正如评论中指出的那样,解决方案是否完整值得怀疑。 Let me explain: 让我解释:

the query was: 查询是:

DELETE FROM `bulletproofaccounting`.`ContactsGroup` WHERE `ContactsGroup`.`GroupName` = ' . $groupname . ' LIMIT 1;

and became: 成为:

DELETE FROM bulletproofaccounting.ContactsGroup WHERE ContactsGroup.GroupName = ? LIMIT 1;

as many people pointed out, the problem is in the quotes, prior to adding the $groupname variable, which is actually completely evaded with the prepared statement since the '?' 正如许多人指出的那样,问题出在加$ groupname变量之前的引号中,该变量实际上是从'?'语句开始就完全避开了准备好的语句 substitutes the proper syntax for adding a variable inside the query, and thus it fixes the incorrect query syntax, written inside the question. 替换为在查询中添加变量的正确语法,从而解决了写在问题内部的错误查询语法。

The answer to the question was that I made a stupid mistake. 问题的答案是我犯了一个愚蠢的错误。 I completely forgot to add a method to the form, which was why the variable was not getting any values. 我完全忘记了向表单添加方法,这就是为什么变量没有任何值的原因。

However, I would like to thank Hristo Valkanov & itsben for the SQLi advice. 但是,我要感谢Hristo Valkanov和itsben的SQLi建议。 Since I am not that great at coding and still learning, it's great advice, and something I will be using in the future. 由于我不太擅长编码和学习,因此这是很好的建议,将来我会用到一些东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM