Already exist not working

hello all am trying to make an already exist code but it doesnt seem to be working code below

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

    $params[] = array(':all_clicked', $_POST['all_clicked'], 'int');
    $params[] = array(':auction', $id, 'int');
    $db->query($query, $params);

plz what am i doing wrong here, thanks

We have no idea what $db->query() is and how does “not working” look like. Therefore we cannot tell you anything.

It appears you are passing parameters into what is not a prepared statement.
You should first prepare the statements by giving it the query.
After that you execute the statements giving it the parameters.
The $params array is multidimensional, that is not how an array of parameters should be structured.
Where is the variable $DBPrefix defined?

thats how i connect to my database

I think I remember that the OP is using some kind of template system for these queries.

Are you sure this is correct?

$params[] = array(':all_clicked', $_POST['all_clicked'], 'int');

I’m guessing that “all_clicked” is a checkbox (but it is just a guess), so why would you pass it as an int, which I’m guessing means “integer”? If it is a checkbox, it’ll either be blank (in which case you might need to deal with it not existing) or “on”, unless you set a value for it.

1 Like

hi its more of an hidden field that get summited when a button is clicked, am trying to make it return error when that column all_clicked is filled to return as error already exist.

and if not int what should i use?

I don’t know. What’s the value in $_POST['all_clicked'], that should give an idea. And what values are in the database column?

mostly 1 and 0

1 means clicked
0 means notclicked

OK, then maybe “int” is correct for the data type after all.

So, what’s actually going wrong? What does it do that it should not, or not do that it should? Do you get error messages?

That would have been useful information for them to supply in the topic.

thats just the issue am even having, am not getting any error, if i was, that would have been nice, it just keep passing through and submiting the form, this is the full form script

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

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

        //update button click
    $query = "UPDATE " . $DBPrefix . "buttonclicked SET all_clicked = :all_clicked 
    WHERE auction_id = :auction";
    $params[] = array(':all_clicked', $_POST['all_clicked'], 'int');
    $params[] = array(':auction', $id, 'int');
    $db->query($query, $params);
    
    // clicked button "all_clicked" already exist
    $query = "SELECT all_clicked FROM " . $DBPrefix . "buttonclicked WHERE all_clicked = :all_clicked AND auction_id = :auction";
    $params = array();

    $params[] = array(':all_clicked', $_POST['all_clicked'], 'int');
    $params[] = array(':auction', $id, 'int');
    $db->query($query, $params);
    if ($db->numrows() > 0)
    {
    $errmsg = $ERR_0099;
    }
            
            header('location: listusers.php?PAGE=' . intval($_POST['offset']));
            exit;
        }

thats d script for the button update insert and suppose exist, but not working.

In one query, you use:

$params[] = array(':all_clicked', $_POST['all_clicked'], 'str');

but in the one you’re having trouble with, you use

 $params[] = array(':all_clicked', $_POST['all_clicked'], 'int');

Presumably as the first query runs OK, that would suggest you should use ‘str’ in the second one.

Slightly OT, but in the last three queries you use the same contents in the params() array, so I don’t see why you rebuild it every time. You also don’t clear the array for the second query, so that will have four parameters.

yeah i tried then both didnt work

So it works OK in the first one, but not in the second?

“Doesn’t work” is not helping. Exactly what happens? How far through your code do you get before things start to go wrong?

havent had any problem for a while, the code seem ok, it suppose to return with error not sure what wrong

OK, so what steps have you taken to narrow down the problem? If you run the queries manually from something like phpmyadmin, do they do what you expect? Are you sure it’s the final query that’s causing the problem?

not sure, can u like go tru d code n see my i miss something ? cause i have different existing code script n they all working

No, I can’t do that - apart from anything else, I don’t have the database, your templates or any of the calling HTML pages.

Cut the page down into individual sections and work through it until you see where it stops working. For one of the queries, for example, you don’t clear the params() array before you add two parameters to it, so you are sending four parameters. Presumably as it just suddenly stopped working, whatever the ->query() function does makes that unimportant, but it seems strange.

You just need to narrow down what the problem is, then go from there.

no i meant d code i gave , m asking if u see anything wrong with it?

plus what u mean by ‘‘you don’t clear the params() array before you add two parameters to it’’?

There’s nothing obvious, but you said it’s been working and now does not, so that would suggest something data-related.

I don’t understand why you have the UPDATE and SELECT queries after the insert, unless there could be more than one row containing the same values for auction_id and all_clicked. If you insert a row, there’s no need to update that same row, and there’s no need to SELECT it either. Just check that the insert query worked, and go from there.

Exactly that. Have a look at these two queries, you can see that in the first

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

you clear the array before adding parameters to it, but in the next

        //update button click
    $query = "UPDATE " . $DBPrefix . "buttonclicked SET all_clicked = :all_clicked 
    WHERE auction_id = :auction";
    $params[] = array(':all_clicked', $_POST['all_clicked'], 'int');
    $params[] = array(':auction', $id, 'int');
    $db->query($query, $params);

you don’t, so by the time the update query is run, params() now has four elements in it. If the code has been working before, then obviously your ->query() function doesn’t mind that.