herghost Posted April 10, 2009 Share Posted April 10, 2009 Is it possible to replace fields using a form, but if a field in the form is left blank not to replace the original data. For instance When you 1st enter the data you enter your name, email address, score then want to chane your email address and score then just change your score without deleting your old email address? Quote Link to comment https://forums.phpfreaks.com/topic/153527-replace-question/ Share on other sites More sharing options...
Maq Posted April 10, 2009 Share Posted April 10, 2009 You can use UPDATE. Quote Link to comment https://forums.phpfreaks.com/topic/153527-replace-question/#findComment-806686 Share on other sites More sharing options...
herghost Posted April 10, 2009 Author Share Posted April 10, 2009 bugger, tahts what I meant, I am using update, however when an option is left blank, it posts blank to the database, like the blank field is a value. Quote Link to comment https://forums.phpfreaks.com/topic/153527-replace-question/#findComment-806690 Share on other sites More sharing options...
Maq Posted April 10, 2009 Share Posted April 10, 2009 Well you need to build your query depending on whether or not your POST values are set. Quote Link to comment https://forums.phpfreaks.com/topic/153527-replace-question/#findComment-806692 Share on other sites More sharing options...
herghost Posted April 10, 2009 Author Share Posted April 10, 2009 Thanks, can you provide an example or further reading? Currently I have this: <?php //Start session session_start(); //Include database connection details require_once('include/database.php'); //Array to store validation errors $errmsg_arr = array(); //Validation error flag $errflag = false; //Function to sanitize values received from the form. Prevents SQL injection function clean($str) { $str = @trim($str); if(get_magic_quotes_gpc()) { $str = stripslashes($str); } return mysql_real_escape_string($str); } //Sanitize the POST values $banddataid = $_SESSION['SESS_USERID']; $bandname = $_SESSION['SESS_BANDNAME']; $genre = clean($_POST['genre']); $formed = clean($_POST['formed']); $member0 = clean($_POST['member0']); $member1 = clean($_POST['member1']); $member2 = clean($_POST['member2']); $member3 = clean($_POST['member3']); $member4 = clean($_POST['member4']); $member5 = clean($_POST['member5']); $position0 = clean($_POST['position0']); $position1 = clean($_POST['position1']); $position2 = clean($_POST['position2']); $position3 = clean($_POST['position3']); $position4 = clean($_POST['position4']); $position5 = clean($_POST['position5']); //Input Validations if($formed == '') { $errmsg_arr[] = 'Year Formed is Missing'; $errflag = true; } $sql = mysql_query("SELECT * FROM banddata WHERE userid = '$banddataid'"); if(mysql_num_rows($sql) == 0) { //Create INSERT query $qry = "INSERT INTO banddata (userid, bandname, genre, formed, position0, member0, position1, member1, position2, member2, position3, member3, position4, member4, position5, member5) VALUES ('$banddataid','$bandname','$genre', '$formed', '$position0', '$member0','$position1', '$member1', '$position2', '$member2','$position3', '$member3','$position4', '$member4', '$position5', '$member5')"; } else { //Create update query $qry = "UPDATE banddata SET banddataid = '$banddataid, bandname = '$bandname', genre = '$genre', formed = '$formed', position0 = '$position0', member0 ='$member0', position1 = '$position1', member1= '$member1', position2 = '$position2', member2 = '$member2', position3 = '$position3', member3 ='$member3', position4 = '$position4', member4 ='$member4', position5 = '$position5', member5 = '$member5' WHERE userid = '$banddataid'"; } $result = mysql_query($qry); //Check whether the query was successful or not if($result) { header("location: member_home.php"); exit(); }else { die(mysql_error()); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/153527-replace-question/#findComment-806696 Share on other sites More sharing options...
laffin Posted April 10, 2009 Share Posted April 10, 2009 sample code <?php function my_escape($string) { return is_int($string)?$string:("'" .mysql_real_escape_string($string) . "'"); } $updates=array(); $fields=array('username','password','email','im'); foreach($fields as $field) { if(isset($_POST[$field]) && !empty($data=trim($_POST[$field]))) $updates[$field]=$data } $update_fields=array_keys($updates); $values=implode(',',array_map('my_escape',$updates)); $fields=implode(',',$update_fields); $query = "UPDATE users ({$fields}) VALUES({$values}) WHERE id={$id}"; Work through the logic You will get it Quote Link to comment https://forums.phpfreaks.com/topic/153527-replace-question/#findComment-806704 Share on other sites More sharing options...
herghost Posted April 10, 2009 Author Share Posted April 10, 2009 thanks laffin, do I leave the insert and update fields as they are and put this after? or replace the update field with this?? Quote Link to comment https://forums.phpfreaks.com/topic/153527-replace-question/#findComment-806844 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.