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
https://forums.phpfreaks.com/topic/98481-inserting-array/
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
https://forums.phpfreaks.com/topic/98481-inserting-array/#findComment-504099
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
https://forums.phpfreaks.com/topic/98481-inserting-array/#findComment-504153
Share on other sites

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.