Jump to content

Why does this update the database with blank value?


TeddyKiller

Recommended Posts

I have a code. That when 2 fields, have a value entered in them, it updates the database in that field as blank. It's strange, if the value wasn't being gotten, it would of been unset and not change the database under the collum. So something happens after the foreach maybe it go blank? Any ideas?

 

Form validation

          $fields = array();
          $fields[] = (isset($_POST['proname']) && !empty($_POST['proname'])) ? "`pro_name` = '" . clean($_POST['proname'],1,0,0) . "'" : '';
        $fields[] = (isset($_POST['protag']) && !empty($_POST['protag'])) ? "`pro_tag` = '" . clean($_POST['protag'],1,0,0) . "'" : '';
        
        if(isset($_POST['checkbox'])) {
            $checkbox = implode(', ', $_POST['checkbox']);
            $fields[] = "`interested_in` = '". $checkbox . "'";
        }

          foreach ($fields as $key => $field) {
            if (empty($field)) {
                unset($fields[$key]);
            }
          }

          $query = mysql_query("UPDATE profile SET " . implode(", ", $fields) . " WHERE `user_id`='$user->id'") or trigger_error("Update Failed: " . mysql_error());
          if($query) {
              echo '<span style="color:#e11919">Profile updated!</span>';
        }

$fields[] = (isset($_POST['proname']) && !empty($_POST['proname'])) ? "`pro_name` = '" . clean($_POST['proname'],1,0,0) . "'" : '';

$fields[] = (isset($_POST['protag']) && !empty($_POST['protag'])) ? "`pro_tag` = '" . clean($_POST['protag'],1,0,0) . "'" : '';

       

might need to be:

$fields['pro_name'] = (isset($_POST['proname']) && !empty($_POST['proname'])) ? "'".clean($_POST['proname'],1,0,0) . "'" : '';
$fields['pro_tag'] = (isset($_POST['protag']) && !empty($_POST['protag'])) ? "'" . clean($_POST['protag'],1,0,0) . "'" : '';

 

Because I don't think you can set an associative array by directly defining ar[]="`key`='value'"

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.