ttmt Posted January 29, 2009 Share Posted January 29, 2009 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'] != ''){ Link to comment https://forums.phpfreaks.com/topic/142952-solved-empty-elements-in-array-added-to-db/ Share on other sites More sharing options...
Mchl Posted January 29, 2009 Share Posted January 29, 2009 I tried to capture the empty elements with - but no luck. if(isset($_POST['title']) && $_POST['title'] != ''){ Try if(isset($_POST['title']) && !empty($_POST['title'])){ Link to comment https://forums.phpfreaks.com/topic/142952-solved-empty-elements-in-array-added-to-db/#findComment-749530 Share on other sites More sharing options...
tran_dinh_ba Posted January 29, 2009 Share Posted January 29, 2009 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(); } } } Link to comment https://forums.phpfreaks.com/topic/142952-solved-empty-elements-in-array-added-to-db/#findComment-749539 Share on other sites More sharing options...
printf Posted January 29, 2009 Share Posted January 29, 2009 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'; } ?> Link to comment https://forums.phpfreaks.com/topic/142952-solved-empty-elements-in-array-added-to-db/#findComment-749547 Share on other sites More sharing options...
ttmt Posted January 29, 2009 Author Share Posted January 29, 2009 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(); } } } ?> Link to comment https://forums.phpfreaks.com/topic/142952-solved-empty-elements-in-array-added-to-db/#findComment-749576 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.