2016-03-03 64 views
0

例如選擇了MySQL表我有 我有兩個表我datbase月1日是列表1和2是列表2如何通過下拉列表

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

假設用戶在下拉選項選擇列表1,則數據插入在列表1選項 如果用戶選擇列表2然後數據插入到列表2如何做到這一點請大家幫我解決這個問題 感謝

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

很簡單,根據條件語句使用條件語句和2個單獨的查詢。 –

+1

名稱應該選擇不選項 –

+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

用''switch'](http://php.net/manual/en/control-structures.switch.php)聲明會少得多。 – tadman

+0

@tadman我同意這是我以前的想法,但TBH我不記得如何通過記憶來完成這些。我輸入所有的*內存* ;-) –

+0

很高興知道你仍然有它! – tadman

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,那將會更好,所以你可以使用準備好的語句而不是將變量代入查詢。

+0

@tadman在檢查列表後,我沒有看到問題。 – Barmar

+0

@tadman我在說'$ _POST ['select']'。其他問題超出了問題的範圍。 – Barmar

+0

它說'$ title'來自'$ _POST'數據? – Barmar