[英]PHP using DELETE FROM to remove row in mysql
Below is the code I have been trying to use to delete a row from my mysql database. 下面是我一直试图用来从mysql数据库中删除一行的代码。 I have been searching through the forum and I have yet find the issue. 我一直在论坛中搜索,但尚未找到问题。 I know my connection is working properly as I have been able to use the INSERT INTO command with no problems. 我知道我的连接工作正常,因为我可以毫无问题地使用INSERT INTO命令。 I'm sure it's pretty obvious that I am new to this, looking for simple solution. 我敢肯定,我很陌生,正在寻找简单的解决方案。
<?php
$_POST[title];
$jimmy = $_POST[title];
echo $jimmy; ## this echo is here to make sure I was getting form data sent over correctly
include("dogs.inc");
$cxn = mysqli_connect($host,$user,$passwd,$dbname)
or die("Couldn't connect to server");
$sql = "DELETE FROM ashly3 WHERE title = '$jimmy' " ;
mysqli_query( $cxn, $sql );
include("ashly.php");
?>
$jimmy = $_POST[title];
Should be 应该
$jimmy = $_POST['title'];
But that's not the reason it doesn't work, PHP should fix that error for you with a warning. 但这不是无法正常工作的原因,PHP应该通过警告为您修复该错误。
Also, inserting text from the $_POST
without escaping is going to cause you trouble with SQL injection. 另外,从$_POST
插入文本而不进行转义会导致SQL注入麻烦。 Imagine if the user inserts hello' OR 1
into the title text field. 想象一下,如果用户在标题文本字段中插入hello' OR 1
。 Use http://phptutorial.info/?mysqli.real-escape-string 使用http://phptutorial.info/?mysqli.real-escape-string
You should really consider using PDO for databases. 您应该真正考虑对数据库使用PDO。
Anyways, for your solution, to identify the error: Try this: 无论如何,为您的解决方案确定错误:尝试以下操作:
mysqli_query( $cxn, $sql )or die(mysqli_error()); mysqli_query($ cxn,$ sql)或die(mysqli_error());
Also, please use mysqli_real_escape_string() around your user input :) 另外,请在您的用户输入周围使用mysqli_real_escape_string():)
i think your error is $_POST[title] should be $_POST['title'] , anyway try this code. 我认为您的错误是$ _POST [title]应该是$ _POST ['title'],还是尝试此代码。
<?php
error_reporting(E_ALL);
//$_POST[title];
$jimmy = $_POST['title'];
echo $jimmy; ## this echo is here to make sure I was getting form data sent over correctly
include("dogs.inc");
$cxn = mysqli_connect($host,$user,$passwd,$dbname)
or die("Couldn't connect to server");
$sql = 'DELETE FROM `ashly3` WHERE title = \''.mysqli_real_escape_string($cxn,$jimmy).'\'';
$retval=mysqli_query( $cxn, $sql );
echo "retval:";var_dump($retval);
if($retval===false)
{
echo "mysqli_error:";var_dump(mysqli_error($cxn));
}
include("ashly.php");
?>
One way could be this, it is working for me 一种可能是这样,它对我有用
<?php
require '../access/dbaccess.php';
$dtlname= $_POST['remove_dtlname'];
mysql_query("delete from `sample_tbl` where `dtl_name`='$dtlname'");
?>
Where dbaccess.php is dbaccess.php在哪里
<?php
$link = mysql_connect('<your server>', '<username>', '<password>');
if (!$link)
die('Could not connect: ' . mysql_error());
mysql_select_db('btp');
?>
尝试将变量从$ SQL行中分解出来:
$sql = "DELETE FROM ashly3 WHERE title = '".$jimmy."' " ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.