将表单中的数据输入数据库 PDO

Entering data from form into Database PDO

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

我现在已经有了正确的代码(我认为),但我无法弄清楚数据是如何输入的。正如您在我的代码中看到的那样,我让数据库在用户提交表单时创建行。然而,数据库只是获取 //insert a row//insert another row.

下语音标记中的任何内容

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

表格:

<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 数组中,您可以通过表单中的名称 属性 访问它们。

你post用户输入你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;
}