在SQL字符串中使用$ variable? [英] Use a $variable inside a SQL string?
本文介绍了在SQL字符串中使用$ variable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望能够从下拉列表中选择一个类别,并根据类别将其添加到与之等效的任何SQL表中.
I would like to be able to select a category from a dropdown and depending on the category it will add it to whatever SQL table is it equal with.
<?php
$article = $_POST['article'];
$con = mysql_connect("******","******","*******");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("jstaltso_staltsoft", $con);
$sql="INSERT INTO $article (headline, content)
VALUES ('$_POST[headline]', '$_POST[content]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "Content Added!";
echo "<br/>";
echo "<a href='articles.php'><- Back</a>";
mysql_close($con)
?>
我希望变量$ articles可以代替表名.
I want the variable $articles to be in the place of where you out the name of the table.
$sql="INSERT INTO $article (headline, content)
VALUES ('$_POST[headline]', '$_POST[content]')";
因此,无论我在下拉菜单中选择什么,都会将其放置在$ articles中.
So whatever I choose in the dropdown, it will put it at $articles.
推荐答案
尝试:
"INSERT INTO `{$article}` ...."
别忘了清理您的输入! (mysql_real_escape_string,对于初学者而言)
Don't forget to sanitize your input! (mysql_real_escape_string, for starters)
这篇关于在SQL字符串中使用$ variable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文