php $id = $_GET['id'] : 变量不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10231979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
$id = $_GET['id'] : variable is not working
提问by Maggie
I am trying to send email by 1) Using $_GET['id'] to define the mysql row 2) Extract the relevant info from the database, including the email address and 3) Send email to the extracted email address.
我正在尝试通过 1) 使用 $_GET['id'] 定义 mysql 行 2) 从数据库中提取相关信息,包括电子邮件地址和 3) 将电子邮件发送到提取的电子邮件地址来发送电子邮件。
No errors are reported with error reporting on (note that pasted code has them commented out), but the variables below ($street2 and $email) don't seem to be retained via the include file. The echo's all work fine when they are not commented out. When I replace the $email variable (on the include file, not shown) with an actual email address, everything sends fine.
没有错误报告并报告错误(请注意,粘贴的代码已将它们注释掉),但下面的变量($street2 和 $email)似乎没有通过包含文件保留。当它们没有被注释掉时,回声一切正常。当我用实际的电子邮件地址替换 $email 变量(在包含文件中,未显示)时,一切正常。
I think the main thing to note is in Line 9, if I define the $id, then everything works fine. However, if I try to use $_GET to define the $id variable, then the echoed variables still show up fine (meaning the db queries are successful), but the include file doesn't work.
我认为要注意的主要事情是在第 9 行,如果我定义了 $id,那么一切正常。但是,如果我尝试使用 $_GET 来定义 $id 变量,则回显变量仍然显示正常(意味着 db 查询成功),但包含文件不起作用。
Here is my code. It is placed above the HTML form. I've included some comments within the code so you can have a better idea of what has worked or not.
这是我的代码。它位于 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");
}
?>
Here is the form:
这是表格:
<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"/> 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>
回答by kmfk
You are trying to get the id from $_GET, then later checking the $_POSTfor 'submit'. What is the method on your form? My guess is your method is POST.
您正在尝试从 获取 id $_GET,然后再检查$_POST“提交”。你的表格上的方法是什么?我的猜测是你的方法是 POST。
You can either try echo'ing out $_GET['id']before line 9 like scalopus mentioned in comments, or try switching it to $_POST['id'].
您可以$_GET['id']像评论中提到的 scalopus 一样尝试在第 9 行之前回显,或者尝试将其切换到$_POST['id'].
edit
编辑
Add the hidden field for id to the form as mentioned in comments. The else die at the bottom is optional and you can remove it once you are sure things are working as you expect. You dont need to wrap the include in the isset($_POST['submit'])any more - if the id is set, then submit is too. Hope this helps.
如评论中所述,将 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');
}
?>

