andy_b_1502 Posted May 28, 2012 Share Posted May 28, 2012 Hello, On submit of my form the blank fields are updating the previous details OR if i have ALL details stored, then just change 1 field, it changes everyone of the text boxes to blank deleting the stored data? This is really annoying, how do i solve this? i have tried isset() but it doen't do anything at all with isset() set? <?PHP session_start(); include ('php only scripts/db.php'); $id = $_GET['id']; $website = $_GET['website']; $phone = $_GET['phone']; $phone2 = $_GET['phone2']; $premiumuser_decription = $_GET['premiumuser_decription']; $website = isset($_POST['$website']); $phone = isset($_POST['$phone']); $phone2 = isset($_POST['$phone2']); $premiumuser_decription = isset($_POST['$premiumuser_decription']); $query = "UPDATE companies SET website = '". mysql_real_escape_string($website) ."', phone = '". mysql_real_escape_string($phone) ."', phone2 = '". mysql_real_escape_string($phone2) ."', premiumuser_description = '". mysql_real_escape_string($premiumuser_decription) ."' WHERE id = ". mysql_real_escape_string($id); $result = mysql_query($query ) or die("SELECT Error: ".mysql_error()); header("Location: view01.php?id=" . $id); exit(0); ?> Am i on the right track or way off the mark? Quote Link to comment https://forums.phpfreaks.com/topic/263291-blank-fields-are-replacingupdating-others/ Share on other sites More sharing options...
Barand Posted May 28, 2012 Share Posted May 28, 2012 Have you thought of reading the manual to see what isset() actually does? isset Quote Link to comment https://forums.phpfreaks.com/topic/263291-blank-fields-are-replacingupdating-others/#findComment-1349363 Share on other sites More sharing options...
andy_b_1502 Posted May 29, 2012 Author Share Posted May 29, 2012 Yeah, determines if a variable has been set, and returns true if it is set? So isset function should work, right? Quote Link to comment https://forums.phpfreaks.com/topic/263291-blank-fields-are-replacingupdating-others/#findComment-1349557 Share on other sites More sharing options...
litebearer Posted May 29, 2012 Share Posted May 29, 2012 since you are UPDATING the record, why not populate the form with the existing data? That way data that has been modified will update the appropriate fields; while data unchanged will simply be populated with its original content. BTW just because the user is registered and editing their own data does NOT mean you should skip validating and sanitizing the data. Quote Link to comment https://forums.phpfreaks.com/topic/263291-blank-fields-are-replacingupdating-others/#findComment-1349562 Share on other sites More sharing options...
Barand Posted May 29, 2012 Share Posted May 29, 2012 The isset function works fine - but not the way you apparently expect it to. It puts a boolean (true or false) value in your variable, not the value you are wanting. Also, isset will return true if the variable exists but with a blank value. If, say, three of your four variables contain values but the fourth is blank : do you want to abort and send an error message or do you want to go ahead and just update the three or do update all four as values may be blank? Quote Link to comment https://forums.phpfreaks.com/topic/263291-blank-fields-are-replacingupdating-others/#findComment-1349563 Share on other sites More sharing options...
andy_b_1502 Posted May 29, 2012 Author Share Posted May 29, 2012 go ahead and just update the three or how ever many fields entered Quote Link to comment https://forums.phpfreaks.com/topic/263291-blank-fields-are-replacingupdating-others/#findComment-1349564 Share on other sites More sharing options...
andy_b_1502 Posted May 29, 2012 Author Share Posted May 29, 2012 iv'e just tested it and it doent't update anything with the isset() functions added? nothing is changed from the original Quote Link to comment https://forums.phpfreaks.com/topic/263291-blank-fields-are-replacingupdating-others/#findComment-1349566 Share on other sites More sharing options...
andy_b_1502 Posted May 29, 2012 Author Share Posted May 29, 2012 It puts a boolean (true or false) value in your variable, not the value you are wanting. What is the value i want? Quote Link to comment https://forums.phpfreaks.com/topic/263291-blank-fields-are-replacingupdating-others/#findComment-1349596 Share on other sites More sharing options...
Barand Posted May 29, 2012 Share Posted May 29, 2012 obviously the value you want is the value entered in the form. $setArray = array(); $setstr = ''; $id = intval($_GET['id']); if (isset($_GET['website']) && $_GET['website']) { $website = mysql_real_escape_string($_GET['website']); $setArray[] = "website = '$website'"; } if (isset($_GET['phone']) && $_GET['phone']) { $phone = mysql_real_escape_string($_GET['phone']); $setArray[] = "phone = '$phone'"; } if (isset($_GET['phone2']) && $_GET['phone2']) { $phone2 = mysql_real_escape_string($_GET['phone2']); $setArray[] = "phone2 = '$phone2'"; } if (isset($_GET['premiumuser_decription']) && $_GET['premiumuser_decription']) { $premiumuser_decription = mysql_real_escape_string($_GET['premiumuser_decription']); $setArray[] = "premiumuser_decription = '$premiumuser_decription'"; } if (count($setArray) > 0) { $setstr = join (', ', $setArray); $query = "UPDATE companies SET $setstr WHERE id = $id"; mysql_query($query) } Quote Link to comment https://forums.phpfreaks.com/topic/263291-blank-fields-are-replacingupdating-others/#findComment-1349612 Share on other sites More sharing options...
andy_b_1502 Posted May 29, 2012 Author Share Posted May 29, 2012 so the full code would look like this: <?PHP session_start(); include ('php only scripts/db.php'); $setArray = array(); $setstr = ''; $id = intval($_GET['id']); if (isset($_GET['website']) && $_GET['website']) { $website = mysql_real_escape_string($_GET['website']); $setArray[] = "website = '$website'"; } if (isset($_GET['phone']) && $_GET['phone']) { $phone = mysql_real_escape_string($_GET['phone']); $setArray[] = "phone = '$phone'"; } if (isset($_GET['phone2']) && $_GET['phone2']) { $phone2 = mysql_real_escape_string($_GET['phone2']); $setArray[] = "phone2 = '$phone2'"; } if (isset($_GET['premiumuser_decription']) && $_GET['premiumuser_decription']) { $premiumuser_decription = mysql_real_escape_string($_GET['premiumuser_decription']); $setArray[] = "premiumuser_decription = '$premiumuser_decription'"; } if (count($setArray) > 0) { $setstr = join (', ', $setArray); $query = "UPDATE companies SET $setstr WHERE id = $id"; mysql_query($query) } header("Location: view01.php?id=" . $id); exit(0); ?> Quote Link to comment https://forums.phpfreaks.com/topic/263291-blank-fields-are-replacingupdating-others/#findComment-1349643 Share on other sites More sharing options...
Barand Posted May 29, 2012 Share Posted May 29, 2012 Don't just use it blindly. Understand what it is doing. Quote Link to comment https://forums.phpfreaks.com/topic/263291-blank-fields-are-replacingupdating-others/#findComment-1349660 Share on other sites More sharing options...
andy_b_1502 Posted May 30, 2012 Author Share Posted May 30, 2012 <?php session_start(); */ Start session */ include ('php only scripts/db.php'); */ connect to db */ $setArray = array(); $setstr = ''; */ having trouble finding much on google for this */ $id = intval($_GET['id']); */ get the interger value of 'id' from the table */ */ get's information from each field set, from the table */ if (isset($_GET['website']) && $_GET['website']) { $website = mysql_real_escape_string($_GET['website']); $setArray[] = "website = '$website'"; } if (isset($_GET['phone']) && $_GET['phone']) { $phone = mysql_real_escape_string($_GET['phone']); $setArray[] = "phone = '$phone'"; } if (isset($_GET['phone2']) && $_GET['phone2']) { $phone2 = mysql_real_escape_string($_GET['phone2']); $setArray[] = "phone2 = '$phone2'"; } if (isset($_GET['premiumuser_decription']) && $_GET['premiumuser_decription']) { $premiumuser_decription = mysql_real_escape_string($_GET['premiumuser_decription']); $setArray[] = "premiumuser_decription = '$premiumuser_decription'"; } */ if there IS NOT a match then add the new user information/details, if there IS a match, leave or do not change */ if (count($setArray) > 0) { $setstr = join (', ', $setArray); $query = "UPDATE companies SET $setstr WHERE id = $id"; mysql_query($query) } */ direct user back to view01.php */ header("Location: view01.php?id=" . $id); exit(0); ?> Please correct me where i have gone wrong or miss understood the information? Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/263291-blank-fields-are-replacingupdating-others/#findComment-1349891 Share on other sites More sharing options...
Barand Posted May 30, 2012 Share Posted May 30, 2012 comment syntax is /* comment */ not */ comment */ What symptoms are you getting that something wrong? Quote Link to comment https://forums.phpfreaks.com/topic/263291-blank-fields-are-replacingupdating-others/#findComment-1349944 Share on other sites More sharing options...
andy_b_1502 Posted May 31, 2012 Author Share Posted May 31, 2012 how come it comments the code out then? or it that just the "standard" way? There's nothing wrong, it works great thank you but for the sake of trying to understand it i was simply trying to clarify for myself each section... Quote Link to comment https://forums.phpfreaks.com/topic/263291-blank-fields-are-replacingupdating-others/#findComment-1350098 Share on other sites More sharing options...
Pikachu2000 Posted May 31, 2012 Share Posted May 31, 2012 When I run that code, it throws an "Unexpected *" parse error due to the mangled comment delimiters. Quote Link to comment https://forums.phpfreaks.com/topic/263291-blank-fields-are-replacingupdating-others/#findComment-1350107 Share on other sites More sharing options...
andy_b_1502 Posted May 31, 2012 Author Share Posted May 31, 2012 i'm using dreamweaver, i don't know why it's working for me but it comments out the code, turning it yellow? i don't know... Quote Link to comment https://forums.phpfreaks.com/topic/263291-blank-fields-are-replacingupdating-others/#findComment-1350108 Share on other sites More sharing options...
Barand Posted May 31, 2012 Share Posted May 31, 2012 Using */comment*/ as you do gives an error when I try to run code Parse error: syntax error, unexpected '*' in C:\inetpub\wwwroot\apps\test\noname2.php on line xx Have you got your error reporting turned off? Added my comments <?php $id = intval($_GET['id']); // guarantee it's a harmless number value if (isset($_GET['website']) && $_GET['website']) { // does the GET value exists and has it a value ? $website = mysql_real_escape_string($_GET['website']); // get its value and escape it $setArray[] = "website = '$website'"; // ok to update this field in the query so store it } if (isset($_GET['phone']) && $_GET['phone']) { $phone = mysql_real_escape_string($_GET['phone']); $setArray[] = "phone = '$phone'"; } if (isset($_GET['phone2']) && $_GET['phone2']) { $phone2 = mysql_real_escape_string($_GET['phone2']); $setArray[] = "phone2 = '$phone2'"; } if (isset($_GET['premiumuser_decription']) && $_GET['premiumuser_decription']) { $premiumuser_decription = mysql_real_escape_string($_GET['premiumuser_decription']); $setArray[] = "premiumuser_decription = '$premiumuser_decription'"; } if (count($setArray) > 0) { // do we have at least on field to update? $setstr = join (', ', $setArray); // form a comma separated string of our updates $query = "UPDATE companies SET $setstr WHERE id = $id"; // update it mysql_query($query); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/263291-blank-fields-are-replacingupdating-others/#findComment-1350110 Share on other sites More sharing options...
andy_b_1502 Posted May 31, 2012 Author Share Posted May 31, 2012 Have you got your error reporting turned off? Just check my php ini file, the error report is turned on: - Show all errors, except for notices and coding standards warnings ; ;error_reporting = E_ALL & ~E_NOTICE Quote Link to comment https://forums.phpfreaks.com/topic/263291-blank-fields-are-replacingupdating-others/#findComment-1350115 Share on other sites More sharing options...
Pikachu2000 Posted May 31, 2012 Share Posted May 31, 2012 That line is commented out by the leading semicolon. You need to have error_reporting = -1 and display_errors = On in the php.ini file. Then save it and restart Apache. Quote Link to comment https://forums.phpfreaks.com/topic/263291-blank-fields-are-replacingupdating-others/#findComment-1350116 Share on other sites More sharing options...
andy_b_1502 Posted May 31, 2012 Author Share Posted May 31, 2012 I'm not using apache, well not on my system anyways.. the php .ini file is on my host's cpanel. why is it -1? Quote Link to comment https://forums.phpfreaks.com/topic/263291-blank-fields-are-replacingupdating-others/#findComment-1350118 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.