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

  • Home
  • SEARCH
  • 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 4105864
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:18:10+00:00 2026-05-20T21:18:10+00:00

How do i secure my site to SQL injection attacks? Im using PHP and

  • 0

How do i secure my site to SQL injection attacks? Im using PHP and mysql. Do i have to alter my mysql Query?

For example, i have one query like this:

<?php
$q=$_GET["q"];// im getting the Q value from the other form,from drop down box[displaying data using Ajax
$a1=$_POST['hosteladmissionno'];
$a2=$_POST['student_name'];
$a3=$_POST['semester'];
$con = mysql_connect('localhost', 'root', '');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("hostel", $con);

$a1="SELECT  hosteladmissionno,student_name,semester FROM registration 
WHERE mess_type ".$q."' AND  status_flag=1";

$result = mysql_query($a1);

if ($result === false) {

    die(mysql_error());
}
echo "<table border='1' width=80%>
<tr>
 <th width=5%> S.No</th>
<th width=10%>H.Admin No</th>
<th width=10%>Student Name</th>
<th width=5%>No of Days</th>
</tr>";
 $i=0;
while($row = mysql_fetch_array($result))
  {
  $i=$i+1;
  echo "<tr>";
  echo "<td  align=center>" .$i."</td>";
  echo "<td size=10 align=center>" . $row['hosteladmissionno'] . "</td>";
  echo "<td size=35  align=center>" . $row['student_name'] . "</td>";
  echo "<td  align=center>  <input type='text' name='days' size=2> </td> ";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?>

Do i have to include any alteration in my query to avoid Injection attacks? Any suggestion would be helpful.Thanks in advance. Cheers!

  • 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-20T21:18:10+00:00Added an answer on May 20, 2026 at 9:18 pm

    Your query appears (EDIT: appeared, in the first version of the query) to be entirely static – i.e. it doesn’t use any user-supplied data. In that case, there’s no risk of SQL injection.

    SQL injection attacks involve taking user input and including that directly in a SQL query, instead of the preferred method of using a parameterized SQL statement and including user-supplied values that way. (I don’t know the details of how that’s done in PHP… I certainly hope it’s possible.)

    EDIT: Okay, now you’ve changed your code, including this:

    $a1="SELECT  hosteladmissionno,student_name,semester FROM registration 
    WHERE mess_type ".$q."' AND  status_flag=1";
    

    Where $q is retrieved from a text box. Now I assume you really meant the second line to be:

    WHERE mess_type='".$q."' AND  status_flag=1";
    

    But that’s still vulnerable to SQL injection attack. Suppose the value of q is:

    ' OR 'x'='x
    

    Your SQL statement would then end up as

    SELECT hosteladmissionno,student_name,semester FROM registration 
    WHERE mess_type='' OR 'x'='x' AND  status_flag=1
    

    which clearly isn’t the logic you’re after.

    You should use parameters for the values, as shown on this PHP prepared statement page.

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

Sidebar

Related Questions

Is SSL secure enough for using sensitive data (like password) in query string? Is
I am trying to secure my site so I'm not vulnerable to sql injection
I have written a secure TCP server in .NET. This was basically as simple
I'd like to programmatically access a site that requires Client certificates, which I have
I'm trying to come up with ways to speed up my secure web site.
I am doing some work on a web site that has a secure area
How secure are php Sessions? I am planning to use the native PHP sessions
I am currently building a site and all the books I have read on
I would like to secure an ASP.NET web application against hacking. Is there a
I have an incredibly frustrating problem cleaning up my site at the moment. On

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.