0

I have this PHP loop:

foreach($_POST['af_break_date'] as $id=>$value) {
    $update_break_date_query = "UPDATE studentdates SET student_break_date = '$value' WHERE student_date_id = '$id';";
    $update_break_date_sql = mysql_query($update_break_date_query) or die(mysql_error());
}

I want to run the $update_break_date_sql only if each $_POST['af_break_date'] is not null.

How can this be done?

1
  • there is no such thing as NULL value from postdata as far as I can tell - are those checkboxes you're posting? Commented Nov 30, 2012 at 12:35

6 Answers 6

4
foreach($_POST['af_break_date'] as $id=>$value)
{
    if($value != '')
    {
        $update_break_date_query = "UPDATE studentdates SET student_break_date = '$value' WHERE student_date_id = '$id';";
        $update_break_date_sql = mysql_query($update_break_date_query) or die(mysql_error());
    }
 }

Don't forget to secure for SQL Injection because you are adding raw user input to the query. Have a look at PDO or MySQLi and parameterised queries.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! How did I not think of this... Anyway, solved my problem!
I will, but I have to wait a few minutes to do so.
1

use isset

if(isset($_POST['af_break_date'] && 
strlen(implode("",$_POST['af_break_date']))>0)
{
....
...
}

1 Comment

Yes I tried that already, but the af_break_date will always POST and then empty data will be sent to the MySQL, I want to prevent that.
0
if( isset( $_POST['af_break_date'] ) && is_array(  $_POST['af_break_date'] )) {
    foreach($_POST['af_break_date'] as $id=>$value) {
        $update_break_date_query = "UPDATE studentdates SET student_break_date = '$value' WHERE student_date_id = '$id';";
        $update_break_date_sql = mysql_query($update_break_date_query) or die(mysql_error());
    }
}

Comments

0

Use !empty();

For Example

$value = false;

isset($value); //return true
!empty($value); //return false

Comments

0

Just add a condition to check & avoid empty values

if($_POST['af_break_date']){
    foreach($_POST['af_break_date'] as $id=>$value) {
      if(!$value) continue;
      $update_break_date_query = "UPDATE studentdates SET student_break_date = '$value' WHERE student_date_id = '$id';";
      $update_break_date_sql = mysql_query($update_break_date_query) or die(mysql_error());
    }
}

Don't use mysql_* functions, they will be deprecated soon. Instead use mysqli or PDO for connecting database. Use prepared statements to avoid SQL injections and before that do validations before updating datas to database.

Comments

0

Alternative to isset is empty which will check if the array is empty, so since its always being posted we check if its empty before running the foreach loop.

if (!empty($_POST['af_break_date']) && is_array($_POST['af_break_date'])) {
    foreach($_POST['af_break_date'] as $id=>$value) {
        $update_break_date_sql = mysql_query("UPDATE studentdates SET student_break_date = '$value' WHERE student_date_id = '$id'") or die(mysql_error());
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.