我試圖讓SQL查詢成爲動態的。PHP選擇所有等於1的變量
這些是我採取的步驟:
步驟1:
//Here the user sets which table fields will be used:
<input type="checkbox" name="id" value="1"> ID
<input type="checkbox" name="name" value="1"> name
<input type="checkbox" name="email" value="1"> email
步驟2:
//Here I see what fields the user wants to use and set a variable to 1
<?php
if (isset($_POST['id']) && $_POST['id'] == "1")
{
$form_id='1';
}
//etc etc
?>
步驟3:
//And finally here's the sql query
$sql = mysql_query("select * from mytable");
現在我需要設置用戶選擇查詢的字段。
例如,如果用戶檢查僅id
和name
,那麼就應該是這樣的:
$sql = mysql_query("select id, name from $table");
什麼是這樣做的最佳方式?
[**在新的代碼,請不要使用'mysql_ *'功能**](HTTP: //bit.ly/phpmsql)。他們不再被維護[並被正式棄用](https://wiki.php.net/rfc/mysql_deprecation)。看到[**粉紅色框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。如果你選擇PDO,[這裏是一個很好的教程](http://j.mp/PoWehJ)。 – h2ooooooo
我發現你的問題有點不清楚,但編輯後我覺得它更容易理解。 – Jonast92