Jump to content

problem passing variable with commas in the string


tallberg

Recommended Posts

I have a recurring problem i hope some kind sol can help with.

Below is a query that inserts a $condition variable into a db.

$dbList = mysql_query("INSERT INTO query VALUES ('','$condition')", $dbLocalhost)
or die("Problems writing to the datbase table drawer" . mysql_error());

The problem is: The condition variable is typically a string containing commas such as:

The_Group='Aplines'

php reads the first comma surrounding Alpines and thinks it is the end of the feild.

The other example of where this is problem:

<input type='hidden' name='condition' value='$conditiion'>

The $condition can not be pass to the $_POST array because it understand the comma is the $condition string variable as the end of the value.


Those are not commas, they are single quotes.  You need to use the function [url=http://www.php.net/mysql_real_escape_string]mysql_real_escape_string()[/url] on the values before they are inserted to escape these characters.

[code]<?php
$dbList = mysql_query("INSERT INTO query VALUES ('','" . mysql_real_escape_string($condition) . "')", $dbLocalhost)
      or die("Problems writing to the datbase table drawer" . mysql_error());
?>[/code]

The solution to your other problem is easier.  Use double quotes to enclose the value:
[code]<?php
echo '<input type="hidden" name="condition" value="' . $conditiion . '">';
?>[/code]

Ken

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.