ladieballer2004 Posted August 17, 2009 Share Posted August 17, 2009 Ok so here it goes: I have an array that is attached to a web form. It's function is to ask the user what she has been involved in. There are 3 rows with 4 columns each. The user is required to fill out at least one row but not all three. Here is the problem. When I test it out all three rows are entered into the database wether they hold information or not. How do I get the parser to recognize when a row is blank and ignore it before the info is inserted into the database? Here is the PHP Code for the Array foreach($_POST['Activity'] as $row=>$Act) { $Activity=($Act); $Position=($_POST['Position'][$row]); $StartDate=($_POST['StartDate'][$row]); $EndDate=($_POST['EndDate'][$row]); if ($row=="") {$row=="NULL";} $involv = "INSERT INTO Involvement (Activity, Position, StartDate, EndDate) VALUES ('$Activity','$Position','$StartDate','$EndDate')"; Quote Link to comment https://forums.phpfreaks.com/topic/170624-help-with-arrays/ Share on other sites More sharing options...
DarkendSoul Posted August 17, 2009 Share Posted August 17, 2009 foreach($_POST['Activity'] as $row=>$Act) { $Activity=($Act); $Position=($_POST['Position'][$row]); $StartDate=($_POST['StartDate'][$row]); $EndDate=($_POST['EndDate'][$row]); if (!empty($Act)) { $involv = "INSERT INTO Involvement (Activity, Position, StartDate, EndDate) VALUES ('$Activity','$Position','$StartDate','$EndDate')"; } I believe thats what you want... also before posting any data to a mysql database you should use mysql_real_escape_string() -> http://ca.php.net/manual/en/function.mysql-real-escape-string.php Quote Link to comment https://forums.phpfreaks.com/topic/170624-help-with-arrays/#findComment-899930 Share on other sites More sharing options...
oni-kun Posted August 17, 2009 Share Posted August 17, 2009 Well try this, you're just asking it to say NULL instead of actually nulling it out. foreach($_POST['Activity'] as $row=>$Act) { $Activity=($Act); $Position=($_POST['Position'][$row]); $StartDate=($_POST['StartDate'][$row]); $EndDate=($_POST['EndDate'][$row]); $involv = "INSERT INTO Involvement (Activity, Position, StartDate, EndDate) VALUES ( if isset($Activity){echo '$Activity';}, if isset($Position){echo '$Position';}, if isset($StartDate){echo '$StartDate';}, if isset($EndDate){echo '$EndDate';} )"; That'll not place it into the database if it is not set.. Quote Link to comment https://forums.phpfreaks.com/topic/170624-help-with-arrays/#findComment-899935 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.