所以这是我用PDO做的第二个项目,在用我的第一个项目检查后,我不能为我的生活弄清楚为什么这个INSERT不工作而且我没有收到错误信息。这是我在第3阶段内使用PDO的第一次。不要认为这与它有任何关系只是想通过Id添加它。
这是我的联系,这对我没有任何问题,但为了以防万一!
<?php
$connString = "mysql:host=localhost;dbname=rmldb";
$uname = "root";
$pwd = "DB_PASS";
try{
$pdo = new PDO($connString, $uname, $pwd);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e){
echo "Connection failed: " . $e->getMessage();
}?>
这是我的php查询: 我通过添加一个回音检查了我是否正在尝试使用try / catch,并且我一直在尝试结束“尝试”。阻止,但仍然没有插入任何东西,我无法弄清楚为什么。
if($_SERVER["REQUEST_METHOD"] == "POST"){
$formdata['fname'] = trim($_POST['fname']);
$formdata['lname'] = trim($_POST['lname']);
$formdata['email'] = trim($_POST['email']);
$formdata['pwd'] = trim($_POST['pwd']);
$formdata['pwd2'] = trim($_POST['pwd2']);
$formdata['phone'] = trim($_POST['phone']);
$formdata['date'] = $_POST['dateCreated'];
//Checking for empty form values
if(empty($formdata['fname'])){
$err = 1;
$errfname = "First name is required";
}
if(empty($formdata['lname'])){
$err = 1;
$errlname = "Last name is required";
}
if(empty($formdata['email'])){
$err = 1;
$erremail = "Email is required";
}
if(empty($formdata['pwd'])){
$err = 1;
$errpwd = "Please enter a password";
}
if(empty($formdata['pwd2'])){
$err = 1;
$errpwd2 = "Please enter a password";
}
if(empty($formdata['phone'])){
$formdata['phone'] = "N/A";
}
//Checking for matching password values
if($formdata['pwd'] != $formdata['pwd2']){
$err = 1;
$errpwd = "Passwords do not match";
$errpwd2 = "Passwords do not match";
}
//Checking for existing emails
try{
$sql = "INSERT INTO users (fname, lname, email, pwd, phone, dateCreated, admin) VALUES (:fname, :lname, :email, :pwd, :phone, :dateCreated, :admin)";
$stmt = $pdo->prepare($sql);
$stmt->bindValue(":fname", $formdata['fname']);
$stmt->bindValue(":lname", $formdata['lname']);
$stmt->bindValue(":email", $formdata['email']);
$stmt->bindValue(":pwd", $formdata['pwd']);
$stmt->bindValue(":phone", $formdata['phone']);
$stmt->bindValue(":dateCreated", $rightnow);
$stmt->bindValue(":admin", 0);
$stmt->execute();
$showform = 0;
echo "<p class='error'> Recorded!</p>";
}catch(PDOException $e){
$e->getMessage();
}
}
答案 0 :(得分:-1)
这可能不是最好的方法,但这可以让你得到你想要的。
<?php
$formdata['fname'] = trim($_POST['fname']);
$formdata['lname'] = trim($_POST['lname']);
$formdata['email'] = trim($_POST['email']);
$formdata['pwd'] = trim($_POST['pwd']);
$formdata['pwd2'] = trim($_POST['pwd2']);
$formdata['phone'] = trim($_POST['phone']);
$formdata['date'] = $_POST['dateCreated'];
//Checking for empty form values
if (empty($formdata['fname'])) {
$err = 1;
$errfname = "First name is required";
} else if (empty($formdata['lname'])) {
$err = 1;
$errlname = "Last name is required";
} else if (empty($formdata['email'])) {
$err = 1;
$erremail = "Email is required";
} else if (empty($formdata['pwd'])) {
$err = 1;
$errpwd = "Please enter a password";
} else if (empty($formdata['pwd2'])) {
$err = 1;
$errpwd2 = "Please enter a password";
} else if (empty($formdata['phone'])) {
$formdata['phone'] = "N/A";
} else if ($formdata['pwd'] != $formdata['pwd2']) {
//Checking for matching password values
$err = 1;
$errpwd = "Passwords do not match";
$errpwd2 = "Passwords do not match";
} else {
$sql = "INSERT INTO users (fname, lname, email, pwd, phone, dateCreated, admin) VALUES (:fname, :lname, :email, :pwd, :phone, :dateCreated, :admin)";
$stmt = $pdo->prepare($sql);
$stmt->bindValue(":fname", $formdata['fname']);
$stmt->bindValue(":lname", $formdata['lname']);
$stmt->bindValue(":email", $formdata['email']);
$stmt->bindValue(":pwd", $formdata['pwd']);
$stmt->bindValue(":phone", $formdata['phone']);
$stmt->bindValue(":dateCreated", $rightnow);
$stmt->bindValue(":admin", 0);
if ($stmt->execute()) {
$showform = 0;
echo "<p class='error'> Recorded!</p>";
} else {
echo "<p class='error'> Sorry, there was an error!</p>";
}
}
?>