如何解决php/mysql插入失败问题?

Cra*_*rty 1 php mysql

我无法让这个mysql查询正常工作.它完成时没有错误,但没有信息插入数据库.我不是很关心找到一个简单的解决方案,但我怎么能让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')");
}

?>
Run Code Online (Sandbox Code Playgroud)

Mar*_*c B 6

如何追踪它?你进行了适当的错误处理.过程模式下的mysqli将在失败时返回布尔值FALSE.您的代码都没有实际检查这些返回值,只是假设一切正常.

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

$result = mysqli_query(...);
if (!$result) {
   die(mysqli_error());
}
Run Code Online (Sandbox Code Playgroud)

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