0

我完全不明白为什么这里的 php 没有向 IF 语句发送一个简单的变量。我尝试在 IF 语句中回显 $to_who 变量,但它什么都没有。因此,INSERT 语句没有为第一列插入任何内容。$to_who 变量在 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);

 }

谢谢

4

1 回答 1

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);

}
于 2013-10-27T03:54:29.683 回答