如何获取PHP中的最后一个错误?

时间:2014-09-09 10:59:18

标签: php mysql mamp

我只是尝试使用PHP将我的表单连接到我的sql但它总是使用函数echo error_reporting()报告32767并且我在mac上使用MAMP 2.1 这是我的全部代码:

$name = $_POST[name];
$topic = $_POST[topic];
$tell = $_POST[tell];
$birthDay = $_POST[birthDay];
$job = $_POST[job];
$email = $_POST[email];
$lastMajor = $_POST[lastMajor];
$major = $_POST[major];
$add = $_POST[add];
$today = "test";

$connectionString = mysqli_connect("localhost","root","root","myDB");
 if (mysqli_connect_errno()) 
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

mysqli_query($connectionString,"INSERT INTO coWork (topic , name , tell , birthDay , job , email , lastMajor , major, add , date )
     VALUES ('$topic', '$name','$tell','$birthDay','$job' , '$email' , '$lastMajor' , '$major' , '$add' , '$today')");

mysqli_close($connectionString);
echo error_reporting();

我该如何解决这个问题?!!

1 个答案:

答案 0 :(得分:3)

error_reporting() => Sets which PHP errors are reported

要获取/处理错误,您必须使用error_get_lastset_error_handler

等功能

在您的情况下,您希望收到MySql错误,因此您必须使用mysqli_errormysqli_errno

if (!mysqli_query($connectionString, 'INSERT ...')) {
    printf("Error: %s\n", mysqli_error($connectionString));
}

就像Quentin所说,你很容易受到SQL注入攻击。因此,请使用mysqli_real_escape_stringmysqli_prepare