Welcome Guest, Not a member yet? Register   Sign In
Issue with special characters and Ajax database updates
#1

[eluser]jsuissa[/eluser]
When trying to update my database via JQuery's $.ajax I noticed that special characters cause a 500 error. I've confirmed it's not a jQuery issue by removing the database calls and it work fine.

In short "Bob's Resumes" fails, but "Bobs Resumes" works.

Are there any suggestions as far as enconding that may help? Thanks in advance for any help on this.

Model:
Code:
public function update_field($table, $field, $value, $whereField = FALSE, $whereValue = FALSE )
{

$user_id = $this->session->userdata('user_id');
if ($whereField === FALSE) {
$sql = "UPDATE $table SET $field = '$value'";
} else {
$sql = "UPDATE $table SET $field = '$value' WHERE $whereField = $whereValue";
}
$query =  $this->db->query($sql);
if ($this->db->_error_message()) {

echo 'Error Num: ' . $this->db->_error_number();  //returns 0 if query successful/no error
echo '<br />Message: ' . $this->db->_error_message();
}

}

Controller:
Code:
public function change_vanity_name()
{
$vanity_name = $this->input->post('vanity_name');
$vanity_name = htmlspecialchars($vanity_name);

$resume_id = $this->input->post('resume_id');
$this->resume_model->update_field('resumes','vanity_name',$vanity_name,'resume_id',$resume_id);
$this->session->set_userdata('vanity_name',$vanity_name);
}
#2

[eluser]jsuissa[/eluser]
Think I found my answer:

BEFORE:

Code:
$sql = "UPDATE $table SET $field = $value WHERE $whereField =  $whereValue";
$query =  $this->db->query($sql);

AFTER:

Code:
$sql = "UPDATE $table SET $field = ? WHERE $whereField =  $whereValue";
$query =  $this->db->query($sql, array($value));

Just by using the ? operator for the value I wanted to change did the trick.
#3

[eluser]CroNiX[/eluser]
or
Code:
$this->db
  ->where($whereField, $whereValue)
  ->update($table, array($field => $value));




Theme © iAndrew 2016 - Forum software by © MyBB