Already exist not working

Still not clear (to me) what exactly $db is. Could you:

echo get_class($db);

And tell us what type of connection object it is?

$allClicked = $_POST['all_clicked'];

Is going to result in $allClicked being a string such as ‘1’ instead of an integer. Might be better to be explicit.

$allClicked = $_POST['all_clicked'] ? 1 : 0;

So now we know for sure that $allClicked will always be 0 or 1.

And finally, make sure error reporting is enabled by adding this to the top of your file:

error_reporting(E_ALL);

oh i think my english is confusing, the code works, except for the already exist one,n that one doesnt work
and the ony select i see is for the already exist, update is for the same table, it has several columns.

with the code above is there a way i can merge the insert and select so it will be if doesnt exist insert but if exist return error

this

// exist
$query = "SELECT all_clicked FROM " . $DBPrefix . "buttonclicked WHERE all_clicked = '1' AND auction_id = :auction";
$params = array();

    $params[] = array(':auction', $id, 'int');
    $db->query($query, $params);
    if ($db->numrows() > 0)
    {
    $errmsg = $ERR_0099;
    }

displays the error message, but only after submitting instead of returning with error

You could set the auction_id column to be a unique field, run the insert, and you’ll get an error if the table already has a row with that value in it. If you don’t get an error, then it must have inserted the new row.

But your code doesn’t actually work that way - you do the SELECT after you’ve inserted the row, so unless the insert fails, you’re always going to get a result. Surely if you only want one row for each auction_id value, you should do the select first, and then only do the insert if you get no results?

All this relies on you checking for errors after you’ve run the queries. Your code doesn’t seem to do that, unless the framework is doing it for you.

this actually shows the error msg

$query = "SELECT all_clicked FROM " . $DBPrefix . "buttonclicked WHERE all_clicked = '1' AND auction_id = :auction";
    $params = array();
    $params[] = array(':auction', $id, 'int');
    $db->query($query, $params);
    if ($db->numrows() > 0)
    {
    $errmsg = $ERR_0099;
    }

but it still submit

I’m confused - you do the SELECT at the end, after you’ve done the insert and the update. If you want to do the select first to see if it’s already present, surely you have to do that before the insert?

yeah u right, stuff got me all confuse at first but then it seem to submit the form even if its has checked and see that it already exist below is the layout again i believe am suppose to use if and else to make that happen

if (isset($_POST['action']) && $_POST['action'] == 'update')
        {
        
    
    //update balance    
    $query = "UPDATE " . $DBPrefix . "users u SET u.balance = :balance + balance WHERE u.id IN (SELECT b.bidder FROM " . $DBPrefix . "bids b    
    WHERE b.auction = :auction)";
    $params = array();
    $params[] = array(':balance', $system->input_money($_POST['balance']), 'float');
    $params[] = array(':auction', $id, 'int');
    $db->query($query, $params);


    if (isset($_POST['action']) && $_POST['action'] == 'update') 
    {
    $query = "SELECT all_clicked FROM " . $DBPrefix . "buttonclicked WHERE all_clicked = '1' AND auction_id = :auction";
    $params = array();
    $params[] = array(':auction', $id, 'int');
    $db->query($query, $params);
    if ($db->numrows() > 0)
    {
    $errmsg = $ERR_0099;
    }
        
        else{
        //insert button click
        $query = "INSERT INTO " . $DBPrefix . "buttonclicked (auction_id, all_clicked) VALUES(:auction_id, :all_clicked);";
        $params = array();
        $params[] = array(':auction_id', $id, 'int');
        $params[] = array(':all_clicked', $_POST['all_clicked'], 'str');
        $db->query($query, $params);    
            

    
    header('location: listusers.php?PAGE=' . intval($_POST['offset']));
            exit;
}
    
    }}

still not working well still submit and then shows the error msgs

ok done was suppose to put the if before the update

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.