dongyin0628 2012-01-06 14:31
浏览 125
已采纳

在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)

?>

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]')";

So whatever I choose in the dropdown, it will put it at $articles.

  • 写回答

3条回答 默认 最新

  • doukanzhuo4297 2012-01-06 14:34
    关注

    Try:

    "INSERT INTO `{$article}` ...."
    

    Don't forget to sanitize your input! (mysql_real_escape_string, for starters)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 一道python难题2
  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备