变量没有结转

时间:2013-10-27 03:19:32

标签: php mysql

我无法弄清楚为什么这里的php不会向IF语句发送一个简单的变量。我尝试在IF语句中回显$ to_who变量,但它没有任何结果。因此,INSERT语句不会为第一列插入任何内容。 $ to_who var在IF语句之前有一个数字。

$to_who = $_GET['id'];
require('connect.php');

echo $to_who;


$to_who_search = "SELECT * FROM donorstable WHERE donor_id = '$to_who'";

$to_who_raw = mysql_query($to_who_search);

$name = mysql_result($to_who_raw , 0 , 'first_name') . ' ' .  mysql_result($to_who_raw , 0 , 'last_name') ;
$email =  mysql_result($to_who_raw , 0 , 'email');


if(isset($_POST['submit1'])){
    require('connect.php');

//gets from data

$getsubject = $_POST['subject'];
$getmessage = $_POST['message'];

//Gets user's email from table

$getformsql = "SELECT * FROM donorstable WHERE donor_id = '$userid'";
$getfrom = mysql_query($getformsql);

//builds email
$to = $email;
$subject = $getsubject;
$message = $getmessage;
$from = mysql_result($getfrom , 0  , 'email');
$headers = "From:" . $from;
//mail($to,$subject,$message,$headers);
echo "Mail Sent.";

$inserstatement = "INSERT INTO messages VALUES ('$to_who' , '$from' ,'$subject', '$message' , '0' , '')";

mysql_query($inserstatement);

 }

由于

1 个答案:

答案 0 :(得分:1)

正如我在评论中所说,$ to_who只有来自另一个页面(作为查询字符串)才有价值,但是你的代码在if之后(isset($ _ POST ['submit1'])){不会工作,因为使用GET协议。如果您在表单上有这个ID,您说$ to_who变量将为空,因此您的查询$ to_who_raw将不会有结果,除非您的数据库中有某些内容与donor_id =''

此外,如果您来自评论中所述的其他页面,则必须将$_POST中使用的所有变量设为$_GET

因此,为了使两个协议都按照您的说法运行,您必须稍微更改一下代码。所以,它会是这样的:

require('connect.php');

$to_who = $_REQUEST['id'];

echo $to_who;

//Here I'm guessing that you have all variables send 
// by your FORM OR in a querystring like somepage.php?id=1&subject=adasdad&message=asdasdads
if( isset($_POST['submit1']) || isset($_GET['id'])  ){

$to_who_search = "SELECT * FROM donorstable WHERE donor_id = '$to_who'";
$to_who_raw = mysql_query($to_who_search);

$name = mysql_result($to_who_raw , 0 , 'first_name') . ' ' .  mysql_result($to_who_raw , 0 , 'last_name') ;
$email =  mysql_result($to_who_raw , 0 , 'email');

$getsubject = $_REQUEST['subject']; //see my previous comment
$getmessage = $_REQUEST['message']; 

$getformsql = "SELECT * FROM donorstable WHERE donor_id = '$userid'";
$getfrom = mysql_query($getformsql);

//builds email
$to = $email;
$subject = $getsubject;
$message = $getmessage;
$from = mysql_result($getfrom , 0  , 'email');
$headers = "From:" . $from;
//mail($to,$subject,$message,$headers);
echo "Mail Sent.";

$inserstatement = "INSERT INTO messages VALUES ('$to_who' , '$from' ,'$subject', '$message' , '0' , '')";

mysql_query($inserstatement);

}