Jump to content

How can I avoid blank rows?


php_rooky

Recommended Posts

Hi everybody,

I have an insert where I'm inserting many rows at the same time, but when I leave a blank row, is inserted as well. Is there a way to stop this?

Here is my code:

$num_records = count($_POST['language']); // this assumes that  room, name, voucher all have the same number of items and the items much up in a one-to-one fashion 

// connect to the db 
$con = mysql_connect ('host', 'username', 'password');
if (!$con)
  { 
  die ('Could not connect: ' .mysql_error()); 
  }
mysql_select_db('mydatabase', $con); 
  
//$valid_languages = array('english', 'french', 'spanish'); 

//foreach($_POST['language'] as $lang) { 
  // this comparison is case-sensitive so while english works, ENGLISH will not 
  //if (!in_array($lang, $valid_languages)) { 
    //die($lang . ' IS NOT A VALID LANGUAGE'); 
  //} 
//}
print($num_records);
for($i=0; $i<$num_records; $i++) { 
  $sql = "INSERT INTO table (language, room, name, voucher) VALUES ( 
    '" . mysql_real_escape_string($_POST['language'][$i]) . "', 
    '" . mysql_real_escape_string($_POST['room'][$i]) . "', 
    '" . mysql_real_escape_string($_POST['name'][$i]) . "', 
    '" . mysql_real_escape_string($_POST['voucher'][$i]) . "' 
  )"; 
  if (!mysql_query($sql, $con))
   {
   die('Error: ' . mysql_error());
   }
}
echo "1 record added";
mysql_close($con)

Link to comment
https://forums.phpfreaks.com/topic/124517-how-can-i-avoid-blank-rows/
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.