2012-04-19 64 views
1

1)使用$ _GET ['id']定义mysql行 2)从数据库,包括电子邮件地址和 3)发送电子邮件到提取的电子邮件地址。

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

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

这是我的代码。它位于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> 
+0

echo $ _GET ['id']会发生什么? – scalopus 2012-04-19 15:57:37

+0

尝试is_int而不是is_numeric,否则就像“4e12a”也是有效的 – Mohammer 2012-04-19 16:02:29

+0

其中是connect.php?是在正确的路径?也许你可以激活php.ini中的显示错误(或检查日志)来查看php – kappa 2012-04-19 16:03:45

回答

1

你正试图从$_GET获得ID,然后再检查$_POST为 '提交'。你的表单上的方法是什么?我的猜测是你的方法是POST。

您可以尝试在第9行之前回显$_GET['id'],如评论中提到的scalopus,或尝试将其切换为$_POST['id']

编辑

的ID添加隐藏字段的形式,在注释中提到。在底部的其他死亡是可选的,你可以删除它,一旦你确定事情正如你所期望的那样工作。你不需要再包装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'); 
    } 
?> 
+0

如果不确定方法,他可以使用$ _REQUEST。 – scalopus 2012-04-19 16:06:07

+0

KMFK:如果我回显出$ _GET ['id'],它会回应这个数字。感谢您的POST建议。我尝试重新定义$ id = $ _POST ['id'],但仍然无法发送脚本。是的,我使用POST作为表单发送方法。这就是为什么GET不能定义$ id的原因吗?感谢您的输入! – Maggie 2012-04-19 16:44:07

+0

scalopus:我如何使用$ _REQUEST,为什么你认为这可能会解决这个问题?谢谢! – Maggie 2012-04-19 16:44:40