$id = $_GET['id']:变量不起作用


$id = $_GET['id'] : variable is not working

我正在尝试通过以下方式发送电子邮件1) 使用 $_GET['id'] 定义 mysql 行2)从数据库中提取相关信息,包括电子邮件地址和3)向提取的电子邮件地址发送电子邮件。

错误报告

时不会报告任何错误(请注意,粘贴的代码已将其注释掉),但下面的变量($street 2 和 $email)似乎没有通过包含文件保留。当回声没有被注释掉时,它们都可以正常工作。当我将$email变量(在包含文件上,未显示)替换为实际的电子邮件地址时,一切都很好。

我认为要注意的主要事情是第 9 行,如果我定义$id,那么一切正常。但是,如果我尝试使用 $_GET 来定义 $id 变量,那么回显的变量仍然显示正常(意味着数据库查询成功),但包含文件不起作用。

这是我的代码。它位于 HTML 表单的上方。我在代码中包含了一些注释,以便您可以更好地了解哪些有效或无效。

<?php
session_start() or die("Could not start session.");
//error_reporting(E_ALL);
//ini_set('display_errors', '1');
$id = 0;
if(isset($_GET['id']) && is_numeric($_GET['id']))
    $id = (int)$_GET['id'];
//$id=4;
//if Line 9 is not commented out, then the whole script works fine and email is sent
require('connect.php');
$query1 = mysql_query("SELECT street2 FROM prop_one WHERE sellerID='$id'") or die("OOPS: Bad    query1");
$row1 = mysql_fetch_assoc($query1, MYSQL_NUM);
$street2 = $row1[0];
//echo "This is $street2";
$query2=mysql_query("SELECT email FROM account WHERE sellerID='$id'") or die("OOPS: Bad query2");
$row2 = mysql_fetch_assoc($query2, MYSQL_NUM);
$email = $row2[0];
//echo "<br>This is $email";
if (isset($_POST['submit']))
{
include_once("emailSeller_inc.php");
}
?>

这是表格:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" name="emailToSeller">
<fieldset>
  <legend>Your Contact Info
  </legend>
  <label for="conFName">First name*</label>
  <input name="conFName" type="text" id="conFName" value="<?php echo stripslashes(htmlentities($conFName)) ?>" placeholder="required" required = "required" size="35" maxlength="50"/>
<label for="conLName">Last name</label>
<input name="conLName" type="text" id="conLName" value="<?php echo stripslashes(htmlentities($conLName)) ?>" size="35" maxlength="50"/>
<label for="conEmail">Email*</label>
<input name="conEmail" type="email" id="conEmail" value="<?php echo stripslashes(htmlentities($conEmail)) ?>" placeholder="required" required="required" size="35" maxlength="50"/>
<label for="conTel">Phone</label>
    <input name="conTel" type="text" id="conTel" value="<?php echo stripslashes(htmlentities($conTel)) ?>" placeholder="" size="25" maxlength="15"/>&nbsp;e.g., 555.555.5555
</fieldset>
<fieldset style="text-align: center; position: absolute; top: -200; z-index: -1000;">
<input class="teaser" type="submit" name="submit" id="submit" value="Submit"/>
</fieldset>
<fieldset>
  <legend>Your Message
  </legend>
  <label for="conSubject">Subject*</label>
  <input name="conSubject" type="text" id="conSubject" value="<?php echo stripslashes(htmlentities($conSubject)) ?>" placeholder="required" required="required" size="35" maxlength="50"/>
  <label for="conMessage">Message*</label>
  <textarea name="conMessage" type="textarea" id="conMessage" placeholder="required" required="required" cols="50" rows="8" maxlength="400"/><?php echo stripslashes(htmlentities($conMessage)) ?></textarea>
  <label class="teaser">Email</label>
  <input class="teaser" name="validate" id="validate" type="text" autocomplete="off" />
</fieldset>
<fieldset style="text-align: center">
  <input class="button" type="submit" name="submit" id="submit" value="Send email"/>
</fieldset>
   </form>
  </div>

您正在尝试从$_GET获取id,然后检查$_POST中的"提交"。 您的表格上的方法是什么?我的猜测是你的方法是开机自检。

您可以尝试像评论中提到的 scalopus 一样回显第 9 行之前的$_GET['id'],也可以尝试将其切换到 $_POST['id']

编辑

将 id 的隐藏字段添加到表单中,如注释中所述。底部的 else 模具是可选的,一旦您确定事情按预期工作,您就可以将其删除。 您不再需要将包含包装在isset($_POST['submit'])中 - 如果设置了 id,则提交也是如此。 希望这有帮助。

    <?php
    session_start() or die("Could not start session.");
    $id = 0;
    $email = null;
    $street2 = null;
    if(isset($_POST['id']) && is_numeric($_POST['id']))
    {
        $id = $_POST['id'];
        require('connect.php');
        $query = mysql_query("SELECT po.street2, a.email FROM prop_one po JOIN account a ON po.sellerId = a.sellerId WHERE sellerID = '$id' LIMIT 1") or die("OOPS: Bad query " . mysql_error() );
        list( $street2, $email ) = mysql_fetch_row($query);
        //echo "Street2 is $street2";
        //echo "<br>Email is $email";
        include_once("emailSeller_inc.php");
    }
    else
    {
       die( 'no Id set in Post');
    }
?>