douke1954 2013-02-02 14:19
浏览 29
已采纳

发布变量值而不是变量名称

I'm trying to get the entry from a textbox code to go to a php form and have the php form send it to a database. THe problem I'm having is instead of posting the textbox value it posts $code. I'm using mysql and php.

PHP:

<?
if( $_POST )
{
$username="***";
$password="***";
    $con = mysql_connect("***",$username,$password);

    if (!$con)
    {
        die('Could not connect: ' . mysql_error());
    }

    mysql_select_db("inmoti6_mysite", $con);

    $code = $_POST['code'];


    $code = htmlspecialchars($code); 


    $query = 'INSERT INTO `storycodes`.`storycodes` (`code`) VALUES ("$code");';


    mysql_query($query);

    echo "<h2>Thank you for your Comment!</h2>";

    mysql_close($con);
}
?>

Doubt this is the issue, but here's the html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Database</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="/scripts/database2.php">
  <label>Code:
  <input name="code" type="text" id="code" value="" size="45" />
</label>
  <p>
    <label>
    <input type="submit" name="submit" id="submit" value="Submit" />
    </label>
  </p>
</form>
</body>
</html>
  • 写回答

2条回答 默认 最新

  • douningchang3610 2013-02-02 14:23
    关注

    You need to use double quotes for the variables to be recognized in a string, so change:

    $query = 'INSERT INTO `storycodes`.`storycodes` (`code`) VALUES ("$code");';
    

    to

    $query = "INSERT INTO `storycodes`.`storycodes` (`code`) VALUES ('$code');";
    

    You also have an sql injection problem; I recommend that you switch to PDO (or mysqli) with prepared statements and bound variables. At the very least you should use mysql_real_escape_string on your variables before you insert them in the database but as you can see in the manual, the mysql_* functions are deprecated.

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

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题