php - 在数据库中插入下拉菜单项

标签 php html mysql forms insert

我对 HTML 和 PHP 非常陌生。我正在尝试创建一个下拉菜单以将选择插入到我的数据库中。我已经尝试了几个小时才能让它发挥作用。我有一份客户需要填写的评估表格,他们只能从数据库中已有的工作中进行选择。

评估表包含 JobNo、evalDate、raterName、评分和注释(可选)。 在jobs表中找到JobNo为jobNo(jobs表中小写j,evaluation中大写)

如果您能够将数据库中的数据填充为下拉框选项,则可获得奖励积分。我也一直这样尝试。

请告诉我我遗漏了什么或做错了什么?

<!DOCTYPE html>
<html>
<body>
 <body style="background-color:#FAEBD7;">
<title>CTS Employment Agency | Evaluation</title>  <!-- title for tab on website -->

<center><h1 style="background-color: #FAEBD7;">CTS Employment Agency</h1></center>
<center><p>123 Anywhere St.<br>Huntsville, AL 35649<br>P: (256)555-5555<br>Fax:(256)554-5554<br>E-mail: questions@ctsemployment.com</p></center>
<center><h3>Evaluation Form</h3></center>

<center><p>Please take a moment to fill out the evaluation form concerning the worker that was provided to you.<br>
Please leave a rating from 1(unsatisfactory) to 5(excellent) for the worker along with a comment box for additional information.
<br> If you have any questions, please feel free to send us an email or call us!</p></center>

<form action="php_formStore.php" method="$Post_"> <!-- beginning of form -->
 <br>
 <center>

Worker First Name: <input type="text" name="wFName">

Middle: <input type="text" name="wMinit">

Last: <input type="Text" name="wLName">

SSN <input type="text" name="snn" placeholder="000-00-0000">
<br><br>


 Job No: 
<select name="JobNo">
  <option value="46">46</option>
  <option value="47">48</option>
  <option value="48">48</option>
</select>

//<input type="text" name="JobNo">  //this works fine, why doesn't the select box work?


 Worker's Job Title: <input type="text" name="title">

Rating: <input type="text" name="rating" placeholder="Rate from 1-5">
<br><br>    

Evaluator's Name: <input type "text" name="raterName">

Evaluation Date:   <input type="date" name="evalDate">
     <br><br>  

 <!-- comment box -->
<textarea name="comment"  rows="8" cols="60"> 
(Optional)
Leave any additional comments here. 
</textarea><br>

<input type="submit" value="Submit">  <!-- The submit buttom -->
<input type="reset">
</center>
</form>


</body>
<html>

这是 PHP 文件

<?php
$servername = "localhost";

$username = "root";

$password = "";

$db="cts employment agency";  //Database Name 
$con=mysqli_connect($servername, $username, $password, $db);

//check connection
if(mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

       $Job_No = $_GET['JobNo'];
       $raterName = $_GET['raterName'];
       $evalDate = $_GET['evalDate'];
       $rating = $_GET['rating'];
       $comment = $_GET['comment'];

       echo "<br><br><br><br>";
       echo "<center><h2>Thank you for taking the time to fill out this evaluation<br>";
       echo "Your feedback helps us to provide you with the highest quality employees<br>";

       echo "If you have any questions, please e-mail us at: questions@ctsemployment.com or call: (256)555-5555</h2></center>";

      mysqli_query($con, "INSERT INTO evaluation (JobNo, evalDate, raterName, rating, comment)
      VALUES ('$Job_No', '$evalDate', '$raterName', '$rating', '$comment')");

      mysqli_close($con);
?>



  [1]: /image/2mJGC.png

最佳答案

在您的 php 脚本中,您尝试使用 $_GET 访问 $_POST 变量。

如果您想按照表单上的建议使用 post,请将声明更改为:

 <form action="php_formStore.php" method="post"> <!-- beginning of form -->

然后在您的 php 脚本中,将 $_GET 变量更改为 $_POST,如下所示:

   $Job_No = $_POST['JobNo'];
   $raterName = $_POST['raterName'];
   $evalDate = $_POST['evalDate'];
   $rating = $_POST['rating'];
   $comment = $_POST['comment'];

您的代码也面临着 SQL 注入(inject)的高风险。您应该研究使用准备好的语句来解决这个问题。下面是相关指南:

https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php

这对于保持数据完整性和安全性、防止不必要的数据访问至关重要。

关于php - 在数据库中插入下拉菜单项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59148977/

相关文章:

php - 创建重定向到另一个 URL 的 WordPress 页面

javascript - Jquery 不工作,但我想一切都好

javascript - JavaScript 中未定义的输入字段值

mysql - 在 MySQL 中创建一个用户定义的函数来处理 auto_increment

php - 用户注册后使用循环插入语句

javascript - 如何使用php检查某个元素是否存在

php - 安装 pear 时出现已弃用的错误

MySQL 连接运算符

javascript - POST-GET session 变量 (javascript-php) -javascript 采用前一个变量,但不是最后创建的变量

javascript - 使用 jQuery 添加类到列表