php和sql新手。我必须在Netbeans上运行本地数据库(使用db浏览器创建)。
我的数据库连接正常,返回结果。但是当我通过界面编辑/更改结果时,它没有任何影响。我错过了什么吗?
这是我插入新条目的代码:
<?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 } ?>
这是用于更新条目:
<?php
$pdo = new PDO('sqlite:events.db');
if (isset($_POST['submit'])) {
try {
$sql = "UPDATE events SET type = :eventsType, name =:eventsName WHERE id=:movieId";
//named paramaters
$stmt = $pdo->prepare($sql);
$eventsId = filter_input(INPUT_POST, 'eventsId');
$stmt->bindValue(':eventsId', $eventsId, PDO::PARAM_INT);
$eventsType = filter_input(INPUT_POST, 'eventsType');
$stmt->bindValue(':eventsType', $eventsType, PDO::PARAM_STR);
/*$movieDescription = filter_input(INPUT_POST, 'movieDescription');
$stmt->bindValue(':movieDescription', $movieDescription, PDO::PARAM_STR);*/
/*$movieRating = filter_input(INPUT_POST, 'movieRating');
$stmt->bindValue(':movieRating', $movieRating, PDO::PARAM_STR);*/
$stmt->execute();
$pdo = null;
echo "<h1>Done</h1>";
echo '<a href="index.php">Return to main menu</a>';
} catch (PDOException $e) {
print "We had an error: " . $e->getMessage() . "<br/>";
die();
}
?>
<?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 events specified to update";
exit();
}
} catch (PDOException $e) {
print "We had an error: " . $e->getMessage() . "<br/>";
die();
}
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
EventID: <input type="text" readonly name="movieId" value="<?= htmlspecialchars($r['id']); ?>"><br>
Type: <input type="text" name="movieTitle" value="<?= htmlspecialchars($r['title'])?>"><br>
Name: <input type="text" name="movieDescription" value="<?= htmlspecialchars($r['description']); ?>"><br>
<input type="submit" name="submit" value="Update record" >
</form>
<?php } ?>
这是为了删除:
<?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 } ?>
我不知道为什么它不起作用。它是uni的一个例子,并告诉我们改变。我表中的行是id,类型,名称和描述。
任何帮助都非常感激,因为我真的不知道发生了什么!
答案 0 :(得分:1)
您可能会从pdo
收到错误。
设置pdo's error mode以抛出异常....
$pdo = new PDO('sqlite:events.db',
array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING));
或check your execute()
methods for errors。
$stmt->execute() || die $stmt->errorInfo();