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 7063507
In Process

The Archive Base Latest Questions

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

I am trying to select from a mySQL table using prepared statements. The select

  • 0

I am trying to select from a mySQL table using prepared statements. The select critera is user form input, so I am binding this variable and using prepared statements. Below is the code:

$sql_query = "SELECT first_name_id from first_names WHERE first_name = ?";
$stmt = $_SESSION['mysqli']->prepare($sql_query);

    $stmt->bind_param('s', $_SESSION['first_name']);
    $stmt->execute();
    $stmt->store_result();

    if ($stmt->num_rows == '1') {
            $stmt->bind_result($_SESSION['first_name_id']);
            $stmt->fetch();
    } else {
            $stmt->close();
            $sql_query = "INSERT INTO first_names (first_name) VALUES (?)";
            $stmt = $_SESSION['mysqli']->prepare($sql_query);
            $stmt->bind_param('s', $_SESSION['first_name']);
            $stmt->execute();
            $_SESSION['first_name_id'] = $_SESSION['mysqli']->insert_id;
    }
    $stmt->close();

Obviously my code is just determining whether or not the first_name already exists in the first_names table. If it does, it returns the corresponding ID (first_name_id). Otherwise, the code inserts the new first_name into the first_names table and gets the insert_id.

The problem is when a user enters a name with an escape character (‘Henry’s). Not really likely with first names but certainly employers. When this occurs, the code does not execute (no select or insert activity in the log files). So it seems like mySQL is ignoring the code due to an escape character in the variable.

How can I fix this issue? Is my code above efficient and correct for the task?

Issue #2. The code then continues with another insert or update, as shown in the code below:

if (empty($_SESSION['personal_id'])) {
            $sql_query = "INSERT INTO personal_info (first_name_id, start_timestamp) VALUES (?, NOW())";
    } else {
            $sql_query = "UPDATE personal_info SET first_name_id = ? WHERE personal_info = '$_SESSION[personal_id]'";
    }

    $stmt = $_SESSION['mysqli']->prepare($sql_query);
    $stmt->bind_param('i', $_SESSION['first_name_id']);
    $stmt->execute();

    if (empty($_SESSION['personal_id'])) {
            $_SESSION['personal_id'] = $_SESSION['mysqli']->insert_id;
    }
    $stmt->close();

The issue with the code above is that I cannot get it to work at all. I am not sure if there is some conflict with the first part of the script, but I have tried everything to get it to work. There are no PHP errors and there are no inserts or updates showing in the mySQL log files from this code. It appears that the bind_param line in the code may be where the script is dying…

Any help would be very much appreciated.

  • 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-28T04:43:19+00:00Added an answer on May 28, 2026 at 4:43 am

    you should validate/escape user input before sending it to the db.

    checkout this mysql-real-escape-string()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to select a number of elements from a mysql table based on
I am trying to get hold of 1 record from a MySQL table using
I'm trying to generate select names from a mysql database by using the rand()
I'm trying to select data from a table defined similar to the following :
I am trying to select rows from a table where one of the (NVARCHAR)
Well, I was trying to select rows from one table if there are no
I'm trying to select a column from a single table (no joins) and I
To get half of MySQL table records here is what I'm using: $numRows=mysql_num_rows(mysql_query(SELECT *
is it possible to order a select from mysql by using a known id
I learned today through this section of the MySQL documentation that prepared statements cannot

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.