0

I've been using implode function of php and suddenly I encounter a problem regarding it.

<?php 
$insertValues[] = "(default,'{$y}', '{$p}', '{$o}', '{$i}', '{$u}','AMM-40','test')"; 

$query_status = "INSERT INTO `mobile1_mn1`.`logs_inbound` (
                  `log_id`, `originator`, `sender`, `date`, `time`,
                   `message`, `company_id`, `keyword`) 
                 VALUES". implode(',',$insertValues);
?>

When the information on $y,$p,$o,$i and $u does not have any single 'quotations' and commas it can save my information on database but when I have a string say for example the string is "he's good" and "Im, good" having a comma and quote it can't save my information anymore...

2

4 Answers 4

3

You have to properly escape the string, use mysql_real_escape_string

The mysql_real_escape_string() function escapes special characters in a string for use in an SQL statement

The following characters are affected:

  • \x00
  • \n
  • \r
  • \
  • '
  • "
  • \x1a This function returns the escaped string on success, or FALSE on failure.
Sign up to request clarification or add additional context in comments.

Comments

1

use mysql_real_escape_string()

Comments

1

You have to escape SQL strings. You can use mysql_real_escape_string for this.

4 Comments

Escaping whatever "inputs" makes absolutely no sense. Escaping belongs to strings only and completely useless for any other "inputs". I hope eventually at least 10k+ folks will learn such a basic concept and refrain from writing misleading answers.
@Col.Shrapnel: All inputs are by default strings in php. So here input == string. I though that you would get it
I thought it is obvious that the topic in question is mysql strings, not PHP variables. Parts of the query that are enclosed in quotes. An number, when placed in the query without quotes, although being of string type in PHP will gain no benefit from escaping. Go figure. Not to mention silly "user unputs". As though non-user supplied strings require no escaping.
see your "escaping user inputs": stackoverflow.com/questions/9577833/…
-1

You have to put data/values that are STRINGS in quotes, so only imploding won't work here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.