问题描述
我之前问过的问题 pdo检索数据并填充记录关于输入掩码,现在我需要验证用户输入并将已输入的内容添加到db表中,这是最后一步.
the questions I have asked earlierpdo to retrieve data and populate a record was about the input mask now i need to validate the user input and add what has been entered to a db table and this is the very last step.
我的错误是,如您在下面的代码中看到的那样,我使用 PDO 误解了 INSERT INTO 和 UPDATE SET .
My mistake is as you can see in the below code that I misinterpret the INSERT INTO and UPDATE SET using PDO.
此外,关于INSERT INTO
我确实使用bindParam
来尝试输入数据,而关于UPDATE SET
我则使用execute(array)
.实际上,此代码可验证用户数据输入,以及该输入是否正确,php会尝试连接到db,并应插入或更新表.奇怪的是,没有错误返回,也没有添加数据
Furthermore as far as concerned with INSERT INTO
I do use bindParam
in order to attempt a data entry, while about the UPDATE SET
I use execute(array)
. As a matter of fact this code validates the user data input and whether that input is correct php attempts to connect to db and should insert into or update a table. The strange part is that no error is returned and no data is added
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
?>
<?php
$servername = "xxx";
$username = "xx";
$password = "xxx";
$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);
echo 'Connected to database<br />';
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
$sth = $dbh->prepare("use accessibilita");
?>
<?php
switch ($_GET['action']) {
case 'add':
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("INSERT INTO tagesroma(nome, cognome, indirizzo, civico, citta, prov)
VALUES (:nome, :cognome, :indirizzo, :civico, :citta, :prov)");
$stmt->bindParam(':nome', $nome);
$stmt->bindParam(':cognome', $cognome);
$stmt->bindParam(':indirizzo', $indirizzo);
$stmt->bindParam(':civico', $civico);
$stmt->bindParam(':citta', $citta);
$stmt->bindParam(':prov', $prov);
} else {
header('Location:tages.php?action=add' .
'&error=' . join($error, urlencode('<br/>')));
}
break;
}
break;
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)) {
//SYNTAX ERROR CORRECTION
$stmt = $dbh->prepare("UPDATE tagesroma SET nome=?, cognome=?, indirizzo=?, civico=?, citta=?, prov=?)");
$stmt->execute(array($nome, $cognome, $indirizzo, $civico, $citta, $prov));
} else {
header('Location:tages.php?action=edit&id=' . $_POST['id'] .
'&error=' . join($error, urlencode('<br/>')));
}
break;
}
break;
}
?>
<html>
<head>
<title>Commit</title>
<meta charset="UTF-8">
</head>
<body>
<p>Done!</p>
</body>
</html>
更新/
我确实纠正了UPDATE SET
部分,但仍然没有添加数据
I did correct the UPDATE SET
part but still no data is added
推荐答案
此处的问题是您从未执行过INSERT操作
The problem here is that you never executed for the INSERT
将此添加到它:
$stmt -> execute();
这就是为什么没有错误返回的原因,因为没有错误;只是一些缺失";-)
which is why no errors return, because there are none; just something "missing" ;-)
参考:
这篇关于验证和数据添加到数据库表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!