php变量已声明,但不会插入数据库

时间:2011-11-28 17:26:19

标签: php mysql variables insert twilio

在我的一个脚本中,php $ _POST正确地声明为变量。当我回显变量时,它会正确显示,但是,当我检查数据库时,它没有正确插入。它显得空白。所以我的猜测是它不是变量的问题,因为它被正确地回调。插入时我不知道问题是什么。

如果需要,这是一个twilio应用程序,因此应用程序从twilio应用程序中提取录制URL,并且我从您录制的表单中传递值。我只是为那些熟悉twilio应用程序的人说明这一点。

$sayid = $_SESSION['id'];
$hearid = "01";
$sayurl = $_REQUEST['RecordingUrl'];
$topic = $_POST['topic'];



mysql_query("INSERT INTO says (say, hear, sid, time_sent, happy) 
        VALUES('$sayid', '$hearid', '$sayurl.mp3', now(), '$topic' )"); 


echo $topic;

我愿意接受建议,我会尝试,并告诉你他们是否有效。

编辑如果我将可变$主题更改为=“无论什么”它会正确地向数据库发送“无论什么”,但是一旦我将其更改为$ _POST ['topic'],它就会再次发布空白值。但是,如果我在任何地方回显$ topic,它将发布正确的$ _POST值

编辑#2好的新更新,所以我将变量声明改为$ sayid =“20”; $ hearid =“01”; $ topic = $ _POST ['topic']; $ sayurl = $ _REQUEST ['RecordingUrl'];而不是$ sayid =“20”; $ hearid =“01”; $ sayurl = $ _REQUEST ['RecordingUrl']; $ topic = $ _POST ['topic'];我现在得到了php脚本CREATING 2 NEW RECORDS的输出,其中一个除了$ topic之外还有其他所有内容,而另一个只有$ topic,所以它们显然是在发布所有字段,但是在两个不同的行!大声笑哇到底是怎么回事

3 个答案:

答案 0 :(得分:2)

使用:

$sayid = mysql_real_escape_string($_SESSION['id']);
$hearid = "01";
$sayurl = mysql_real_escape_string($_REQUEST['RecordingUrl']);
$topic = mysql_real_escape_string($_POST['topic']);
$topic2 = mysql_real_escape_string($_POST['topic']);

$query = "INSERT INTO says
            (say, hear, sid, time_sent, happy, sad)
          VALUES
            ('$sayid', '$hearid', '$sayurl.mp3', now(), '$topic', '')";

if (!mysql_query($query)) {
  // Handle error here
  // e.g.
  // echo "Oh no! The query failed! Error: ".mysql_error();
}

这可能会解决问题,并可以防止SQL注入攻击。

答案 1 :(得分:1)

你错过了“悲伤”的价值。您可以使用mysql_error()查看是否在执行上一个SQL语句时出错。

答案 2 :(得分:0)

<form action="test.php" type="post">
<input type="text" name="topic" />
</form>

<?php
$input = $_POST['topic'];
echo $input;
mysql_query("INSERT INTO balbal VALUES ('".$input."')") or die (mysql_error());
?>

为你工作?

相关问题