提问人:Stavros Kasapi 提问时间:11/2/2017 最后编辑:B. DesaiStavros Kasapi 更新时间:11/2/2017 访问量:1290
PHP 错误 未定义的索引 [重复]
PHP errors Undefined index [duplicate]
问:
所以我有一个表格和一些 php 代码来在数据库中输入信息。但是,当我运行 php 文件时,出现以下错误:
注意:未定义索引:第 24 行 C:\xampp\htdocs\temp\insert.php 中的第一个
注意:未定义的索引:最后在 C:\xampp\htdocs\temp\insert.php 第 25 行
注意:未定义的索引:电子邮件在 C:\xampp\htdocs\temp\insert.php 中第 26 行
注意:未定义的索引:第 27 行 C:\xampp\htdocs\temp\insert.php 中的消息
致命错误:未捕获的 PDOException:SQLSTATE[23000]:完整性 约束冲突:1048 列“first”不能为 null C:\xampp\htdocs\temp\insert.php:29 堆栈跟踪:#0 C:\xampp\htdocs\temp\insert.php(29):PDOStatement->execute() #1 {main} 在第 29 行的 C:\xampp\htdocs\temp\insert.php 中抛出
这是我的HTML表格:
<form action="insert.php" method="post">
<label for="first" >Firstname:</label>
<input type="text" name="first" id="first" />
<label for="last" >Surname:</label>
<input type="text" name="last" id="last" />
<label for="email" > Email: </label>
<input type="text" name="email" id="email" />
<label for="message">Message:</label>
<textarea name="message" id="message"> Enter your question here and we will get back
to you as soon as possible </textarea>
<input type="Submit">
</form>
这是我的PHP:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully <br />";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
$query=$conn->prepare("INSERT INTO contacts (first, last, email, message) VALUES(?,?,?,?)");
$query->bindParam(1, $first);
$query->bindParam(2, $last);
$query->bindParam(3, $email);
$query->bindParam(4, $message);
$first=$_POST['first'];
$last=$_POST['last'];
$email=$_POST['email'];
$message=$_POST['message'];
$query->execute();
$conn = null;
echo 'Hi '.$_POST['first'].' ' .$_POST['last'] .' thanks for your interest.</br>';
echo 'We will contact you at '. $_POST['email'].' soon.</br>';
?>
答:
0赞
no0ob
11/2/2017
#1
编辑:刚刚认识到您只需要检查:)感谢@philisset
试试这个代码:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully <br />";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
$query=$conn->prepare("INSERT INTO contacts (first, last, email, message) VALUES(?,?,?,?)");
$query->bindParam(1, $first);
$query->bindParam(2, $last);
$query->bindParam(3, $email);
$query->bindParam(4, $message);
$first=isset($_POST['first'])?$_POST['first']:"";
$last=isset($_POST['last'])?$_POST['last']:"";
$email=isset($_POST['email'])?$_POST['email']:"";
$message=isset($_POST['message'])?$_POST['message']:"";
$query->execute();
$conn = null;
echo 'Hi '.$_POST['first'].' ' .$_POST['last'] .' thanks for your interest.</br>';
echo 'We will contact you at '. $_POST['email'].' soon.</br>';
?>
评论
0赞
Phil
11/2/2017
bindParam
绑定到变量引用。不必在绑定时设置它
0赞
Stavros Kasapi
11/2/2017
不:/相同的错误
0赞
no0ob
11/2/2017
好的,在@Phil的帮助下,我编辑了帖子:)检查新代码
0赞
Phil
11/2/2017
以 OP 显然执行此代码的方式,他们最终会在他们的数据库中得到一堆空行
0赞
no0ob
11/2/2017
是的,可能是命名 HTML 表单输入时存在一些问题......
评论
insert.php
$_POST