Check if value exist

hello programmers i have this code which act kinda funny

$query = "SELECT tagged FROM " . $DBPrefix . "bids_tagged WHERE tagged = :tagged";
    $params = array();
    $params[] = array(':tagged', $tagged, 'int');
    $db->query($query, $params);
if ($db->numrows() > 0)
{
    $errmsg = $ERR_0099;
}

it returns saying data already exist but still insert value inside the table what am i doing wrong here, thanks

without knowing your $db class that cannot be answered.

I told you already,

$stmt = $pdo->prepare("SELECT tagged FROM {$DBPrefix}bids_tagged WHERE tagged = ?");
$stmt->execute([$tagged]);
if ($stmt->fetchColumn())
{
    $errmsg = $ERR_0099;
}

hi is this like an example cause it didnt work

Did you show the correct piece of code? There’s no way the code you posted is inserting a value in a table.

yeap it is i ran multiple test, it submitted fine and then filled the form with a value i know is already in the table i then got already exist error which it’s good but the table still get filled n worst of all over write the last data entry

i meant it doesnt insert value, but for the love me it end up updating a row even after the error value already exist

You need to show us the bits of code that are doing the insert / update, and producing the error message.

below is the full code i use, for exist checking, insert n update

// tagged user
$query = "SELECT b.*, u.nick FROM " . $DBPrefix . "bids b
LEFT JOIN " . $DBPrefix . "users u ON (u.id = b.bidder)
WHERE b.bidder NOT IN ('b.tagged') and b.tagged IN ('b.bidder') and b.auction = :auc_id ";
$params = array();
$params[] = array(':auc_id', $id, 'int');
$db->query($query, $params);
				
if ($bidder = $tagged)
			{
			// tagged already exist
$query = "SELECT tagged FROM " . $DBPrefix . "bids WHERE tagged = :tagged";
	$params = array();
	$params[] = array(':tagged', $tagged, 'int');
	$db->query($query, $params);
	if ($db->numrows() > 0)
	{
	$errmsg = $ERR_0099;
	}
	
				
				// Also insert bids_tagged table
				$query = "INSERT INTO " . $DBPrefix . "bids_tagged VALUES (NULL, :auc_id, :bidder_id, :bid, :time, :qty, :willwin, :tagged, :balance)";
				
				$params = array();
				$params[] = array(':bid', $bid, 'float');
				$params[] = array(':auc_id', $id, 'int');
				$params[] = array(':bidder_id', $bidder_id, 'int');
				$params[] = array(':time', $NOW, 'int');
				$params[] = array(':qty', $qty, 'int');
				$params[] = array(':willwin', $willwin, 'str');
				$params[] = array(':tagged', $tagged, 'int');
				$params[] = array(':balance', $balance, 'int');
				$db->query($query, $params);
				
					// update bids
$query = "UPDATE " . $DBPrefix . "bids SET tagged = :tuser where auction = :auc_id and bidder = :tag";
$params[] = array(':tuser', $bidder_id, 'int');
$params[] = array(':auc_id', $id, 'int');
$params[] = array(':tag' , $tagged, 'int');
$db->query($query, $params);

This is probably not what you meant:

if ($bidder = $tagged)

Did you intend to run the INSERT and UPDATE queries even if the second SELECT returns more than zero rows? The structure of the code is that it runs that query, sets $err_msg, and then continues with the insert and updates. If not, then all that remaining code should be in your else() clause.

1 Like

i see, but theres no else clause in code trying to make one keep getting error

Just add

else { 

after the closing } after you assign $errmsg, and another closing } after the update query.

1 Like

works like magic droopnoot thanks alot.

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