php_rooky Posted September 16, 2008 Share Posted September 16, 2008 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 More sharing options...
cooldude832 Posted September 16, 2008 Share Posted September 16, 2008 2 options 1) use an if(!empty($_POST['langugae'][$i]){ insert } 2) Insert all then delete from table where langauge = '' Link to comment https://forums.phpfreaks.com/topic/124517-how-can-i-avoid-blank-rows/#findComment-643022 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.