Ajax / PHP注意:未定义的索引:

时间:2015-01-23 21:44:16

标签: javascript php jquery ajax http-post

用这个把头发拉出来。突然收到以下错误:

>>PHP Notice:  Undefined index: rmvFileList in C:\wamp\www\somesubdirectory\members\delete.php on line 7

这是来自Ajax提交到delete.php。虽然"通知"正在出现,脚本正在正常执行(正确完成数据库删除,正确删除文件,并在console.log中正确记录成功调用)。可能只是某种病态的痴迷,但我想在每次调用删除脚本时摆脱错误日志中弹出的警告。

这是ajax:

var rmvFile = "Some file name";

$.ajax({
type: "POST",
url: "delete.php",  
dataType:"text",                
data: {'rmvFileList' : rmvFile },
success: function(returnData) {
console.log(returnData);
}
});

这是delete.php:

<?php
session_start();
include('../../phpincl/db.php');

$table = 'phoeteo_img_' . $_SESSION['memberID'] . '_' . $_SESSION['showID'];

$name = $_POST['rmvFileList'];  <<--This is line 7

$sql = "SELECT * FROM $table WHERE imgNameTime = '$name'";
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));

while($row = mysqli_fetch_assoc($result))
{
    $toBeDeletedIndex = $row['imgIndex'];
    $toBeDeletedFilename = $row['imgNameTime'];
    $sql = "DELETE FROM $table WHERE imgNameTime = '$name'";

    mysqli_query($conn, $sql) or die('Failed: ' . mysqli_error($conn));

    $path = 'repository/' . $_SESSION['memberID'] . '_' . $_SESSION['showID'] . '/' . $toBeDeletedFilename;

    unlink($path);
}

echo $name;
?>

非常感谢任何和所有帮助。谢谢!

1 个答案:

答案 0 :(得分:0)

在php中设置 isset :检查以下代码:

if(isset($_POST['rmvFileList'])){
$name = $_POST['rmvFileList'];
$sql = "SELECT * FROM $table WHERE imgNameTime = '$name'";
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));

while($row = mysqli_fetch_assoc($result))
{
$toBeDeletedIndex = $row['imgIndex'];
$toBeDeletedFilename = $row['imgNameTime'];
$sql = "DELETE FROM $table WHERE imgNameTime = '$name'";

mysqli_query($conn, $sql) or die('Failed: ' . mysqli_error($conn));

$path = 'repository/' . $_SESSION['memberID'] . '_' . $_SESSION['showID'] . '/' . $toBeDeletedFilename;

unlink($path);
}

echo $name;
 }