验证和数据添加到数据库表

编程入门 行业动态 更新时间:2024-10-27 11:16:01
本文介绍了验证和数据添加到数据库表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我之前问过的问题 pdo检索数据并填充记录

the questions I have asked earlier pdo 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" ;-)

参考:

  • php/pdo.prepared-statements

更多推荐

验证和数据添加到数据库表

本文发布于:2023-07-16 07:32:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1119395.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!