【发布时间】:2015-07-29 16:10:38
【问题描述】:
我有一个记录列表,每个人都可以编辑。每当用户单击编辑时,就会出现一个输入掩码,其中需要输入新数据以替换旧数据。我使用 PDO 来处理数据库连接,我坚信问题是我无法更新现有表
这是我的旧帖子validation and data add to a db table的链接
第一个 sn-p 代码创建了一个表单,用户可以在其中输入一些数据。
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
?>
<?php
$servername = "xxxx";
$username = "xxxx";
$password = "xxxxx";
$dbname = "xxxxx";
try {
$dbh = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
<?php
if ($_GET['action'] == 'edit') {
//retrieve the record's information
$sth = $dbh->prepare("
SELECT nome, cognome, indirizzo, civico, citta, prov
FROM tagesroma
WHERE id = ?
");
$sth->execute(array($_GET['id']));
} else {
//set values to blank
$nome = '';
$cognome = '';
$indirizzo = '';
$civico = 0;
$citta = '';
$prov = '';
}
?>
<html>
<head>
<meta charset="UTF-8">
<title><?php echo ucfirst($_GET['action']); ?> Tages</title>
<style type="text/css">
<!--
#error { background-color: #600; border: 1px solid #FF0; color: #FFF;
text-align: center; margin: 10px; padding: 10px; }
-->
</style>
</head>
<body>
<?php
if (isset($_GET['error']) && $_GET['error'] != '') {
echo '<div id="error">' . $_GET['error'] . '</div>';
}
?>
<form action="commit.php?action=<?php echo $_GET['action']; ?>&type=tages"
method="post" accept-charset="UTF-8">
<table>
<tr>
<td>Nome</td>
<td><input type="text" name= "nome" value="<?php echo !empty($_POST['nome']) ? $_POST['nome'] : ''; ?>"></td>
</tr><tr>
<td>Cognome</td>
<td><input type="text" name= "cognome" value="<?php echo !empty($_POST['cognome']) ? $_POST['cognome'] : ''; ?>"></td>
</tr><tr>
<td>Indirizzo</td>
<td><input type="text" name= "indirizzo" value="<?php echo !empty($_POST['indirizzo']) ? $_POST['indirizzo'] : ''; ?>"></td>
</tr><tr>
<td>Civico</td>
<td><input type="text" name= "civico" value="<?php echo !empty($_POST['civico']) ? $_POST['civico'] : ''; ?>"></td>
</tr><tr>
<td>Citta</td>
<td><input type="text" name= "citta" value="<?php echo !empty($_POST['citta']) ? $_POST['citta'] : ''; ?>"></td>
</tr><tr>
<td>Prov</td>
<td><input type="text" name= "prov" value="<?php echo !empty($_POST['prov']) ? $_POST['prov'] : ''; ?>"></td>
</tr><tr>
<td colspan="2" style="text-align: center;">
<?php
if ($_GET['action'] == 'edit') {
echo '<input type="hidden" value="' . $_GET['id'] . '" name="id" />';
}
?>
<input type="submit" name="submit"
value="<?php echo ucfirst($_GET['action']); ?>" />
</td>
</tr>
</table>
</form>
</body>
</html>
代码的第二部分验证输入的数据,当且仅当一切正确时,新数据才会更新
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
?>
<?php
$servername = "xxx";
$username = "xxx";
$password = "xxxx";
$dbname = "xxxx";
try {
$dbh = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
$sth = $dbh->prepare("use accessibilita");
?>
<?php
switch ($_GET['action']) {
case 'edit':
switch ($_GET['type']) {
case 'tages':
$error = array();
$nome = isset($_POST['nome']) ?
trim($_POST['nome']) : '';
if (empty($nome)) {
$error[] = urlencode('Si prega di inserire un nome.');
}
$cognome = isset($_POST['cognome']) ?
trim($_POST['cognome']) : '';
if (empty($cognome)) {
$error[] = urlencode('Si prega di inserire un cognome.');
}
$indirizzo = isset($_POST['indirizzo']) ?
trim($_POST['indirizzo']) : '';
if (empty($indirizzo)) {
$error[] = urlencode('Si prega di inserire un indirizzo.');
}
$civico = isset($_POST['civico']) ?
trim($_POST['civico']) : '';
if (empty($civico)) {
$error[] = urlencode('Si prega di inserire un numero civico.');
}
$citta = isset($_POST['citta']) ?
trim($_POST['citta']) : '';
if (empty($citta)) {
$error[] = urlencode('Si prega di inserire una citta valida.');
}
$prov = isset($_POST['prov']) ?
trim($_POST['prov']) : '';
if (empty($prov)) {
$error[] = urlencode('Si prega di inserire una provincia.');
}
if (empty($error)) {
$stmt = $dbh->prepare("UPDATE tagesroma SET nome=?, cognome=?, indirizzo=?, civico=?, citta=?, prov=? WHERE id=1");
$stmt->execute(array($nome, $cognome, $indirizzo, $civico, $citta, $prov));
} else {
header('Location:tages.php?action=edit&id=' . $_GET['id'] .
'&error=' . join($error, urlencode('<br/>')));
}
break;
}
break;
}
?>
<html>
<head>
<title>Commit</title>
<meta charset="UTF-8">
</head>
<body>
<p>Done!</p>
</body>
</html>
【问题讨论】:
-
把prepare和execute放在一个TRY CATCH块中并报告任何错误。然后你可能会看到什么问题
-
很确定您的表单操作是错误的。
-
你知道吗?直到今天一切正常,但现在这并不是绝对令人沮丧
-
@RiggsFolly 你能给我一个答案或建议吗?我现在无法处理代码
-
请执行一些调试,以便您可以将代码示例缩短到相关部分,并且能够更好地解释代码执行与您期望的不同之处。