如何通过下拉列表选择MySQL表

时间:2016-03-03 18:46:45

标签: php mysql

例如我有 我的数据库中有两个表,第一个是list1,第二个是list2

<select>
<option name='select' value="0">Select</option>
<option value="list1">List1</option>
<option value="List2">List2</option>
</select>

假设用户在下拉选项中选择了列表1,然后在list1选项中插入数据 如果用户选择list2然后数据插入到list2如何执行此操作请帮我解决此问题  感谢

mysql_query("INSERT list1 SET title='$titile', subject='$subject'")

这里是完整的代码

 <?php 
 }

    //connect to database

     mysql_connect('localhost','root','');
     mysql_select_db('pdsd');



 // check if the form has been submitted. If it has, start to process the form and save it to the database
 if (isset($_POST['submit']))
 { 
 // get form data, making sure it is valid

 $title = mysql_real_escape_string(htmlspecialchars($_POST['title']));
 $subject = mysql_real_escape_string(htmlspecialchars($_POST['subject']));



 // check to make sure both fields are entered
 if ($title == '' || $subject == '')
 {
 // generate error message
 $error = 'ERROR: Please fill in all required fields!';

 // if either field is blank, display the form again
 renderForm($title, $subject,$date, $error);
 }
 else
 {



 // save the data to the database



$tables = array('list1', 'list2');
if (in_array($_POST['select'], $tables)) {
   mysql_query("INSERT {$_POST['select']}  SET title='$title',subject='$subject'");

}


or die(mysql_error()); 

 echo "<center>Succesfully add</center>";
 echo "<script>setTimeout(\"location.href = 'login.php';\",1500);</script>";
 // once saved, redirect back to the view page

 }
 }
 else
 // if the form hasn't been submitted, display the form
 {
 renderForm('','','','','','','','','','','','','','','','','','','','');
 }




?>

2 个答案:

答案 0 :(得分:3)

正如我在评论中所述,使用条件语句和基于条件语句的2个单独查询以及选择等于所选值的内容。

例如,假设您使用纯PHP并使用表单:

旁注:您需要在此处使用自己的查询,如评论// query for LIST X中所示。

另一个旁注:name属性属于<select>而非<option>

最后一面:我遗漏action=""等于“自我”。因此,如果您希望使用单独的文件,可以向其添加action="handler.php"

<form method="post">

    <select name="select">
    <option value="0">Select</option>
    <option value="list1">List1</option>
    <option value="list2">List2</option>
    </select>

<input type = "submit" name = "submit" value = "Submit">

</form>

<?php 

if(isset($_POST['submit'])){

if(isset($_POST['select']) && $_POST['select'] == 'list1'){

   // query for LIST 1

}

if(isset($_POST['select']) && $_POST['select'] == 'list2'){

   // query for LIST 2

}

if(isset($_POST['select']) && $_POST['select'] == '0'){

   // Do nothing

}

}

这只是一个例子,应该考虑使用准备好的陈述。

您应该阅读的与MySQL有关的其他参考资料:

修改

您还可以使用switch/case声明:

if(isset($_POST['submit'])){

    switch($_POST['select']) {

    case 'list1':
       // query for LIST 1
       break;
    case 'list2':
       // query for LIST 2
       break;

    case '0':
       // Do nothing
       break;
    }

}

答案 1 :(得分:0)

您应首先验证输入是否有效,然后您可以将其替换为SQL。

$tables = array('list1', 'list2');
if (in_array($_POST['select'], $tables)) {
    mysql_query("INSERT INTO {$_POST['select']} SET title='$titile', subject='$subject'") or die(mysql_error());
}

如果变量来自用户输入,请确保正确转义变量$titile$subject,以防止SQL注入(使用mysql_real_escape_string())。如果您使用MySQLI或PDO会更好,那么您可以使用预准备语句而不是将变量替换为查询。