从表单输入数据到Database PDO

时间:2015-02-20 22:43:08

标签: php mysql forms pdo

我学习了MySQL,创建了一个表单并让它与数据库一起工作。然后我被告知我应该用准备好的语句来做PDO,所以我做了一些研究并改变了我的代码。

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

例如,现在如果用户完成并提交表单,无论他们输入什么信息,我都会得到Joe'和' joe@example.com'等在我的数据库中显示。显然我需要他们的答案,否则我的表格将是无关紧要的(数据提交也是如此)。我完全错过了这个标记还是我错过了一些愚蠢的东西?我尝试过改变和研究,但我正在努力。对这一切真的很新。

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;

2 个答案:

答案 0 :(得分:5)

改变这个 -

// 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属性访问它们。

答案 1 :(得分:3)

您将用户输入的数据发布到您的php文件

<form action="some_php_file.php" method="post">
  <input type="text" name="stu_name">
   <input type="email" name="stu_email">
  <input type="text" name="stu_city">
  <input type="submit" name="submit" value="submit">
</form>

在PHP代码中,首先需要检查是否单击了提交按钮

   //check if submit button is clicked
  If(isset($_POST['submit'])){
    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);

    $stu_name = $_POST['stu_name'];
    $stu_email = $_POST['stu_email'];
    $stu_city = $_POST['stu_city'];
    $stmt->execute();
    echo "New records created successfully";
}catch(PDOException $e){
  echo "Error: " . $e->getMessage();
}
$conn = null;
}