Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7159919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:18:40+00:00 2026-05-28T13:18:40+00:00

I am trying to insert and update data using 3 prepared statements. There are

  • 0

I am trying to insert and update data using 3 prepared statements. There are two problems happening:

1: The SELECT COUNT query checks for if the record exists with a LIMIT 1 and result stored in $check. However, the LIMIT 1 seems to be ignored such as when I have 3 rows matching the WHERE conditions in the SELECT COUNT, $check returns 3 instead of 1.

2: The if ( (int)$check < 1 ) statement works and whenever $check = 0, I see the output from my echo’s, however the $stmt_insert->execute(); and $stmt_total->execute(); are not actually inserting and updating the database.

But, when I comment out:

 $stmt_check->execute();
 $stmt_check->bind_result($check);
 $stmt_check->fetch();

then the $stmt_insert and $stmt_total works and the rows are inserted and updated. I’m wondering if there’s a way to make all three statements work properly, for it seems some kind of conflict is occurring with my current set up. Thank you for your time.

Here is the full code:

$user_id = 100;
$count = preg_match_all("#<li>(.*?)</li>#is", $html, $matches, PREG_SET_ORDER);

$stmt_check = $mysqli->stmt_init();
$stmt_check->prepare("SELECT COUNT(`user_id`) AS `check` FROM `ua` WHERE `ach_class` = ? AND `user_id` = ? LIMIT 1");
$stmt_check->bind_param('si', $ach_class, $user_id);

$stmt_insert = $mysqli->stmt_init();
$stmt_insert->prepare("INSERT INTO `ua` (`user_id`, `ach_class`, `time_log`) VALUES (?, ?, ?)");
$stmt_insert->bind_param('isi', $user_id, $ach_class, $ach_timestamp);

$stmt_total = $mysqli->stmt_init();
$stmt_total->prepare("UPDATE `ach` SET `total` = `total` +1 WHERE `ach_class` = ? LIMIT 1");
$stmt_total->bind_param('s', $ach_class);

for ($i = 0; $i < $count; $i++) {
    $li_block = $matches[$i][1];
    preg_match('#img/(.*?)_60.png#is', $li_block, $class_matches);
    $ach_class = $class_matches[1];
    $ach_class = substr($ach_class, 11, -11);

    $ach_timestamp = time();

    $stmt_check->execute();
    $stmt_check->bind_result($check);
    $stmt_check->fetch();
    echo $ach_class.' --- Check = '.$check.'<br />';

    if ( (int)$check < 1 ) {
        $stmt_insert->execute();
        echo 'insert<br />';

        $stmt_total->execute();
        echo 'update<br />';
    } else {
        echo 'already present<br />';
    }
}

$stmt_insert->close();
$stmt_check->close();
$stmt_total->close();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T13:18:41+00:00Added an answer on May 28, 2026 at 1:18 pm
    1. LIMIT works…it limits number of return rows, and your returning 1 row with just one field (just count), so that LIMIT at the end is kind of meaningless, because it will return single result, anyway. Also, why do you have LIMIT 1 in your UPDATE command?
    2. You can use INSERT ON DUPLICATE KEY UPDATE and create UNIQUE index on your columns: ach_class, user_id and you will be fine.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using SQL Server, I'm trying to query a kind of averaged count from a
I'm trying to insert some sample data (Seed data) using EF 4.3 Migration. The
I'm trying to do the classic Insert/Update scenario where I need to update existing
I'm trying to do a single row insert/update on a table but all the
I am trying work out with MERGE statment to Insert / Update Dimension Table
Trying to do an insert, I have: jdbcTemplate.update(insert into...., new Object[]{foo.getId(), foo.getName()}) foo.getId() returns
im trying to insert this query with mysql_query INSERT INTO um_group_rights (`um_group_id`,`cms_usecase_id`,`um_right_id`) VALUES (2,1,1)
I am trying to INSERT INTO a table using the input from another table.
I was trying to insert new data into an existing XML file, but it's
I'm attempting to be more secure and start using PDO and prepared statements. This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.