【发布时间】:2012-02-02 16:54:38
【问题描述】:
我正在尝试连续执行两个 sql 语句。编辑:只有第一个文本框“框”正确发布。其他 3 个变量,bubbleWrap、studentAddress1 和 studentAddress2 似乎都是空白的。两条 SQL 语句都在 mysql 工作台中工作。
studentAddress1 和 studentAddress 2 的文本框包含在与框和气泡包装(true/false)单选按钮相同的形式中。如果您愿意,我可以包含该表单,但它非常简单和基本。
AJAX:
$("#needEmptyBoxesForm").submit (function() {
alert('click');
boxes = $("#boxes").val();
bubbleWrap = $("#bubbleWrap").val();
studentAddress1 = $("#studentAddress1").val();
studentAddress2 = $("#studentAddress2").val();
$.post('needEmptyBoxesRequest.php', 'boxes=' + boxes + 'bubbleWrap=' + bubbleWrap + 'studentAddress1=' + studentAddress1 + 'studentAddress2=' + studentAddress2, function (response) {
alert('post');
$("#needEmptyBoxesRequestResults").html(response);
alert (response);
});
return false;
});
处理页面(php)
else {
$boxes = mysql_real_escape_string($_POST['boxes']);
$bubbleWrap = mysql_real_escape_string($_POST['bubbleWrap']);
$studentAddress1 = mysql_real_escape_string($_POST['studentAddress1']);
$studentAddress2 = mysql_real_escape_string($_POST['studentAddress2']);
$email = mysql_real_escape_string($_SESSION["email"]);
$clientId = mysql_real_escape_string($_SESSION["clientId"]);
$sql = "INSERT INTO supplies (`clientId`, `boxesRequested`, `bubbleWrapRequested`)
VALUES ('".$clientId."', '".$boxes."', '".$bubbleWrap."');";
//set results to variables
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
//in case query fails
if (!$result) {
die("Database query failed: " . mysql_error());
}
$sql2 = "UPDATE `clients` SET `studentAddress1`= '".$studentAddress1."',`studentAddress2`= '".$studentAddress2."' WHERE clientId = '".$clientId."';";
//set results to variables
$result2 = mysql_query($sql2);
$row2 = mysql_fetch_array($result2);
//in case query fails
if (!$result2) {
die("Database query failed: " . mysql_error());
}
}
表格代码:
echo
'<form id = "needEmptyBoxesForm">
<table class = "needEmptyBoxes1">
<tr>
<td>
<span class="buttonText">
Our free signature boxes are 24"x18"x16", and double-walled to protect your items. How many boxes would you like delivered?
</span>
</td>
</tr>
<tr>
<td>
<input type="text"
class="boxRequestBlank"
id="boxes"
name="boxes">
</td>
</tr>
<tr>
<td>
<span class="buttonText">
Free tape will be delivered with the boxes.
</span>
</td>
</tr>
<tr>
<td>
<span class="buttonText">
Would you like to purchase bubble wrap for $5?
</span>
</td>
</tr>
<tr>
<td>
<input type="radio"
class="radioButton"
name="bubbleWrap"
value="1">
<span class="buttonText">
Yes
</span>
<input type="radio"
class="radioButton"
name="bubbleWrap"
value="0">
<span class="buttonText">
No
</span>
</td>
</tr>
<tr>
<td>
<img src="images/arrow.png"
class="needEmptyBoxesFormForward1"
id="needEmptyBoxesFormForward1">
</td>
</tr>
</table>
<table class = "needEmptyBoxes2">
<tr>
<td>
<span class="buttonText">
Please confirm your address:
</span>
</td>
</tr>
<tr>
<td>';
$sql = "SELECT A.studentAddress1, A.studentAddress2
FROM clients A
WHERE '".$_SESSION["email"]."' = A.studentEmail";
include "databaseConnection.php";
//Close connection
mysql_close($connection);
echo
'<input
type="text"
class="needEmptyBoxesTextbox"
name="studentAddress1"
value="'.$row["studentAddress1"].'">
<input
type="text"
class="needEmptyBoxesTextbox"
name="studentAddress2"
value="'.$row["studentAddress2"].'">
</td>
</tr>
</table>
<input type="submit"
src="images/arrow.png"
class="needEmptyBoxesFormForward2"
id="needEmptyBoxesFormForward2">
</form>';
【问题讨论】:
-
我有 2 个错误说“mysql_fetch_array():提供的参数不是有效的 MySQL 结果资源....”但我认为这是因为我还没有对结果做任何事情.这会阻止第二个 sql 语句的处理吗?
-
使用 Firefox + Firebug,打开 NET 选项卡并窥探您的请求。
-
您在查询后没有进行任何错误检查。
mysql_query()的手册或此参考问题中概述了如何执行此操作。 -
将
$row2 = mysql_fetch_array($result2)放在if块之后。 -
@shiplu 我不是已经在我当前的代码中这样做了吗?
标签: php javascript jquery ajax post