VickP07
12-07-2011, 09:57 PM
Hey guys so i have a form where i am getting data from text fields that the user will enter his/her data into and then passing those data fields into variables and then passing them in a SQL query to be saved into the database being used.
I am trying to figure out how to use mysql_real_escape_string on any text input field to prevent sql injection and causing problems like when a user enters a last name like: O'leary the data is being saved like this: O\'leary i have tried to add the mysql_real_escape_string in my query where i am passing the variables that contain all the data from the text fields but so far it hasn't worked for me.
Can someone please tell me the correct way to use this function sot that way the data will be validated. I AM DOING THE mysql_real_escape_string in my SQL QUERY
here is my code that gets whatever the user enters from the text fields and then assigns them into variables. the sql INSERT statement can be found at the bottom of this code snipet:
<?php
// a class
class User
{
public $user, $pass, $fname, $lname, $role, $role2;
public $user_err, $pass_err, $name_err, $pass_err2, $pass_err3;
public function __construct( $post_array ) {
$this->user = $_POST['username'];
$this->pass = $_POST['password'];
$this->pass2 = $_POST['password2'];
$this->fname = $_POST['first_name'];
$this->lname = $_POST['last_name'];
$this->role = $_POST['role'];
$this->user_err = NULL;
$this->pass_err = NULL;
$this->pass_err2 = NULL;
$this->pass_err3 = NULL;
$this->name_err = NULL;
}
public function validate() {
// username isn't a duplicate
if( !$this->user ) {
$this->user_err = "Please specify username";
} else if( duplicate( $this->user ) ) {
$this->user_err = "That username is already in use";
}
// passwords match and are at least 6 chars
if( !$this->pass || strlen( $this->pass ) < 6 ) {
$this->pass_err = "Password must be at least 6 characters";
} else if( $this->pass != $this->pass2 ) {
$this->pass_err = "Passwords do not match";
}
//user forgot to provide a password
if( !$this->pass || !$this->pass2)
{
$this->pass_err2 = "Please provide a password";
}
//user forgot to provide a ROLE (doctor or nurse)
if( $this->role == 0)
{
$this->pass_err3 = "Please select one Role";
}
// first/last name aren't blank
if( !$this->fname || !$this->lname ) {
$this->name_err = "Please provide a first and last name";
}
return !$this->has_errors();
}
public function has_errors() {
return $this->user_err || $this->pass_err || $this->pass_err2 || $this->pass_err3 || $this->name_err;
}
public function insert()
{
$sql = "
INSERT INTO users
(username, f_name, l_name, role, pword)
VALUES ( '$this->user', mysql_real_escape_string('$this->fname'), mysql_real_escape_string('$this->lname'), '$this->role', aes_encrypt( 'The Secret Phrase', '$this->pass' ));";
//echo "<hr>DEBUG SQL: " . $sql . "<hr/>\n";
mysql_query( $sql ) or die( "Error( $sql): " . mysql_error() );
}
}
function duplicate( $username )
{
//function used to test for duplicate usernames
$sql = "SELECT user_id FROM users WHERE username = '$username'";
$result = mysql_query( $sql ) or die( "Error( $sql): " . mysql_error() );
return mysql_num_rows( $result ) > 0;
}
?>
I am trying to figure out how to use mysql_real_escape_string on any text input field to prevent sql injection and causing problems like when a user enters a last name like: O'leary the data is being saved like this: O\'leary i have tried to add the mysql_real_escape_string in my query where i am passing the variables that contain all the data from the text fields but so far it hasn't worked for me.
Can someone please tell me the correct way to use this function sot that way the data will be validated. I AM DOING THE mysql_real_escape_string in my SQL QUERY
here is my code that gets whatever the user enters from the text fields and then assigns them into variables. the sql INSERT statement can be found at the bottom of this code snipet:
<?php
// a class
class User
{
public $user, $pass, $fname, $lname, $role, $role2;
public $user_err, $pass_err, $name_err, $pass_err2, $pass_err3;
public function __construct( $post_array ) {
$this->user = $_POST['username'];
$this->pass = $_POST['password'];
$this->pass2 = $_POST['password2'];
$this->fname = $_POST['first_name'];
$this->lname = $_POST['last_name'];
$this->role = $_POST['role'];
$this->user_err = NULL;
$this->pass_err = NULL;
$this->pass_err2 = NULL;
$this->pass_err3 = NULL;
$this->name_err = NULL;
}
public function validate() {
// username isn't a duplicate
if( !$this->user ) {
$this->user_err = "Please specify username";
} else if( duplicate( $this->user ) ) {
$this->user_err = "That username is already in use";
}
// passwords match and are at least 6 chars
if( !$this->pass || strlen( $this->pass ) < 6 ) {
$this->pass_err = "Password must be at least 6 characters";
} else if( $this->pass != $this->pass2 ) {
$this->pass_err = "Passwords do not match";
}
//user forgot to provide a password
if( !$this->pass || !$this->pass2)
{
$this->pass_err2 = "Please provide a password";
}
//user forgot to provide a ROLE (doctor or nurse)
if( $this->role == 0)
{
$this->pass_err3 = "Please select one Role";
}
// first/last name aren't blank
if( !$this->fname || !$this->lname ) {
$this->name_err = "Please provide a first and last name";
}
return !$this->has_errors();
}
public function has_errors() {
return $this->user_err || $this->pass_err || $this->pass_err2 || $this->pass_err3 || $this->name_err;
}
public function insert()
{
$sql = "
INSERT INTO users
(username, f_name, l_name, role, pword)
VALUES ( '$this->user', mysql_real_escape_string('$this->fname'), mysql_real_escape_string('$this->lname'), '$this->role', aes_encrypt( 'The Secret Phrase', '$this->pass' ));";
//echo "<hr>DEBUG SQL: " . $sql . "<hr/>\n";
mysql_query( $sql ) or die( "Error( $sql): " . mysql_error() );
}
}
function duplicate( $username )
{
//function used to test for duplicate usernames
$sql = "SELECT user_id FROM users WHERE username = '$username'";
$result = mysql_query( $sql ) or die( "Error( $sql): " . mysql_error() );
return mysql_num_rows( $result ) > 0;
}
?>