Welcome Guest, Not a member yet? Register   Sign In
Problem in single quote
#1

[eluser]Tamilmani[/eluser]
Hi All,

I am using SQL database in my back end application. I need to allow the single quote(') values into the database.Any one suggest me how to fix this issue in Code igniter.

For Example: Mc'Donald - I need to allow this value into database.

Thanks
Tamilmani Mohan
#2

[eluser]Isern Palaus[/eluser]
http://php.net/manual/en/function.addslashes.php
http://www.php.net/manual/en/function.stripslashes.php

Code:
$add = "Mc’Donald - I need to allow this value into database.";
$add = addslashes($add);

echo $add;

$strip = $add;
$strip = stripslashes($strip);

echo $strip;
#3

[eluser]Tamilmani[/eluser]
I have tried those things

For Example:

$name = "Mc'Donald";
$slashName = addslashes($t); // output- 'Mc\'Donald'
insert into user(user_name)values('$slashName');

when i tried to run the above query.

Getting below error message.

Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'Donald'.
Msg 105, Level 15, State 1, Line 1
Unclosed quotation mark after the character string ')
'.
#4

[eluser]Isern Palaus[/eluser]
Can you copy your controller/model where are you processing this $name and the query?
#5

[eluser]Tamilmani[/eluser]
My Controller Code

function addUser()
{
$rules['username']="required";
$rules['password']="required";
$rules['fisrtname']="";
$rules['lastname']="";
$rules['address']=""
$this->validation->set_rules($rules);

$fields['username']="UserName";
$fields['password']="Password";
$fields['fisrtname']="";
$fields['lastname']="";
$fields['address']=""
$this->validation->set_rules($fields);
if($this->valdiation->run()== FALSE)
{

}
else
{
$firstname= addslashes($this->validation->firstname);
$this->User_Model->addnewUser($this->validation->username,$this->validation->password,$firstname,$this->validation->lastname,$this->validation->address);
}
}


Model Code

function addnewUser($username,$password,$firstname,$lastname,$address)
{
$this->db->query("insert into users(UserName,Password,FirstName,LastName,Address)values('$username','$password','$firstname','$lastname','$address')");
return $this->db->insert_id();
}
#6

[eluser]Isern Palaus[/eluser]
Why don't you use Active Record? It will be easier and class Validation was deprecated on CI 2.0. What version of CI are you using?

Well assuming that he info you are passing to the model is right, you can try the next:

Code:
function addnewUser($username, $password, $firstname, $lastname, $address)
{
     $data = array(
          "UserName" => $username,
          "Password" => $password,
          "FirstName" => $firstname,
          "LastName" => $lastname,
          "Address" => $address
     );

     $this->db->insert("users", $data);
}

And remove $firstname= addslashes($this->validation->firstname);
#7

[eluser]Tamilmani[/eluser]
I am using older version of codeigniter 1.7.

Is it any alternate way to do addslashes.

For example
If we set in rules then no need to write the addslashes in each model function.

like

$rules['firstname']="required|addslashes"

is it possible?
#8

[eluser]Isern Palaus[/eluser]
Did u tried what I said? The model part




Theme © iAndrew 2016 - Forum software by © MyBB