Jump to content

MySQL Query Problem


nathanb

Recommended Posts

Hi, I am editing a script and sometimes this query works and sometimes it does not. Here is the particular code that is (sometimes) malfunctioning:
[code]if (($_POST['description']!='DEBUG')||(!in_array('DEBUG', $_POST))) $query1 = mysql_query("INSERT INTO countdowns (`creator`, `creationdate`, `countdowndate`, `type`, `description`, `private`, `timezone`, `special`, `specialinfo`) VALUES ('$creator', '$creationdate', '$countdowndate', '".($tag=='official'?'official':'user')."', '$description', '$private', '$ctimezone', '$special', '$specialinfo')");[/code]

In a working case, the variables include something like this:
[code]$creator = '1';
$creationdate = '1141347209';
$countdowndate = '1141362000';
$description = "rarw";
$private = '1';
$timezone = 'GMT-5';
$special = '';
$specialinfo = '';[/code]

and it goes through as expected as it always did. Another working case is this:
[code]$creator = '1';
$creationdate = '1141347209';
$countdowndate = '1141362000';
$type = '';
$description = "So and so.. Auction Ends";
$private = '1';
$timezone = 'GMT-5';
$special = 'ebay';
$specialinfo = '4437837371';[/code]

But this and others do not work:
[code]$creator = '1';
$creationdate = '1141347209';
$countdowndate = '1141362000';
$description = "Jim's 1st Anniversary";
$private = '1';
$timezone = 'GMT-5';
$special = 'anniversary of marriage';
$specialinfo = '1st|Jim';[/code]

The $query1 variable appears to be null, but it shouldn't. mysql_error() says that it is not a valid MySQL Link Resource.. Note that this doesn't happen in the above working cases.

Any ideas? Thanks in advance!
Link to comment
Share on other sites

the error was because of an apostrophe in the following string:
[code]$description = "Jim's 1st Anniversary";[/code]

Your server doesn't addslashes to post values automatically. one option is to set the php.ini directive magic_quotes_gpc to "on", or use the following script to clean up description
[code]if(!get_magic_quote_gpc())
{
   $_POST['description'] = addslashes($_POST['description']);
}

//produces: Jim\'s 1st Anniversary
[/code]

the following script will addslashes to all post values (except arrays)
[code]foreach($_POST as $k=>$v)
{
  $_POST[$k] = !get_magic_quote_gpc() ? addslashes($v) : $v;
}[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.