Jump to content

How do I use the mysql_real_escape_string()?


neex1233

Recommended Posts

I don't understand how you use it. Here are the scripts that I would like to use it one:

 

<?php
$con = mysql_connect("localhost","My_Username","Password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("My_DB", $con);

printf("Last inserted record has id %d\n", mysql_insert_id());

$sql="INSERT INTO users (id,username,password,userlevel)
VALUES
('$_POST[id]','$_POST[username]','$_POST[password]','$_POST[userlevel]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "<strong>User Added to Database!</strong>";

mysql_close($con)
?> 

 

<?php
$con = mysql_connect("localhost","My_Username","Password");
mysql_select_db("My_DB", $con);
$username = mysql_real_escape_string($_POST["username"]);
if (isset($_POST['edit'])) {
$sql = "UPDATE users SET username = '{$_POST['username']}', password = '{$_POST['password']}', userlevel = '{$_POST['userlevel']}'
WHERE `username` = '$username' LIMIT 1";
mysql_query($sql)
or die (mysql_error());
}  
?>

 

Also, how else could I make these scripts more secure? Thanks!!

I just read it.

 

i can't count how many times reading the manual about a function solves a thread, and could have done so even before the thread was posted. i hope you've learned a lesson - the PHP manual is your friend.

I also learned that the protection script that I am using (X-Protection) is not secure :-\

 

So, now I guess I will work on making it secure.  :'(

 

There are a multitude of tools that you can get via plug-in for your browser (i.e. - SQL Inject ME), to test your applications with.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.