开发者

MySQL won't update from PHP - what is possibly wrong with my code?

Edit:

$rf is blank during if($_POST['email']){ - why though when it's not blank outside of that and how do i fix this?

$rf = $_GET['_id'];
//$_GET['_id']; is generated by ME

if ( ! empty($rf))
{
$mysqli->query("UPDATE cse SET clicks = clicks + 1 WHERE code='" . $rf ."'");
}
//this works. it adds 1 to the counter of clicks column for that referring users row


if($_POST['email']){

    // Requested with AJAX:
    $ajax = ($_SERVER['HTTP_X_REQUESTED_WITH']  == 'XMLHttpRequest');

    try{
        if(!filter_input(INPUT_POST,'email',FILTER_VALIDATE_EMAIL)){
            throw new Exception('Invalid Email!');
        }

        $mysqli->query("INSERT INTO cse
                        SET email='".$mysqli->real_escape_string($_POST['email'])."'");

        if($mysqli->affected_rows != 1){
            throw new Exception('exists in the database.');
        } else {   
          $ecode = add_code($mysqli->i开发者_如何学Gonsert_id); 
        } 
        if(!empty($rf)){
          $mysqli->query("UPDATE cse SET sign_ups = sign_ups + 1 WHERE code='" . $rf ."'");
//this does not work. it should add 1 to the counter of sign_ups column for that referring users row
        }

        if($ajax){
            die(json_encode(array('msg' => $msg)));
        }

    }
    catch (Exception $e){

        if($ajax){
            die(json_encode(array('error'=>$e->getMessage())));
        }

        $msg = $e->getMessage();        
    }
}

Edit: For comments below, yes it is already bound in JS

$.post("./index.php",{email:$('#email').val()},function(r){

            if(r.error){
                $('#email').val(r.error);
                        } else {
                $('#email').val(r.msg);
            }           
            working = false;
        },'json');


Given that none of the numerous comments above have pointed out the obvious - you have no error checking on your queries. If a mysqli query fails, it returns FALSE. If the query succeeds, it returns either TRUE or a result object.

instead of just guessing at what the problem is, why not do the obvious and let mysql tell you exactly what the problem is. Using the following logic everywhere you run a query will make debugging this problem far easier:

$result = $mysqli->query("UPDATE cse SET clicks = clicks + 1 WHERE code='" . $rf ."'");
if ($result === FALSE) {
    die("Mysql error: " . $mysqli->error);
}

Remember - there's only one way for a query succeed, and many many ways for them to fail, even if the SQL statement you're executing is syntactically correct.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜