Jump to content

INSERTING "Array"


tgavin

Recommended Posts

I have a form with a set amount of fields. If a user doesn't fill in a field it should insert nothing. Currently the word Array is being inserted

How do I prevent this from inserting Array into the database if the field is left blank?

 

escape() is just a function to clean the code (mysql_real_escape_string(), etc.)

ucsmart() just capitalizes words.

 

<?php
foreach($_POST['title'] as $i => $ctitle){
$title = ucsmart(strip_tags(stripslashes($_POST['title'][$i])));
$sql = mysql_query("INSERT INTO titles (id,num,title,added_uid) VALUES (".escape($newid).",{$i},'".escape($title)."','".escape($_SESSION['uid'])."')") or die(mysql_error());
}
?>

Link to comment
Share on other sites

Well, what has me scratching my head is that if the field is empty, $_POST shouldn't recognize it, right?

 

<?php
function escape($value) {
// strip slashes
if(get_magic_quotes_gpc()) {
	$value = stripslashes($value);
	$value = mysql_real_escape_string($value);
}
// quote if not integer
if(!get_magic_quotes_gpc()) {
	if(!is_numeric($value) || $value[0] == '0') {
		$value = mysql_real_escape_string($value);
	}
}
return convert_smart_quotes($value);
}

function convert_smart_quotes($string) {
$search = array(
	chr(145),
	chr(146),
	chr(147),
	chr(148),
	chr(151));

$replace = array(
	"'",
	"'",
	'"',
	'"',
	'-');
return str_replace($search, $replace, $string);
}
?>

 

Link to comment
Share on other sites

Well, what has me scratching my head is that if the field is empty, $_POST shouldn't recognize it, right?

 

Nope, if you post something thats blank it still posted

from the code i you have posted i think i was wrong the bug thier but you can add a workaround

 

function escape($value) {
if(empty($value)) return ""; // NOTE: 0 with return "" but then again so will array()
// strip slashes
if(get_magic_quotes_gpc()) {
//....snip

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.