【问题标题】:Second SQL statement is not inserting/updating, but first is第二条 SQL 语句不是插入/更新,但首先是
【发布时间】: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


【解决方案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 属性,看看会发生什么。

【讨论】:

  • 你似乎是对的,在调查了这个之后,bubbleWrap、studentAddress1 和 studentAddress2 没有正确发布。
  • 我已经添加了 JSON 并嵌入了字符串,但似乎仍然只有“boxes”值可以通过,所有其他值都被擦除为空白。
  • 那我觉得你需要把表单代码贴出来让我们看看。
  • 很好,我想,我剪掉了一些中间的东西,所以格式可能看起来有点奇怪
  • 确实,您的表单代码有问题;此外,您需要在您的 $row[] 数据上使用 htmlspecialchars() 以防止您的网页出现异常。
【解决方案2】:

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);

【讨论】:

  • 好的,如上所述,我已将其切换为 JSON,但它仍在删除除“框”之外的所有内容的值。我不明白您所说的“还将 if (!$result) 语句放在 mysql_query 调用之后”是什么意思。我想我已经拥有它了。
  • $row2 = mysql_fetch_array($result2); 语句介于 mysql_queryif 之间。
  • 这似乎没有任何改变。
  • 我认为数据没有正确发布,有什么想法吗?我认为 sql 有效,但变量不包含任何内容。
【解决方案3】:

您构建的查询字符串中似乎缺少&amp;

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

【讨论】:

  • 好的,这很有帮助,我不敢相信我忘记了 &。我已经添加了它们,但你帮助我意识到 $bubbleWrap 也没有发布
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-02
  • 2010-09-24
  • 1970-01-01
  • 2011-12-13
  • 1970-01-01
  • 1970-01-01
  • 2016-12-21
相关资源
最近更新 更多