第二个SQL语句没有插入/更新,但首先是

时间:2012-01-04 19:29:15

标签: php javascript jquery ajax post

我试图连续执行两个sql语句。编辑:只有第一个文本框“框”正确发布。似乎其他3个变量,bubbleWrap,studentAddress1和studentAddress2都是空白的。这两个SQL语句都在mysql工作台中工作。

studentAddress1和studentAddress 2的文本框包含在与box和bubblewrap(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>';

3 个答案:

答案 0 :(得分:1)

您似乎错过了您构建的查询字符串中的&

$.post('needEmptyBoxesRequest.php', 'boxes=' + boxes + '&bubbleWrap=' + bubbleWrap + '&studentAddress1=' + studentAddress1 + '&studentAddress2=' + studentAddress2,
//-----------------------------------------------------^^-----------------------------^^-------------------------------------^^

答案 1 :(得分:1)

嗯......我在这里有很多不同的做法,它可能会有所作为。

首先,在.post()命令中,我建议使用JSON:

$.post('needEmptyBoxesRequest.php', 
    {'boxes': boxes, 'bubbleWrap': bubbleWrap, 
    'studentAddress1': studentAddress1, 'studentAddress2': studentAddress2}, 
    function (response) { ...

其次,在您的查询中,我嵌入了字符串:

 $sql =  "INSERT INTO supplies (`clientId`, `boxesRequested`, `bubbleWrapRequested`)
            VALUES ('$clientId', '$boxes', '$bubbleWrap');";

我认为您的表单数据没有正确发布。

<强>更新 在查看表单代码之后,我注意到您正在通过jQuery中的ID重新引用输入,但是在表单代码中按名称重新引用。请在输入中添加id属性,看看会发生什么。

答案 2 :(得分:1)

Ajax Post错了。使用 JSON 来映射参数。

$.post(
    'needEmptyBoxesRequest.php', 
    {
        'boxes': boxes,
        'bubbleWrap': bubbleWrap,
        'studentAddress1':studentAddress1,
        'studentAddress2': studentAddress2
    },
    function (response) {
        alert('post');
        $("#needEmptyBoxesRequestResults").html(response);
        alert (response);
    }
);

同样if (!$result)电话之后发布您的mysql_query语句。喜欢这个

$result2 = mysql_query($sql2);
//in case query fails
if (!$result2) { 
    die("Database query failed: " . mysql_error()); 
}
$row2 = mysql_fetch_array($result2);