开发者

Periodic failure when editing or creating in SQL using PHP

I have a problem which I can't really figure out. I am tranfering information from a form, via JSON, to a PHP file which is handling the database call.

When trying it on my apache server it works aprox 75% of the time. The last 25% nothing happens. The form-window will close as ment to, and the page will reload, but the information hasn't been put into the DB.

When trying it at my domain, nothing happens at all. Same code though (I have another DB_connection file, with domain info, which works other places).

I'd like to have my code reviewed, to see if I have made a major mistake. Would love to have it 100% working.

As I said I have both a creation and an edit. I will post the edit form + php content here. If you need to see the creation, please tell me.

Javascript function, called when pushing the edit key (a test will be performed on the form fields before):

function xmlHTTPEdit开发者_如何学JAVA() {
    var product_ID = document.forms["addForm"]["product_ID"].value;
    var product_Name = document.forms["addForm"]["product_Name"].value;
    var product_Price = parseInt(document.forms["addForm"]["product_Price"].value);
    if(window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    } else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            alert(xmlhttp.responseText);
        }
    }
    xmlhttp.open("POST", "../db/productEdit.php", true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send("createID=" + product_ID + "&createName=" + product_Name + "&createPrice=" + product_Price);
}

My php-file, updating DB, using the new data:

<?php
    include('DB_Connect.php');

    $product_ID = trim($_POST['createID']);
    $product_Name = trim($_POST['createName']);
    $product_Price = trim($_POST['createPrice']);

    $sql = "UPDATE products SET productName = '$product_Name', productPrice = '$product_Price' WHERE productID = '$product_ID'";

    mysql_query($sql);
?>

I really hope some of you can help me finding this flaw in my code and maybe help me with an explanation, why it works sometimes at localhost and never at my domain.

Thanks. /Pyracell


The fact that it works sometimes makes me think there's something MySQL is unhappy about. Since PHP no longer throws MySQL errors as exceptions, you'll need to manually fetch the error.

Calling and echoing `mysql_error immediately after the query will display any resulting error, though a more appropriate solution for production would be to have it log to file rather than output to the user.


Try writing the data to a file rather than the database. Ajax is less solid than a database connection. I'd look there first. But isolate the two from each other for testing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜