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 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64