我想通过单击PHP中的Button来删除表中的所有行。我使用过这段代码
<form>
<input type="submit" class="button" name="delet" id="delet" value="DELETE WHOLE DATA"/>
</form>
<br></br>
<?php
//if($_GET){
if(isset($_GET['delet'])){
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$sql = "DELETE FROM tfinal";
$sql1=mysql_query($sql, $link);
if (mysql_query( $sql1)) {
echo "Database dqqwewrw was successfully dropped\n";
} else {
echo 'Error dropping database: ' . mysql_error() . "\n";
}
}
?>
但在毁了之后说:Error dropping database: Query was empty
关于克服这个问题的任何想法
答案 0 :(得分:4)
mysql_*
- officially deprecated和vulnerable to injection。切换到使用mysqli_*
或PDO准备好的语句。 DELETE
用于删除行; DROP
用于删除表格。您的SQL查询应如下所示:
$sql = "DROP TABLE `tablename`";
您正在运行查询两次 - 一次在$sql1 = mysql_query(...)
中,再次在if(mysql_query(...))
中。对于DROP
或DELETE
查询,it will return TRUE
on success and FALSE
on error。
第二次运行查询(if(mysql_query(...))
)时,您正在查询第一个查询($sql1
)的结果,而不是查询本身($sql
)。你应该写:
if(mysql_query($sql)){
...而是采用PDO / mysqli_
格式。
参考文献:
答案 1 :(得分:2)
试试这段代码:
<form>
<input type="submit" class="button" name="delet" id="delet" value="DELETE WHOLE DATA"/>
</form>
<br></br>
<?php
if(isset($_GET['delet'])){
$connection = mysqli_connect('localhost', 'root', 'root', 'testlo');
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$sql = 'DELETE FROM tbl_fordelete';
$sql1=mysqli_query($connection, $sql);
if ($sql1) {
echo "Database dqqwewrw was successfully dropped\n";
} else {
echo 'Error dropping database: ' . mysql_error() . "\n";
}
$connection->close();
}
?>
答案 2 :(得分:1)
if(mysql_query($sql1)) {
那是错的。 $ sql1由mysql_query()返回,因此它不是查询,而是结果。
相关:What does a successful MySQL DELETE return? How to check if DELETE was successful?
答案 3 :(得分:0)
进行以下更改:
<form>
<input type="submit" class="button" name="delet" id="delet" value="DELETE WHOLE DATA"/>
</form>
<br></br>
<?php
//if($_GET){
if(isset($_GET['delet'])){
$link = mysqli_connect('localhost', 'root', '');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$sql = "DROP TABLE tfinal";
//$sql1=mysql_query($sql, $link);
if (mysqli_query($link, $sql)) {
echo "Database dqqwewrw was successfully dropped\n";
} else {
echo 'Error dropping database: ' . mysqli_error() . "\n";
}
}
?>
答案 4 :(得分:0)
如果要删除表,首先应创建临时表 例如:
$i="i";$joined=$i.$numCar;
$sql = "CREATE TEMPORARY TABLE $joined (
id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
km INT(11) UNSIGNED NOT NULL,
gas INT(1) UNSIGNED NOT NULL,
comment TEXT NOT NULL,
date VARCHAR(32) NOT NULL,
workId INT(11) UNSIGNED NOT NULL,
name VARCHAR(32) NOT NULL,
damage TINYINT(1) NOT NULL,
image1 VARCHAR(128) NOT NULL,
image2 VARCHAR(128) NOT NULL)";
并且可以删除表后,例如:
$i="i";$joined1=$i.$numberCar;$sql = "DELETE FROM '$joined1'";mysqli_query($con,$sql);
您必须拥有每个表的DROP权限。 更好地解释的链接: mySql