Error: INSERT INTO fails mysql
Error: INSERT INTO fails mysql
我有一个问题 mySQL 我收到这个错误
Notice: Undefined property: PDO::$error in C:\MAMP\htdocs\paresFiles.php on line 33
Error: INSERT INTO `searchtable`(`word`, `array`, `count`) VALUES ( pinocchio , 1 ,-1)
Notice: Undefined property: PDO::$error in C:\MAMP\htdocs\paresFiles.php on line 33
Error: INSERT INTO `searchtable`(`word`, `array`, `count`) VALUES ( alice's adventures in wonderland , 2 ,-1)
Notice: Undefined property: PDO::$error in C:\MAMP\htdocs\paresFiles.php on line 33
Error: INSERT INTO `searchtable`(`word`, `array`, `count`) VALUES ( peter pan , 3 ,-1)
Notice: Undefined property: PDO::$error in C:\MAMP\htdocs\paresFiles.php on line 33
Error: INSERT INTO `searchtable`(`word`, `array`, `count`) VALUES ( snow white , 4 ,-1)
我想在 table 中插入 3 个值到我的搜索 table 中,当我插入它们时出现此错误并且在我的 table 中没有得到任何数据。
我需要做什么来解决这个错误?
这是我的代码(用于插入数据):
include_once 'connect.php';
$dir = "storage";
$title;
$auther;
$fileNum;
// Open a directory, and read its contents
foreach (glob($dir."/*.txt") as $filename) {
$handle = fopen($filename, "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
if(substr( $line, 0, 3 ) === "#id")
{
$fileNum=substr( $line, 5, 3 );
}
if(substr( $line, 0, 6 ) === "#title")
{
$title=substr( $line, 8, 100 );
}
if(substr( $line, 0, 5 ) === "#name")
{
$auther=substr( $line, 8, 12 );
}
}
$titleVal=-1;
$sql = "INSERT INTO `searchtable`(`word`, `array`, `count`) VALUES ($title,$fileNum,$titleVal)";
if ($db->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $db->error;
}
fclose($handle);
}
连接到 mysql:
<?php
$db = new PDO('mysql:host=localhost;dbname=search;charset=utf8mb4', 'root', 'root');
if (!$db) {
die('Could not connect:' . mysql_error());
}
echo 'Connected successfully';
?>
你应该使用准备语句。
像这样
$sql = "INSERT INTO searchtable (word, array, count) VALUES (:word , :array, :count)";
$statement = $db->prepare($sql);
$statement->exec(array(
'word' => $title,
'array' => $fileNum,
'count' => $titleVal
));
我有一个问题 mySQL 我收到这个错误
Notice: Undefined property: PDO::$error in C:\MAMP\htdocs\paresFiles.php on line 33
Error: INSERT INTO `searchtable`(`word`, `array`, `count`) VALUES ( pinocchio , 1 ,-1)
Notice: Undefined property: PDO::$error in C:\MAMP\htdocs\paresFiles.php on line 33
Error: INSERT INTO `searchtable`(`word`, `array`, `count`) VALUES ( alice's adventures in wonderland , 2 ,-1)
Notice: Undefined property: PDO::$error in C:\MAMP\htdocs\paresFiles.php on line 33
Error: INSERT INTO `searchtable`(`word`, `array`, `count`) VALUES ( peter pan , 3 ,-1)
Notice: Undefined property: PDO::$error in C:\MAMP\htdocs\paresFiles.php on line 33
Error: INSERT INTO `searchtable`(`word`, `array`, `count`) VALUES ( snow white , 4 ,-1)
我想在 table 中插入 3 个值到我的搜索 table 中,当我插入它们时出现此错误并且在我的 table 中没有得到任何数据。 我需要做什么来解决这个错误?
这是我的代码(用于插入数据):
include_once 'connect.php';
$dir = "storage";
$title;
$auther;
$fileNum;
// Open a directory, and read its contents
foreach (glob($dir."/*.txt") as $filename) {
$handle = fopen($filename, "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
if(substr( $line, 0, 3 ) === "#id")
{
$fileNum=substr( $line, 5, 3 );
}
if(substr( $line, 0, 6 ) === "#title")
{
$title=substr( $line, 8, 100 );
}
if(substr( $line, 0, 5 ) === "#name")
{
$auther=substr( $line, 8, 12 );
}
}
$titleVal=-1;
$sql = "INSERT INTO `searchtable`(`word`, `array`, `count`) VALUES ($title,$fileNum,$titleVal)";
if ($db->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $db->error;
}
fclose($handle);
}
连接到 mysql:
<?php
$db = new PDO('mysql:host=localhost;dbname=search;charset=utf8mb4', 'root', 'root');
if (!$db) {
die('Could not connect:' . mysql_error());
}
echo 'Connected successfully';
?>
你应该使用准备语句。
像这样
$sql = "INSERT INTO searchtable (word, array, count) VALUES (:word , :array, :count)";
$statement = $db->prepare($sql);
$statement->exec(array(
'word' => $title,
'array' => $fileNum,
'count' => $titleVal
));