我一直在尝试设置此代码以删除mysql数据库中的行以及随其上传的照片。删除行数据非常有效,但它不会删除照片,我无法弄清楚我做错了什么。为简化起见,我使用变量$ id,它是在触发此php文件的表单中输入的行号:
<?php
$host="localhost"; // Host name
$username="blahblah_plans"; // Mysql username
$password="password"; // Mysql password
$db_name="blahtbl_name"; // Database name
$tbl_name="plans"; // Table name
// Connect to server and select databse.
$conn = mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// get value of id that sent from address bar
$id=$_GET['idnum'];
$compositesql="SELECT composite FROM plans WHERE ID ='$id'";
$compositeresult = mysql_query($compositesql) or die(mysql_error());
$compositefilename = "/composite/" + $compositeresult;
$unlink = unlink($compositefilename);
if($unlink) {
echo 'Successfully deleted file: ';
echo $compositefilename;
} else {
echo 'Error deleting file: ';
echo $compositefilename;
}
// Delete data in mysql from row that has this id
$sql="DELETE FROM $tbl_name WHERE ID ='$id'";
$result = mysql_query($sql);
if($result){
header("location:planentry.php");
}
else {
echo "ERROR";
}
?>
<?php
// close connection
mysql_close();
?>
答案 0 :(得分:5)
确保路径正确,$compositefilename = "/composite/" + $compositeresult;
应该是服务器中的路径,最有可能是
$compositefilename = PATH_TO_YOUR_WEB_ROOT . "/composite/" . $compositeresult;
并且php不使用+
来连接字符串。
答案 1 :(得分:0)
问题是$compositeresult
包含资源而不是结果集。这条线就是它的原因:
$compositeresult = mysql_query($compositesql) or die(mysql_error());
要解决此问题,请将资源存储在变量上,然后将结果集存储在另一个变量上,如下所示:
$compositequery = mysql_query($compositesql) or die(mysql_error());
$compositeresult = mysql_fetch_array($compositequery) or die(mysql_error());
此外,我强烈建议您开始使用mysqli
或PDO
代替mysql
,因为它更安全。另外,正如xdazz所说,PHP的连接运算符是点,而不是加号。所以你的$compositefilename
应该被声明为(注意$compositeresult
是一个数据数组,因此应该明确写出正确的键):
$compositefilename = "/composite/" . $compositeresult['composite'];