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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:18:31+00:00 2026-06-15T20:18:31+00:00

I have two question is the following code a good way of practicing against

  • 0

I have two question

  1. is the following code a good way of practicing against SQL injection ( it seems to work OK as an insert method)

  2. How would in place this error message in the full example:

    if (!mysqli_query($query,$link))
    {
        die('Error: ' . mysqli_error());
    }
    

here is the full example:

<?php

$link = mysqli_connect("localhost","root","", "runtracker");
if (!$link)
{
    die('Could not connect: ' . mysqli_error());
}

$query="INSERT INTO userinfo (UserName) VALUES (?)";

if ($stmt = mysqli_prepare($link, $query)) {

    // Lets create the variables
    $name = $_POST['UserName'];

    // Bind the variables and execute the query
    mysqli_stmt_bind_param($stmt,"s", $name);
    mysqli_stmt_execute($stmt);

    // And now we close the statement
    mysqli_stmt_close($stmt);
}

echo "1 record added";

mysqli_close($link);
?>
  • 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-06-15T20:18:33+00:00Added an answer on June 15, 2026 at 8:18 pm

    Yes, using bound parameters is a good way to protect against SQL injection, for dynamic values in SQL queries. For more on SQL injection, you might like my presentation SQL Injection Myths and Fallacies.

    You’re right that it’s good to check for errors after calling API functions. Most mysqli functions return FALSE on error, but connecting is handled slightly differently.

    I also like to output the Mysqli error to a log I can read, but not to the user’s browser.

    Here’s how I would code it:

    <?php
    
    $mysqli = new mysqli("localhost","root","", "runtracker");
    if (mysqli_connect_error())
    {
        error_log("Connect error in file ".__FILE__.", line ".__LINE__.": "
          .mysqli_connect_error());
        die("Could not connect to database");
    }
    
    if (($stmt = $mysqli->prepare($link, $query)) === false) {
      error_log("Error on prepare in file ".__FILE__.", line ".__LINE__.": "
        .$mysqli->error);
      die('Error on prepare');
    }
    
    // Lets create the variables
    $name = $_POST['UserName'];
    
    // Bind the variables and execute the query
    if ($stmt->bind_param("s", $name) === false) {
      error_log("Error on bind in file ".__FILE__.", line ".__LINE__.": "
        .$stmt->error);
      die('Error on bind');
    }
    if ($stmt->execute() === false) {
      error_log("Error on execute in file ".__FILE__.", line ".__LINE__.": "
        .$stmt->error);
      die('Error on execute');
    }
    
    // And now we close the statement
    $stmt->close();
    
    echo "1 record added";
    
    $mysqli->close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following up this question , I have a further problem - I have two
i have following code for allocation two dimensional array #include <iostream> using namespace std;
Good Morning, I have the following line of code: var viewModel = new ClassifiedsBrowseViewModel
We have following code in our out_logs controller for saving into two tables in
I have two question to put forward: I was very interested, even intrigued by
guys I have two question to ask, they're easy, but bothering me for a
This is two part question: I have two stored procedures: sp1 & sp2. If
I have a question: I have two MSSQL tables, items and states, that are
I have two tables userdetails and blog question The schema is UserDetails: connection: doctrine
I have two examples I have a question about. Let me explain via some

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.