尝试使用PDO将表单数据插入到数据库中时仍会收到错误 [英] Keep getting error when trying inserting form data into database with PDO
问题描述
这里是我的代码:
database.php (建立连接,所以我可以使用require)
<?php
$ servername =localhost;
$ username =root;
$ password =root;
$ port = 8889;
$ database =oopdb;
try {
$ conn = new PDO(mysql:host = $ servername; dbname = $ database; port = $ port,$ username,$ password);
//将PDO错误模式设置为异常
$ conn-> setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION);
$ conn-> exec(SET NAMES'utf8');
} catch(Exception $ e){
echoError:。 $ e-> getMessage();
exit;
}
?>
然后我的主PHP文件格式为:
<!DOCTYPE html>
< html>
< head>
< title>带有PDO的表单< / title>
< / head>
< body>
<?php
require(database.php);
if(isset($ _ POST ['submit'])){
try {
//准备并绑定
$ stmt = $ conn - > prepare(INSERT INTO clients(phonenumber,firstname,lastname,address,note)VALUES(:phonenumber,:firstname,:lastname,:address,:note)
$ stmt-> bindParam(':phonenumber',$ phonenumber,PDO :: PARAM_STR);
$ stmt-> bindParam(':firstname',$ firstname,PDO :: PARAM_STR);
$ stmt-> bindParam(':lastname',$ lastname,PDO :: PARAM_STR);
$ stmt-> bindParam(':address',$ address,PDO :: PARAM_STR);
$ stmt-> bindParam(':note',$ note,PDO :: PARAM_STR);
//设置参数并执行
if(isset($ _ POST ['phonenumber'])){$ phonenumber = $ _POST ['phonenumber']; }
if(isset($ _ POST ['firstname'])){$ firstname = $ _POST ['firstname']; }
if(isset($ _ POST ['lastname'])){$ lastname = $ _POST ['lastname']; }
if(isset($ _ POST ['address'])){$ address = $ _POST ['address']; }
if(isset($ _ POST ['note'])){$ note = $ _POST ['note']; }
$ stmt-> execute();
} catch(Exception $ e){
echo$ e;
exit;
}
}
?>
< h2>表单< / h2>
< hr />
< br />
< form action =<?php echo $ _SERVER ['PHP_SELF'];?> method =post>
Number:< input type =textname =numbervalue =/>
< br />< br />
名字:< input type =textname =firstnamevalue =/>
< br />< br />
姓氏:< input type =textname =lastnamevalue =/>
< br />< br />
地址:< input type =textname =addressvalue =/>
< br />< br />
注释:< input type =textname =notesvalue =/>
< br />< br />
< input type =submitname =submitvalue =Submit>
< / form>
< br />
< hr />
< / body>
< / html>
所以我试图将数据从我的表单插入我的数据库,但我不断收到这个错误:
异常'PDOException'带有消息'SQLSTATE [23000]:Integrity
约束违规:1048列'phonenumber'不能为null 'in
/Users/lucasantos/Sites/oop_testing/form.php:32堆栈跟踪:#0
/Users/lucasantos/Sites/oop_testing/form.php(32):
PDOStatement - > execute()#1 {main}
真的不明白这里发生了什么,检查在线错误,找不到任何相关。请帮助。
一些其他信息:
- 我还有一个 id
谢谢!
我看到您使用的html输入名称如下:
< input type =textname =numbervalue =/>
但在您的php代码中,您为电话号码字段使用不同的名称
//设置参数并执行
if(isset($ _ POST ['phonenumber'])){$ phonenumber = $ _POST [ '电话号码']; }
将输入文件的HTML名称更改为phonenumber,看看是否可以修复它。 / p>
First here is my code:
database.php (established connection so I can use with require)
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$port = 8889;
$database = "oopdb";
try{
$conn = new PDO("mysql:host=$servername; dbname=$database; port=$port", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->exec("SET NAMES 'utf8'");
}catch(Exception $e){
echo "Error: " . $e->getMessage();
exit;
}
?>
Then my main PHP file with the form:
<!DOCTYPE html>
<html>
<head>
<title>Forms with PDO</title>
</head>
<body>
<?php
require("database.php");
if(isset($_POST['submit'])){
try{
// prepare and bind
$stmt = $conn->prepare("INSERT INTO clients (phonenumber, firstname, lastname, address, note) VALUES (:phonenumber, :firstname, :lastname, :address, :note)");
$stmt->bindParam(':phonenumber', $phonenumber, PDO::PARAM_STR);
$stmt->bindParam(':firstname', $firstname, PDO::PARAM_STR);
$stmt->bindParam(':lastname', $lastname, PDO::PARAM_STR);
$stmt->bindParam(':address', $address, PDO::PARAM_STR);
$stmt->bindParam(':note', $note, PDO::PARAM_STR);
// set parameters and execute
if(isset($_POST['phonenumber'])){ $phonenumber = $_POST['phonenumber']; }
if(isset($_POST['firstname'])){ $firstname = $_POST['firstname']; }
if(isset($_POST['lastname'])){ $lastname = $_POST['lastname']; }
if(isset($_POST['address'])){ $address = $_POST['address']; }
if(isset($_POST['note'])){ $note = $_POST['note']; }
$stmt->execute();
}catch (Exception $e) {
echo "$e";
exit;
}
}
?>
<h2>The Form</h2>
<hr />
<br />
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
Number: <input type="text" name="number" value="" />
<br /><br />
First Name: <input type="text" name="firstname" value="" />
<br /><br />
Last Name: <input type="text" name="lastname" value="" />
<br /><br />
Address: <input type="text" name="address" value="" />
<br /><br />
Notes: <input type="text" name="notes" value="" />
<br /><br />
<input type="submit" name="submit" value="Submit">
</form>
<br />
<hr />
</body>
</html>
So I am trying to INSERT the data from my form into my database but I keep getting this error:
exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'phonenumber' cannot be null' in /Users/lucasantos/Sites/oop_testing/form.php:32 Stack trace: #0 /Users/lucasantos/Sites/oop_testing/form.php(32): PDOStatement->execute() #1 {main}
Really do not understand what is going on here, I have re checked the code and checked the error online and can't find anything relavant. Please help.
Some additional information:
- I am doing this on local host through MAMP if that matters.
- I also have a column for the id of the row but I did not include it because it is set to auto increment.
Thank you!
I see that you are using the html input name as follows:
<input type="text" name="number" value="" />
But in your php code, you are using a different name for your phone number field
// set parameters and execute
if(isset($_POST['phonenumber'])){ $phonenumber = $_POST['phonenumber']; }
Change your HTML name of the input filed to "phonenumber" and see if that fixes it.
这篇关于尝试使用PDO将表单数据插入到数据库中时仍会收到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!