尝试使用下拉列表向数据库添加数据时“断开连接”

时间:2014-02-19 02:04:34

标签: php forms drop-down-menu mysql-insert-id

我正在尝试使用表单中“输入类型”的混合将数据添加到数据库中。我可以通过“type = text”和“text area”标签将数据输入到数据库中但不能填充并使用“选项1”,“选项2”的“选择/下拉”标签输入“答案”和“选项3”选项。

将3个选项视为问卷调查的一部分。您输入3个选项,然后选择哪个是正确的。

我提供了两个php文件。我希望你能理解我的问题并尽力帮助你。谢谢。

文件1:questions_menu.php

    <form action="question-added.php" method="post">
    <table border="0" name"form_table">
      <tr>
        <td>Question ID</td>
        <td><input type="questionID" name="text" maxlength="30" size="30"></td>
      </tr>
      <tr>
        <td>Question Description</td>
        <td><textarea name="description" rows="4" cols="50"></textarea></td>
      </tr>
       <tr>
        <td>Option 1</td>
        <td><input type="text" name="option1" maxlength="30" size="30"></td>
      </tr>
      <tr>
        <td>Option 2</td>
        <td> <input type="text" name="option2" maxlength="30" size="30"></td>
      </tr>
      <tr>
        <td>Option 3</td>
        <td><input type="text" name="option3" maxlength="30" size="30"></td>
      </tr>
       <tr>
        <td>Answer</td>
        <td><select name="dropdown"> <option value='option1'>Option 1</option> <option value='option2'>Option 2</option> <option value='option3'>Option 3</option> </select></td>
      </tr>
       <tr>
        <td colspan="2"><p>
          <input type="submit" value="Add Question">
        </p></td>
      </tr>
    </table> 

</form>    

    <?php
$username = "root";
$password = "";
$hostname = "localhost";
$database = "basketball_database";
$table = "question_bank";

$con = mysql_connect($hostname, $username, $password)
 or die("Unable to connect to MYsql");
 //echo "Connected to mysql<br>";

 mysql_select_db("$database")
 or die("Could not select Basketball_database");
 //echo "Connected to database";

 //update when update button pressed
 if(isset($_POST['update'])){
     $UpdateQuery = "UPDATE $table SET question_description='$_POST[description]', option_a='$_POST[option1]', option_b='$_POST[option2]', option_c='$_POST[option3]', answer='$_POST[dropdown]', question_id='$_POST[questionID]'  WHERE question_id='$_POST[hidden]'";

     mysql_query($UpdateQuery, $con);

 };//end of if statement

  //delete when delete button pressed
 if(isset($_POST['delete'])){
     $DeleteQuery = "DELETE FROM $table WHERE question_id='$_POST[hidden]'";

     mysql_query($DeleteQuery, $con);

 };//end of if statement



$mysql = "SELECT * FROM $table";



 $mydata = mysql_query($mysql,$con);

 //create table
 echo "<table border=1
 <tr>
 <th>Question ID</th>
 <th>Question Description</th>
 <th>Option 1</th>
 <th>Option 2</th>
 <th>Option 3</th>
 <th>Answer</th>
 <th>Picture</th>
 <th>Video</th>
 </tr>";

 //insert data into rows
 while($records = mysql_fetch_array($mydata)){
     echo "<form action=question_menu.php method=post>";
     echo "<tr>";
     echo "<td>"."<input type=text name=questionid size=10 value=".$records['question_id']." </td>";
     echo "<td>"."<textarea name=description rows=2 cols=25>".$records['question_description']."</textarea>"."</td>";
     echo "<td>"."<input type=text name=option1 size=18 value=".$records['option_a']." </td>";
     echo "<td>"."<input type=text name=option2 size=15 value=".$records['option_b']." </td>";
     echo "<td>"."<input type=text name=option3 size= 15 value=".$records['option_c']." </td>";
     echo "<td>"."<input type=text name=answer size=15 value=".$records['answer']." </td>";

     echo "<td>"."<input type=hidden name=hidden value=".$records['question_id']." </td>";

     //update button
     echo "<td>"."<input type=submit name=update value=Update"." </td>";
     //delete button
      echo "<td>"."<input type=submit name=delete value=Delete"." </td>";


     echo "</tr>";  

     echo "</form>";//end form

 } echo "</table>";

调用文件2时。我得到“断开连接”,这意味着输入没有添加到数据库中。

文件2:question-added.php

    <?php
$username = "root";
$password = "";
$hostname = "localhost";
$database = "basketball_database";
$table = "question_bank";

mysql_connect($hostname, $username, $password)
 or die("Unable to connect to MYsql");
// echo "Connected to mysql<br>";

 mysql_select_db("$database")
 or die("Could not select Basketball_database");
 //echo "Connected to database";

 $mysql = "INSERT INTO $table(question_description, option_a, option_b, option_c, answer) VALUES('$_POST[description]','$_POST[option1]','$_POST[option2]','$_POST[option3]','$_POST[dropdown]')";

 if(!mysql_query($mysql))
 die("Disconnected");

 mysql_close();

?>

我希望很快能收到回复。

我是使用php的新手。

已更改

mysql_connect($hostname, $username, $password)

$con = mysql_connect($hostname, $username, $password)

我添加

echo mysql_errno($ con)。 “:”。 mysql_error($ con)。 “\ n” 个;

到我的代码并收到 - &gt;

1062:键'user_id'重复输入'0'

0 个答案:

没有答案