将表单中的数据输入数据库PDO [英] Entering data from form into Database PDO

查看:120
本文介绍了将表单中的数据输入数据库PDO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习了MySQL,创建了一个表格并使其与数据库一起使用.然后我被告知我应该使用准备好的语句进行PDO,因此我对此进行了一些研究并更改了代码.

I learned MySQL, created a form and had it working with the database. I was then told I should be doing it PDO with prepared statements, so I did some research on that and changed my code.

我现在拥有正确的代码(我认为),但是我无法弄清楚数据是如何输入的.如您在我的代码上看到的,我让数据库在用户提交表单时创建行.但是,数据库只会提取//insert a row//insert another row下语音标记内的内容.

I now have the code right (I think) but I can't figure out how data gets input. As you can see on my code, I have the database creating the rows as the user submits the form. However the database just picks up on whatever is within the speech marks under //insert a row and //insert another row.

例如,现在,如果用户填写并提交了表格,无论他们输入什么信息,我都会在数据库中看到"Joe"和"joe@example.com"等信息.显然,我需要他们的回答,否则我的表格将是不相关的(数据提交也一样).我是否完全错过了商标,或者我错过了一些愚蠢的事情?我曾尝试过更改和研究,但仍在挣扎.这真是新手.

For example, right now if the user completes and submits the form, no matter what information they enter, I just get 'Joe' and 'joe@example.com' etc showing in my database. Obviously I need their answers, otherwise my form would be irrelavant (as would the data submission). Have I totally missed the mark or am I missing something silly? I've tried changing and researching but am struggling. Really new to all this.

FORM:

<form action="testsubmit-pdo.php" method="post">
<label>Student Name :</label>
<input type="text" name="stu_name" id="name" required="required"       placeholder="Please Enter Name"/><br /><br />
<label>Student Email :</label>
<input type="email" name="stu_email" id="email" required="required" placeholder="john123@gmail.com"/><br/><br />
<label>Student City :</label>
<input type="text" name="stu_city" id="city" required="required" placeholder="Please Enter Your City"/><br/><br />
<input type="submit" value=" Submit " name="submit"/><br />
</form>

PHP:

try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);

// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// prepare sql and bind parameters
$stmt = $conn->prepare("INSERT INTO demo (stu_name, stu_email, stu_city)
VALUES (:stu_name, :stu_email, :stu_city)");
$stmt->bindParam(':stu_name', $stu_name);
$stmt->bindParam(':stu_email', $stu_email);
$stmt->bindParam(':stu_city', $stu_city);

// insert a row
$stu_name = "Joe";
$stu_email = "joe@example.com";
$stu_city = "Joeland";
$stmt->execute();

// insert another row
$stu_name = "Mary";
$stu_email = "mary@example.com";
$stu_city = "Maryland";
$stmt->execute();

echo "New records created successfully";
}
catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}
$conn = null;

推荐答案

更改此项-

// insert a row
$stu_name = "Joe";
$stu_email = "joe@example.com";
$stu_city = "Joeland";
$stmt->execute();

// insert another row
$stu_name = "Mary";
$stu_email = "mary@example.com";
$stu_city = "Maryland";
$stmt->execute();

对此-

// insert a row
$stu_name = $_POST['stu_name'];
$stu_email = $_POST['stu_email'];
$stu_city = $_POST['stu_city'];
$stmt->execute();

您的表单会将这些值放在PHP的POST数组中,您可以通过表单中的name属性访问它们.

Your form will place the values in PHP's POST array and you can access them by the name property from the form.

这篇关于将表单中的数据输入数据库PDO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆