Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何排除php/mysql插入失败的故障?_Php_Mysql - Fatal编程技术网

如何排除php/mysql插入失败的故障?

如何排除php/mysql插入失败的故障?,php,mysql,Php,Mysql,我无法让这个mysql查询正常工作。它完成时没有错误,但没有向数据库中插入任何信息。我不太关心寻找iMedia解决方案,但我如何让mysql报告幕后发生的事情 <?php error_reporting(E_ALL); ini_set('display_errors', '1'); $tempProf = $_POST["professor"]; $tempProfArray = explode("=",$tempProf); $prof = $tempProfArray[1]; $t

我无法让这个mysql查询正常工作。它完成时没有错误,但没有向数据库中插入任何信息。我不太关心寻找iMedia解决方案,但我如何让mysql报告幕后发生的事情

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

$tempProf = $_POST["professor"];
$tempProfArray = explode("=",$tempProf);
$prof = $tempProfArray[1];

$tempName = $_POST["name"];
$tempNameArray = explode("=",$tempName);
$name = $tempNameArray[1];

$tempNum = $_POST["number"];
$tempNumArray = explode("=",$tempNum);
$num = $tempNumArray[1];

$tempSec = $_POST["section"];
$tempSecArray = explode("=",$tempSec);
$section = $tempSecArray[1];

$tempCat = $_POST["category"];
$tempCatArray = explode("=",$tempCat);
$category = $tempCatArray[1];

$con=mysqli_connect("localhost","root","*****","******");

$result = mysqli_query($con,"SELECT * FROM professors where id='$prof'");
$row = mysqli_fetch_array($result);



if(empty($prof) || empty($name) || empty($num) || empty($section) || empty($category))
{
    echo "emptyField";
}
elseif(!is_numeric($num)  || !is_numeric($section))
{
    echo "NaN";
}
elseif(empty($row))
{
    mysqli_query($con,"INSERT INTO classes (className, classNumber, section, classCategory)
    VALUES ('$name','$num','$section','$category')");

    $classTemp = mysqli_query($con,"SELECT id FROM classes where className='$name' and classNumber='$num' and section ='$section'");
    $classTempArray = mysqli_fetch_array($classTemp);
    $classId = $classTempArray['id'];

    mysqli_query($con,"INSERT INTO professors (name, classes) VALUES ('$name','$classId')");

    $profTemp = mysqli_query($con,"SELECT id FROM professors where name='$name'");
    $profTempArray = mysqli_fetch_array($profTemp);
    $profId = $classTempArray['id'];

    mysqli_query($con,"INSERT INTO classes (professor) VALUES ('$profId') where id ='$classId'");

}
else
{
    $profName = $row['id'];
    mysqli_query($con,"INSERT INTO classes ($profName, className, classNumber, section, classCategory)
VALUES ('$prof', '$name','$num','$section','$category')");
}

?>

启用MySQL日志,然后
tail-f/path/to/the/log/file

您可以从MySql服务器的角度监视什么查询以及何时执行

您还可以使用grep进行过滤

tail-f/path/to/to/log/file | grep'INSERT'-color

启用MySQL日志,然后
tail-f/path/to/the/log/file

您可以从MySql服务器的角度监视什么查询以及何时执行

您还可以使用grep进行过滤

tail-f/path/to/to/log/file | grep'INSERT'-color

如何跟踪它?您输入了正确的错误处理。程序模式下的mysqli在失败时将返回布尔值FALSE。您的代码实际上都没有检查这些返回值,只是简单地假设一切正常

您的基本查询代码流应该是

$result = mysqli_query(...);
if (!$result) {
   die(mysqli_error());
}

您的所有代码也容易受到攻击。在您知道如何处理此问题之前,不要将此代码投入生产环境,甚至测试环境。

如何跟踪它?您输入了正确的错误处理。程序模式下的mysqli在失败时将返回布尔值FALSE。您的代码实际上都没有检查这些返回值,只是简单地假设一切正常

您的基本查询代码流应该是

$result = mysqli_query(...);
if (!$result) {
   die(mysqli_error());
}
您的所有代码也容易受到攻击。在您知道如何处理此问题之前,不要将此代码投入生产环境,甚至测试环境。

您应该使用pdo(),因为它比mysqli更安全

您可以使用

调试msqli中的错误。您应该使用pdo(),因为它比mysqli更安全


您可以使用

调试msqli中的错误。是否打开了错误?请尝试
mysqli\u error($connection\u string)
我更改了,但这不是问题所在。如何从中获取实际的文本数据?
mysqli\u error($connection\u string)
是否自行回显字符串?是否打开了错误?请尝试
mysqli\u error($connection\u string)
我更改了,但这不是问题所在。如何从中获取实际的文本数据?
mysqli\u error($connection\u string)
会自动回显字符串吗?非常有趣,但说到Sql注入,我真的很迷茫。我不明白在您已经对变量进行了查询之后,转义对它做了什么。您应该在构建查询之前转义它。在事实发生后逃跑是毫无意义的,也是毫无用处的。这就是我所想的,但在我所看到的所有例子中,逃跑都发生在查询之后。谢谢。您使用的是mysqli,所以您可以只使用占位符和/或准备好的语句。这几乎完全消除了逃逸的必要性。非常有趣,但说到Sql注入,我真的很迷茫。我不明白在您已经对变量进行了查询之后,转义对它做了什么。您应该在构建查询之前转义它。在事实发生后逃跑是毫无意义的,也是毫无用处的。这就是我所想的,但在我所看到的所有例子中,逃跑都发生在查询之后。谢谢。您使用的是mysqli,所以您可以只使用占位符和/或准备好的语句。这几乎完全消除了逃跑的必要性。