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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:44:23+00:00 2026-05-28T16:44:23+00:00

I am trying to use both mysqli_real_escape_string and trim together before making a MySQL

  • 0

I am trying to use both mysqli_real_escape_string and trim together before making a MySQL INSERT query. My code is as follows:

<?php
$fname = mysqli_real_escape_string($dbc, trim($_POST['fname']));
$sname = mysqli_real_escape_string($dbc, trim($_POST['sname']));
$occ = mysqli_real_escape_string($dbc, trim($_POST['occ']));
$twitter = mysqli_real_escape_string($dbc, trim($_POST['twitter']));
$email = mysqli_real_escape_string($dbc, trim($_POST['email']));
$skype = mysqli_real_escape_string($dbc, trim($_POST['skype']));
$topic1 = mysqli_real_escape_string($dbc, trim($_POST['topic1']));
$topic2 = mysqli_real_escape_string($dbc, trim($_POST['topic2']));
$topic3 = mysqli_real_escape_string($dbc, trim($_POST['topic3']));
$avoid1 = mysqli_real_escape_string($dbc, trim($_POST['avoid1']));
$avoid2 = mysqli_real_escape_string($dbc, trim($_POST['avoid2']));
$avoid3 = mysqli_real_escape_string($dbc, trim($_POST['avoid3']));
$cr = mysqli_real_escape_string($dbc, trim($_POST['cr']));

if ((!empty($fname)) && (!empty($sname)) && (!empty($email)) && (!empty($topic1))) {
    $dbc = mysqli_connect('host', 'user', 'password', 'database') or die('Error connecting to MySQL server');
    $query = "INSERT INTO initial_details (fname, sname, occ, twitter, email, skype, topic1, topic2, topic3, avoid1, avoid2, avoid3, cr) VALUES ('$fname', '$sname', '$occ', '$twitter', '$email', '$skype', '$topic1', '$topic2', '$topic3', '$avoid1', '$avoid2', '$avoid3', '$cr')";
    $result = mysqli_query($dbc, $query);
    if (!$result) {
        mysqli_close($dbc);
        echo 'Duplicate';
    } else {
        mysqli_close($dbc);
        echo 'Success - entry added';
    }
} else {
    echo 'Error';
}   
?>

Using the code as above I get the ‘Error’ message, however if I remove mysqli_real_escape_string() and just use trim I am able to insert my entry successfully.

Why am I not able to use mysqli_real_escape_string() in this scenario?

  • 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-28T16:44:23+00:00Added an answer on May 28, 2026 at 4:44 pm

    $dbc = mysqli_connect must precede all the mysqli_real_escape_string calls to make it work. This is because you need an active mysqli connection to use that function.

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

Sidebar

Related Questions

I'm trying to use the following query syntax from a php file: $sql =
I have a PHP script which runs a MySQL query. $query = INSERT INTO
In Rails 2.3.6 I'm trying to use both MongoDb via MongoMapper and Mysql on
I'm trying to use jQuery to find the number of divs that are both
I'm trying to use DSDP (semidefinite programming package) with cvxopt. I have both of
I'm trying to use an open-source project. Both the latest official (from a *.tar.gz
I'm trying use mod_rewrite to rewrite URLs from the following: http://www.site.com/one-two-file.php to http://www.site.com/one/two/file.php The
I am trying to port a php file that is used with MySQL. My
I am trying to insert data into MySQL with a list using Python. The
I'm trying to write a SQL UNION that works on both MySQL and SQLite.

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.