Jump to content

INSERT not liking apostrophies


86Stang

Recommended Posts

Hi All,

 

A quick question --- I've got this INSERT:

 

while ($row = mysql_fetch_array($results))
{
$insertion = "INSERT INTO table (entry_id, title) VALUES ('$row[entry_id]','$row[title]')";
mysql_query($injection) or die (mysql_error());
}

 

The entry_id inserts just fine but the query dies as soon as it hits the first title that has an apostrophe in it.  Any thoughts?

Link to comment
Share on other sites

You need to call mysql_real_escape_string() on any data you plan to insert into the database.

 

You should also enclose any variables inside of double-quoted strings in curly brackets and always use single quotes indexes into associative arrays:

$var = "inside a string, this value is {$row['index']}";

Link to comment
Share on other sites

seeing the above your forming your query with variable name $insertion and then using variable $injection to run the query??? this will not work! your code should be

 

while ($row = mysql_fetch_array($results))
{
$insertion = "INSERT INTO table (entry_id, title) VALUES ('$row[entry_id]','$row[title]')";
mysql_query($insertion) or die (mysql_error());
}

Link to comment
Share on other sites

As roopurt18 said, use the function mysql_real_escape_string() on all strings you're inserting into the database when they are coming from an external source:

<?php
$insertion = "INSERT INTO table (entry_id, title) VALUES ('" . mysql_real_escape_string($row[entry_id]) . "','" . mysql_real_escape_string($row['title']) . "')";
?>

 

Ken

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.