TeddyKiller Posted April 15, 2010 Share Posted April 15, 2010 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>'; } Link to comment https://forums.phpfreaks.com/topic/198657-why-does-this-update-the-database-with-blank-value/ Share on other sites More sharing options...
andrewgauger Posted April 15, 2010 Share Posted April 15, 2010 $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'" Link to comment https://forums.phpfreaks.com/topic/198657-why-does-this-update-the-database-with-blank-value/#findComment-1042505 Share on other sites More sharing options...
TeddyKiller Posted April 15, 2010 Author Share Posted April 15, 2010 I can honestly say, it worked before.. but suddenly just stopped.. but thanks. I might have to look at redesigning the validation though.. Link to comment https://forums.phpfreaks.com/topic/198657-why-does-this-update-the-database-with-blank-value/#findComment-1042510 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.