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
https://forums.phpfreaks.com/topic/87280-insert-not-liking-apostrophies/
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']}";

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());
}

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

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.