Php 为什么我的SQL查询没有更改我的数据库?
新的php和sql。我必须在Netbeans上运行一个本地数据库(用db浏览器创建) 我的数据库连接良好,正在返回结果。但当我通过界面编辑/更改结果时,它没有任何影响。我错过什么了吗 这是我插入新条目的代码:Php 为什么我的SQL查询没有更改我的数据库?,php,mysql,Php,Mysql,新的php和sql。我必须在Netbeans上运行一个本地数据库(用db浏览器创建) 我的数据库连接良好,正在返回结果。但当我通过界面编辑/更改结果时,它没有任何影响。我错过什么了吗 这是我插入新条目的代码: <?php error_reporting(E_ALL); ini_set('display_errors', 1); $pdo = new PDO('sqlite:events.db'); if (isset($_POST['subm
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$pdo = new PDO('sqlite:events.db');
if (isset($_POST['submit'])) {
try {
$sql = "INSERT INTO events (type, name) VALUES (:eventsType, :eventsName)";
//named paramaters
$stmt = $pdo->prepare($sql);
$eventstype = filter_input(INPUT_POST, 'eventstype');
$stmt->bindValue(':eventstype', $eventstype, PDO::PARAM_STR);
$eventsName = filter_input(INPUT_POST, 'eventsName');
$stmt->bindValue(':eventsName', $eventsName, PDO::PARAM_STR);
//$movieRating = filter_input(INPUT_POST, 'movieRating');
//$stmt->bindValue(':movieRating', $movieRating, PDO::PARAM_INT);
$stmt->execute();
$pdo = null;
echo "<h1>Event added to database.</h1>";
echo '<a href="index.php">Return to main menu</a>';
} catch (PDOException $e) {
//for development
print "We had an error: " . $e->getMessage() . "<br/>";
die();
}
?>
<?php } else { ?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
Type: <input type="text" name="eventsType"><br>
Name: <input type="text" name="eventsName"><br>
//Rating: <input type="text" name="movieRating"><br>
<input type="submit" name="submit">
</form>
<?php } ?>
您可能从pdo
中得到错误
设置为抛出异常
$pdo = new PDO('sqlite:events.db',
array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING));
或者
但是你注释掉了movie_id参数!?!不检查错误?愚蠢的教授把戏。更新应该抛出一个错误。。。有三个绑定占位符,但其中只有两个提供了值。删除操作也应引发错误。对bindValue的调用引用了SQL语句中不存在的绑定占位符:eventsId
。(SQL语句中的占位符具有不同的名称。)如果要捕获来自PDO的异常,则需要使用PDO::ATTR_ERRORMODE
启用PDO异常。(这项作业只是让代码运行的一部分。至关重要的是学习调试程序所需的技能。)
<?php
$pdo = new PDO('sqlite:events.db');
if (isset($_POST['submit'])) {
if ( isset($_POST['confirm']) && $_POST['confirm'] == 'yes'){
try {
//better, but still need to do more checking for security
$sql = "DELETE from events WHERE id=:eventId";
//named paramaters
$stmt = $pdo->prepare($sql);
$eventsId = filter_input(INPUT_POST, 'eventsId');
$eventsId = filter_input(INPUT_POST, 'eventsId', FILTER_SANITIZE_NUMBER_INT);
$stmt->bindValue(':eventsId', $eventsId, PDO::PARAM_INT);
$stmt->execute();
$pdo = null;
echo '<h1>Film has been removed from database</h1>';
echo '<a href="index.php">Return to main menu</a>';
} catch (PDOException $e) {
print "We had an error: " . $e->getMessage() . "<br/>";
die();
}
} else {
echo 'You need confirm.';
}
?>
<?php } else {
try {
$sql = "SELECT * FROM events WHERE id=:eventsId LIMIT 1";
//named paramaters
$stmt = $pdo->prepare($sql);
$id = filter_input(INPUT_GET, 'id');
$stmt->bindValue(':eventsId', $id, PDO::PARAM_INT);
$stmt->execute();
$r = $stmt->fetch(PDO::FETCH_ASSOC);
$pdo = null;
if (!$r){
print "No film specified to update";
exit();
}
} catch (PDOException $e) {
print "We had an error: " . $e->getMessage() . "<br/>";
die();
}
?>
Title:<?= htmlspecialchars($r['type'])?> <br>
Desc:<?= htmlspecialchars($r['name']); ?> <br>
Year:<?= htmlspecialchars($r['rating']); ?> <br>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<input type="text" readonly name="eventsId" value="<?= htmlspecialchars($r['id']); ?>"><br>
<input type="checkbox" name="confirm" value="yes">Yes, delete this record<br>
<input type="submit" name="submit" value="Delete record">
</form>
<?php } ?>
$pdo = new PDO('sqlite:events.db',
array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING));
$stmt->execute() || die $stmt->errorInfo();