【问题标题】:$id = $_GET['id'] : variable is not working$id = $_GET['id'] : 变量不工作
【发布时间】:2012-04-19 15:54:27
【问题描述】:

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

错误报告打开时不会报告错误(请注意,粘贴的代码已将它们注释掉),但下面的变量($street2 和 $email)似乎没有通过包含文件保留。当没有注释掉时,回声一切正常。当我用实际的电子邮件地址替换 $email 变量(在包含文件中,未显示)时,一切正常。

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

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

<?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>

【问题讨论】:

  • echo $_GET['id'] 会发生什么?
  • 尝试 is_int 而不是 is_numeric 否则像“4e12a”这样的东西也是有效的
  • connect.php 在哪里?是在正确的路径?也许您可以从 php.ini 激活显示错误(或检查日志)以查看 php 给出的错误
  • scalopus:如果我回显 $id 或 $_GET['id'],它会按照应有的方式回显。换句话说,当 URL 显示 ?id=4 时,回显显示 4。谢谢!
  • 执行print_r($_REQUEST); 并将输出粘贴到问题中。

标签: php include get


【解决方案1】:

您正试图从$_GET 获取 id,然后检查$_POST 的“提交”。你的表格上的方法是什么?我猜你的方法是 POST。

您可以尝试在第 9 行之前回显 $_GET['id'],就像 cmets 中提到的 scalopus 一样,或者尝试将其切换为 $_POST['id']

编辑

将 id 的隐藏字段添加到 cmets 中提到的表单中。底部的 else die 是可选的,一旦您确定事情按预期工作,您就可以将其删除。您不再需要将包含包含在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');
    }
?>

【讨论】:

  • 如果不确定方法,他可以使用 $_REQUEST 代替。
  • KMFK:如果我回显 $_GET['id'],它会很好地回显数字。感谢您的 POST 建议。我尝试重新定义 $id = $_POST['id'] 并且脚本仍然无法发送。是的,我使用 POST 作为表单发送方法。这就是为什么 GET 不能定义 $id 的原因吗?感谢您的意见!
  • scalopus:我将如何使用 $_REQUEST,您为什么认为这可以解决这个问题?谢谢!
  • Maggie - Id 是表单中的字段,还是在查询字符串中(在 url 中)?您能否编辑您的问题以包含表单 html。
  • 好的,所以这个问题似乎是由于我对GET和POST之间的区别缺乏了解。 KMFK 的建议听起来合乎逻辑。我会按照 KMFK 的建议修改我的代码并报告!
猜你喜欢
  • 1970-01-01
  • 2010-10-01
  • 2016-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-27
  • 2020-03-14
  • 1970-01-01
相关资源
最近更新 更多