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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:16:04+00:00 2026-05-26T20:16:04+00:00

I have been using the block of code below to supposedly stop sql injections.

  • 0

I have been using the block of code below to supposedly stop sql injections. It is something someone showed me when I first started php(which was not that long ago)

I place it in every page just as shown on the open. I am wondering if it is effective? I do not know how to test for sql injections

<?php

//Start the session

session_start();


//=======================open connection

include ('lib/dbconfig.php');

//===============This stops SQL Injection in POST vars

  foreach ($_POST as $key => $value) {
    $_POST[$key] = mysql_real_escape_string($value);
  }

  foreach ($_GET as $key => $value) {
    $_GET[$key] = mysql_real_escape_string($value);
  }

My typical insert and update queries look like this

$insert = ("'$email','$pw','$company', '$co_description', '$categroy', '$url', '$street', '$suite', '$city', '$state', '$zip', '$phone', '$date', '$actkey'");

mysql_query("INSERT INTO provider (email, pw, company, co_description, category, url, street, suite, city, state, zip, phone, regdate, actkey) VALUES ($insert)") or die ('error ' . mysql_error());

mysql_query("UPDATE coupon SET head='$_POST[head]', fineprint='$_POST[fineprint]', exdate='$exdate', creationdate=NOW() WHERE id='$cid'") or die ('error ' . mysql_error());
  • 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-26T20:16:05+00:00Added an answer on May 26, 2026 at 8:16 pm

    This is not enough.
    1. You’re missing cookies, $_COOKIE variable.
    2. If you use $_REQUEST you’re in trouble.
    3. You didn’t show your queries, you must enquote each variable with single quotes ” when you put it into query (especiall when the data is supposted to be an integer and you might think that quote is not necessary in that case, but that would be a big mistake).
    4. Data used in your query could come from other source.

    The best way is to use data binding and have the data escaped automatically by the driver, this is available in PDO extension.

    Example code:

    $PDO = new PDO('mysql:dbname=testdb;host=127.0.0.1' $user, $password);
    $stmt = $PDO->prepare("SELECT * FROM test WHERE id=? AND cat=?");
    $stmt->execute(array($_GET["id"], $_GET["cat"]));
    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
    

    You can also bind data using string keys:

    $stmt = $PDO->prepare("SELECT * FROM test WHERE id = :id AND cat = :cat");
    $stmt->execute(array(":id" => $_GET["id"], ":cat" => $_GET["cat"]));
    

    If you want to learn PDO, you might find useful these helper functions I use:

    http://www.gosu.pl/var/PDO.txt

    PDO_Connect(dsn, user, passwd) - connects and sets error handling.
    PDO_Execute(query [, params]) - only execute query, do not fetch any data.
    PDO_InsertId() - last insert id.
    
    PDO_FetchOne(query [, params]) - fetch 1 value, $count = PDO_FetchOne("SELECT COUNT(*) ..");
    PDO_FetchRow(query [, params]) - fetch 1 row.
    PDO_FetchAll(query [, params]) - fetch all rows.
    PDO_FetchAssoc(query [, params]) - returns an associative array, when you need 1 or 2 cols
    
    1) $names = PDO_FetchAssoc("SELECT name FROM table");
    the returned array is: array(name, name, ...)
    
    2) $assoc = PDO_FetchAssoc("SELECT id, name FROM table")
    the returned array is: array(id=> name, id=>name, ...)
    
    3) $assoc = PDO_FetchAssoc("SELECT id, name, other FROM table");
    the returned array is: array(id=> array(id=>'',name=>'',other=>''), id=>array(..), ..)
    

    Each of functions that fetch data accept as 2nd argument parameters array (which is optional), used for automatic data binding against sql injections. Use of it has been presented earlier in this post.

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

Sidebar

Related Questions

We have been using CruiseControl for quite a while with NUnit and NAnt. For
I have been using PHP and JavaScript for building my dad's website. He wants
I have been using Eclipse as an IDE for a short amount of time
I have been using Castle MonoRail for the last two years, but in a
We have been using Scrum for around 9 months and it has largely been
I have been using C# for a while now, and going back to C++
I have been using ASP.NET for years, but I can never remember when using
I have been using Ruby for a while now and I find, for bigger
I have been using IoC for a little while now and I am curious
I have been using the CSLA framework for couple of years now for windows

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.