Jump to content

HELP adding mysql_real_escape_string as ' gives error inserting...


wispas

Recommended Posts

I need help adding a mysql_real_escape_string into my code as whenever i insert a ' into my city description it gives me a error and does not insert into the database... please help.

 

<?php
include('includes/config.php');
$tbl_name="city"; // Table name

// Get values from form
$city_name=$_POST['city_name'];
$city_desc=$_POST['city_desc'];

// Insert data into mysql
$sql="INSERT INTO $tbl_name(city_name, city_desc)VALUES('$city_name', '$city_desc')";
$result=mysql_query($sql);

?>

Link to comment
Share on other sites

Thanks Neil. I have also done this which also works:

 

<?php
include('includes/config.php');
$tbl_name="city"; // Table name

// Get values from form
$city_name = mysql_real_escape_string($_POST['city_name']) ;
$city_desc = mysql_real_escape_string($_POST['city_desc']) ;
$city_meta_keywords = mysql_real_escape_string($_POST['city_meta_keywords']) ;
$city_meta_desc = mysql_real_escape_string($_POST['city_meta_desc']) ;

// Insert data into mysql
$sql="INSERT INTO $tbl_name(city_name, city_desc, city_meta_keywords, city_meta_desc)VALUES('$city_name', '$city_desc', '$city_meta_keywords', '$city_meta_desc')";
$result=mysql_query($sql);

?>

Link to comment
Share on other sites

Yeah, but it looks messy. Why create a variable when the value is already stored in the post array. Just clean the array values in one go.

<?php
$fields = array('city_name','city_desc','city_meta_keywords','city_meta_desc');
foreach($fields as $field) {
$_POST[$field] = mysql_real_escape_string($_POST[$field]);
}
$result = mysql_query("INSERT INTO ".$tbl_name." (city_name, city_desc, city_meta_keywords, city_meta_desc) 
								VALUES('".$_POST['city_name']."', '".$_POST['city_desc']."', '".$_POST['city_meta_keywords']."', '".$_POST['city_meta_desc']."')");
?>

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.