Jump to content

[SOLVED] Empty elements in array added to DB ?


Recommended Posts

Empty elements in array added to DB ?

 

Hi all

 

I have a simple form with a textarea, this textarea is repeated 3 times using a loop (it's a simpified  version os what I'm doing).

 

<?php
  require_once("includes/connection.php");
  require_once("includes/functions.php");
  include("includes/header.php");
$number = 3;
?>
<body>
<div id="con">
	<form name="upload_form" action="upload.php" method="post" >
		<?php
			for ($n = 1; $n <= $number; $n++) {
		?>
		<p>
			<b>Title:</b>
	    <textarea name="title[]" cols="30" rows="1"></textarea>
		</p>
		<?php
			}
		?>
		<br/>
		<p>
			<input type="submit" name="submit" value="Add Photos" />
		</p>
	</form>
</div>
</body>
</html>

 

When I enter the info from the textareas into the DB there is always 3 rows entered into the DB even if the the textarea is empty. I only want to enter values from textareas that have been filled in.

 

If 2 titles(textareas) have been filled in I would expect 2 rows in the DB

 

<?php
  require_once("includes/connection.php");
  require_once("includes/functions.php");
?>
<?php
$charid = "";
if(isset($_POST['title']) && $_POST['title'] != ''){
	foreach($_POST['title'] as $key => $input){
		$query = "INSERT INTO photos (filename) VALUES ('{$input}')";
		$result = mysql_query($query);
		confirm_query($result);
		if($result){
			$charid = mysql_insert_id();
		}
	}
}

 

 

I tried to capture the empty elements with - but no luck.

 

if(isset($_POST['title']) && $_POST['title'] != ''){

Below will only insert a new record into table if titles are not empty

 

foreach($_POST['title'] as $key => $input){

                              if(!empty(trim($input))) {

$query = "INSERT INTO photos (filename) VALUES ('{$input}')";

$result = mysql_query($query);

confirm_query($result);

if($result){

$charid = mysql_insert_id();

}

                              }

}

I would do it like this...

 

<?php
require_once 'includes/connection.php';

require_once 'includes/functions.php';

$insert = array ();

if ( ! empty ( $_POST['title'] ) )
{
	$_POST['title'] = array_diff ( array_map  ( 'trim', $_POST['title'] ), array ( '' ) );

	foreach ( $_POST['title'] as $key => $input )
	{
		$insert[] = "('" . mysql_real_escape_string ( $input ) . "')";
	}

	if ( ! empty ( $insert ) )
	{
		$result = mysql_query ( "INSERT INTO photos (filename) VALUES " . implode ( ', ', $insert ) . ";" );

		echo 'inserted ' . mysql_affected_rows () . ' row(s)';

	}
	else
	{
		echo 'no rows inserted';
	}
}
else
{
	echo 'nothing to insert';
}
?>

Mchl, tran_dinh_ba

 

I tried both your code but i still getting the same results

 

<?php
$charid = "";
if(isset($_POST['title']) && ! empty($_POST['title'])){
	foreach($_POST['title'] as $key => $input){
		$query = "INSERT INTO photos (filename) VALUES ('{$input}')";
		$result = mysql_query($query);
		confirm_query($result);
		if($result){
			$charid = mysql_insert_id();
		}
	}
}
?>

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.