[英]PHP, MySQL, PDO - Insert Into Not Working
I have this code: 我有以下代码:
<?php
require_once 'dbconfig.php';
if(isset($_GET['id'])) {
try {
$db = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$getSavedHolidays = $db->prepare("INSERT INTO tvinfo(id, imdbid, name, rating, genre1, genre2, year, plot, uploader, views, downloads, uploaddate, size, resolution, fps, audio) VALUES (:id, :id, :id, :id, :id, :id, :id, :id, :id, :id, :id, :id, :id, :id, :id, :id)");
$getSavedHolidays->setFetchMode(PDO::FETCH_ASSOC);
$getSavedHolidays->execute(array(':id' => $_GET['id']));
$Result = $getSavedHolidays->fetchAll();
if(!$Result){
die('Error: ID Not Found');
}
}
catch (PDOException $e) {
print_r($e->errorInfo);
die();
}
foreach ($Result as $r) {
echo 'Name: '.$r['name'].'<br>';
echo 'Rating: '.$r['rating'].'<br>';
echo 'IMDB ID: '.$r['imdbid'].'<br>';
echo 'Genre 1: '.$r['genre1'].'<br>';
echo 'Genre 2: '.$r['genre2'].'<br>';
echo 'Year: '.$r['year'].'<br>';
echo 'Plot: '.$r['plot'].'<br>';
echo 'Uploader: '.$r['uploader'].'<br>';
echo 'Views: '.$r['views'].'<br>';
echo 'Downloads: '.$r['downloads'].'<br>';
echo 'Uploaded at: '.$r['uploaddate'].'<br>';
echo 'Size: '.$r['size'].'<br>';
echo 'Resoution: '.$r['rsolution'].'<br>';
echo 'FPS: '.$r['fps'].'<br>';
echo 'Audio: '.$r['audio'].'<br>';
}
}
?>
When i go to example.com/file.php?id=Lel it outputs: Array ( [0] => HY093 ) and no error_logs. 当我转到example.com/file.php?id=Lel时,它输出:数组([0] => HY093)并且没有error_logs。 the tables etc are definitely there.
桌子等肯定在那里。
This code works fine when I use it with a select query to grab some info and output it but when I try inserting it wont work. 当我将它与选择查询一起使用以获取一些信息并将其输出时,此代码可以正常工作,但是当我尝试将其插入时将无法正常工作。 Any ideas?
有任何想法吗?
You can set ATTR_EMULATE_PREPARES
to true: 您可以将
ATTR_EMULATE_PREPARES
设置为true:
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
then you will be allowed to bind a single time or you will have to do it for each time you have it in the query 那么您将可以绑定一次,或者每次查询中都必须绑定一次
Also get rid of $Result = $getSavedHolidays->fetchAll();
还摆脱
$Result = $getSavedHolidays->fetchAll();
since you are inserting there is nothing to fetch 因为您要插入,所以没有什么可提取的
To check if the value exist before inserting, you can do: 要在插入之前检查该值是否存在,可以执行以下操作:
$sql = 'SELECT COUNT(*) from tvinfo WHERE imdbid = :id';
$stmt = $conn->prepare($sql);
$stmt->execute(array(':id' => $_GET['id']));
if($stmt->fetchColumn()){ die('Already exist');}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.