发布变量未发送

时间:2011-12-10 20:13:07

标签: php variables post

这真让我烦恼,我无法弄清楚。我有一个表格,其中有几个选项由POST发送:

<form method="POST" action="scripts/submit.php"><strong>
    To User: <input type="text" name="ID" size="21" /><br />
    Short Description: <input type="text" name="Item" size="21" /><br />
    Link: <input type="text" name="Link" size="21" /><br />
    Points: <select name="Points">
    <option value="1" selected="selected">1</option>
    <option value="0">0</option><option value="-1">-1</option>
    </select> (1 = Positive, 0 = Neutral, -1 = Negative)<br />
    Text: <br /><textarea name="Text" rows="5" cols="50"/></textarea></strong><br />
    <input type="submit" value="Send" />
</form>

以下是submit.php给我带来麻烦的部分:

<?php
include('functions.php');
Connect();

if(!isset($_SESSION)) 
{ 
session_start(); 
} 
$id_from = $_SESSION['SESS_MEMBER_ID'];
$id_to = Sanitize($_POST['ID']);
$item = Sanitize($_POST['Item']);
$link = Sanitize($_POST['Link']);
$points= Sanitize($_POST['points']);
$text = Sanitize($_POST['Text']);

有人在这看到问题吗?我从除会话1之外的所有变量中得到未定义的索引。

提前致谢。

编辑:如果我有这个:

<?php
include('functions.php');
Connect();
if(!isset($_SESSION)) 
{ 
session_start(); 
} 

$id_from = $_SESSION['SESS_MEMBER_ID'];
$id_to = Sanitize($_POST['ID']);
$item = Sanitize($_POST['Item']);
$link = Sanitize($_POST['Link']);
$points = Sanitize($_POST['points']);
$text = Sanitize($_POST['Text']);

?>

变量填充得很好。如果我添加:

 $id_query=mysql_query("SELECT ID FROM tbl_users WHERE Username = '$id_to'");

 $count=mysql_num_rows($id_query);

 $id_row=mysql_fetch_array($id_query);
 $id_to=$id_row['ID'];

 if ($points> 1 || $points< -1) {
     echo "Nice try";
     exit();
 } else {

 if(!($id_to == $id_from))
 {
 if($count==1)
 {
 mysql_query("INSERT INTO tbl_data (Item, Link, Points, Text, ID_To, ID_From) VALUES ('$item', '$link', '$points', '$text', '$id_to','$id_from')");
 header('Location:?id=submit');
 }
 else
 {
 echo "Nice try1";
 }
 }
 else
 {
 echo "Nice try2";
 }
 }

2 个答案:

答案 0 :(得分:1)

我刚把我的代码放在我的开发服务器上试图测试运行它。由于我不知道你的Sanitize()做了什么,我不能确定这个函数里面发生了什么。

如果您尝试删除Sanitize(),我很确定它会起作用,您必须查看此内容才能找到错误。

我猜你可能会遗漏像($ var,str)这样的字符串来清理字符串。你能告诉我这个功能吗?

编辑:一些轻微的拼写错误。

编辑:做了一些测试并发生了错误,两个代码显示了它。第一个工作,而第二个给我一个空的var_dump。

这个给了我一个完整的var_dump();

<?
function Sanitize($String) { 
$output = mysql_real_escape_string(stripslashes($String));
return $output;
}
if(!isset($_SESSION)) 
{ 
session_start(); 
} 
?>
<form method="post" action=""><strong>
To User: <input type="text" name="ID" size="21" /><br />
Short Description: <input type="text" name="Item" size="21" /><br />
Link: <input type="text" name="Link" size="21" /><br />
Points: <select name="Points"><option value="1" selected="selected">1</option><option value="0">0</option><option value="-1">-1</option></select> (1 = Positive, 0 = Neutral, -1 = Negative)<br />
Text: <br /><textarea name="Text" rows="5" cols="50"/></textarea></strong><br />
<input type="submit" value="Send" />
</form>

<?

$id_from = $_SESSION['SESS_MEMBER_ID'];
$id_to = Sanitize($_POST['ID']);
$item = Sanitize($_POST['Item']);
$link = Sanitize($_POST['Link']);
$points= Sanitize($_POST['points']);
$text = Sanitize($_POST['Text']);


var_dump($_POST);

echo $text;

?>

这个给了我一个空的var_dump

<?
if(!isset($_SESSION)) 
{ 
session_start(); 
} 
?>
<form method="post" action=""><strong>
To User: <input type="text" name="ID" size="21" /><br />
Short Description: <input type="text" name="Item" size="21" /><br />
Link: <input type="text" name="Link" size="21" /><br />
Points: <select name="Points"><option value="1" selected="selected">1</option><option value="0">0</option><option value="-1">-1</option></select> (1 = Positive, 0 = Neutral, -1 = Negative)<br />
Text: <br /><textarea name="Text" rows="5" cols="50"/></textarea></strong><br />
<input type="submit" value="Send" />
</form>

<?

$id_from = $_SESSION['SESS_MEMBER_ID'];
$id_to = Sanitize($_POST['ID']);
$item = Sanitize($_POST['Item']);
$link = Sanitize($_POST['Link']);
$points= Sanitize($_POST['points']);
$text = Sanitize($_POST['Text']);


var_dump($_POST);

echo $text;

?>

答案 1 :(得分:1)

根据您的评论,您有此代码:

$id_query = mysql_query("SELECT ID FROM tbl_users WHERE Username = '$id_to'");
$count = mysql_num_rows($id_query);
$id_row = mysql_fetch_array($id_query);
$id_to = $id_row['ID'];
if ($points > 1 || $points < -1) {

} else {
    if (! ($id_to == $id_from)) {
        if ($count == 1) {
            mysql_query("INSERT INTO tbl_data (Item, Link) VALUES ('$item', '$link')");
            header('Location:?id=submit');
        } else {}
    } else {}
}

我认为问题在于说:

header('Location:?id=submit');

也许您正在使用某种方式进行测试$points大于1或小于-1,$count为1,$id_to$id_from不同然后执行else块并执行(特别是行)header(),然后用户立即重定向。

要检查是否属实,请尝试var_dump($_GET)查看是否有类似的内容:

array (size=1)
  'id' => string 'submit' (length=6)

如果你这样做,也许数据库没有更新,那么就是你需要检查标题之前的mysql_query。

希望这有帮助。