promotec Posted May 20, 2013 Share Posted May 20, 2013 Hi guys the outcome Im trying to achieve is adding an offline button and online button to a membership area im using and offline form button and a online form button to send either a 1 value or a 0 value to the php script to update the MySQL field online_status this is the form buttons (page online ,php) <form method='post' action='online2.php'><input type="hidden" name="online" value="1"><input type="submit" value="Online"> </form> <form method='post' action='online2.php'><input type="hidden" name="online" value="0"><input type="submit" value="Offline"> </form> This is the script to collect the form output (page online2 ,php) <?php$dbhost = 'localhost:3036';$dbuser = 'promotec_admin';$dbpass = 'moldflow888';$conn = mysql_connect($dbhost, $dbuser, $dbpass);$online = $_POST["online"];if(! $conn ){ die('Could not connect: ' . mysql_error());}$sql = 'UPDATE oc_t_user SET online_status="$online" WHERE s_username="Promotec"'; mysql_select_db('promotec_osclass');$retval = mysql_query( $sql, $conn );if(! $retval ){ die('Could not update data: ' . mysql_error());} mysql_close($conn);?> I cant get it to work using the $online variable as the SET value if I replace the $online variable with a 1 or a 0 it works fine and updates the database field fine but using the variable as the SET value it doesn't update the database field.... it doesn't show an error but doesn't update the online_status field also is there a way of making the script redirect back to the page (online.php) with the form buttons once the script has executed I tried via form hidden redirect but that didn't work Ive spent hours playing with it to no avail, any advice would be much appreciated Kind regards Chris Quote Link to comment https://forums.phpfreaks.com/topic/278193-simple-problem-writing-form-data-to-mysql/ Share on other sites More sharing options...
Muddy_Funster Posted May 20, 2013 Share Posted May 20, 2013 you have given the same name to both of your hidden form elements. You can't do that (well you can, but you end up hitting problems when you try to access the contents of the variable....) change one to online and the other to offline, have both of them suybmit the value of 1 (as 0 can sometimes be treated as faulse for boolian checks) then check on the online2.php page which one is clicked by using the !isset($_POST['...']) check : <?php ... if(!isset($_POST['offline'])){ if(!isset($_POST['online'])){ echo"I don't think you should be here...."; exit(); } else{ //do online stuff } } else{ //do offline stuff } ... ?> Quote Link to comment https://forums.phpfreaks.com/topic/278193-simple-problem-writing-form-data-to-mysql/#findComment-1431140 Share on other sites More sharing options...
Barand Posted May 20, 2013 Share Posted May 20, 2013 It isn't the variable naming that is the problem. They are in different forms any way. The SQL string is enclosed in single quotes so the $online variable is not interpreted. Swap the single quotes for double and vice versa Quote Link to comment https://forums.phpfreaks.com/topic/278193-simple-problem-writing-form-data-to-mysql/#findComment-1431228 Share on other sites More sharing options...
promotec Posted May 21, 2013 Author Share Posted May 21, 2013 (edited) Thankyou Barand you are a champion, that was the info I needed that has solved my main problem and Muddy_Funster thankyou as well for the time you took to assist me much appreciated and you imput will also be utilized. The second issue was redirecting the script back to the form when execution has completed using the header method runs into probs as this script will be included in another script with the include statement. Can the form be in the same script, Im not exactly sure without researching it heres my theory firstly ill try to explain in words the script gets called online.php?online=1 (output from the form) the script checks whether there are any variables IF it finds the query string with the ?online=1 it completes the processing routine and writes the given value to MySQL then continues to the next routine which echo's the form, If the script doesn't find any query string/variables it bypasses the processing routine and goes straight to the echo the form, ill include an example the syntax wont be correct but just as an example <?php $dbhost = 'localhost:3036'; $dbuser = 'promotec_admin'; $dbpass = 'moldflow888'; $conn = mysql_connect($dbhost, $dbuser, $dbpass if(!isset($_POST['online'])){ $online = $_POST["online"]; if(! $conn ) { die('Could not connect: ' . mysql_error()); } $sql = "UPDATE oc_t_user SET online_status='$online' WHERE s_username='Promotec'"; mysql_select_db('promotec_osclass'); $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('Could not update data: ' . mysql_error()); } mysql_close($conn); } else{ echo "<form method='post' action='online2.php'><input type='hidden' name='online' value='1'> <input type='submit' value='Online'></form> "; } exit(); ?> Any assistance with the correct syntax for the above would be greatly appreciated, I was a webdeveloper for many years but 95% of the programming was out sourced now after a break of 5 years, saying im rusty is an understatement to say the least *grin* Kind regards Chris Edited May 21, 2013 by promotec Quote Link to comment https://forums.phpfreaks.com/topic/278193-simple-problem-writing-form-data-to-mysql/#findComment-1431335 Share on other sites More sharing options...
Barand Posted May 21, 2013 Share Posted May 21, 2013 The second issue was redirecting the script back to the form when execution has completedusing the header method runs into probs as this script will be included in another script with the include statement. Could you redirect back to the including script header("location: including.php?option=1"); and in the script conditionally include online.php if (isset($_GET['online'] && $_GET['onlline']==1) include('online.php'); Quote Link to comment https://forums.phpfreaks.com/topic/278193-simple-problem-writing-form-data-to-mysql/#findComment-1431345 Share on other sites More sharing options...
Muddy_Funster Posted May 21, 2013 Share Posted May 21, 2013 header problems can normally be gotten around by using the output buffer for scripts that run mid process. There is no reason that you can't have the whole thing on the one page given the way you have described it in your last post. You do need to be careful of your logic when checking the !isset() of the post data. Most often (if not always) post information is sent through the header, not the url query string - you would use $_GET to retrieve that info as Barand has shown in his example. I do hope that the connection info you posted is not actually accurate..... Give us a full breakdown of what you are doing (as well as what your final goal is) and we'll be able to help you more comprehensively. Quote Link to comment https://forums.phpfreaks.com/topic/278193-simple-problem-writing-form-data-to-mysql/#findComment-1431346 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.