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

时间:2019-12-03 01:28:23

标签: php html mysql forms insert

我是HTML和PHP的新手。我正在尝试创建一个下拉菜单,以将选择内容插入到我的数据库中。我已经尝试了好几个小时才能使它正常工作。我有一个客户需要填写的表格,用于评估,他们只能从数据库中已经存在的工作中进行选择。

评估表具有JobNo,evalDate,raterName,等级和注释(可选)。 在作业表中找到的JobNo为jobNo(作业表中的小写j,在评估中为大写)

奖励积分,如果您可以选择它作为下拉框的选择来填充数据库中的数据。我也永远尝试过。

请告诉我我在想什么或做错了什么?

<!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]: https://i.stack.imgur.com/2mJGC.png

3 个答案:

答案 0 :(得分:0)

在您的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注入的威胁。您应该研究使用准备好的语句来解决此问题。可以在下面看到此指南:

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

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

答案 1 :(得分:0)

首先将[]添加到您的名字

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

然后到您的Php文件

$Job_No= "";
    foreach ($_POST['JobNo'] as $select) {
        $Job_No = $select;
    }

要从数据库中填充数据作为下拉框选择,此链接可能会为您提供帮助

Adding options to select with javascript

How do I pass variables and data from PHP to JavaScript?

答案 2 :(得分:0)


       $Job_No= "";
    foreach ($_POST['JobNo'] as $select) {
        $Job_No = $select;
    };
       $raterName = $_POST['raterName'];
       $evalDate = $_POST['evalDate'];
       $rating = $_POST['rating'];
       $comment = $_POST['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);