Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    May 2013
    Posts
    24
    Thanks
    8
    Thanked 0 Times in 0 Posts

    simple problem writing form data to mysql

    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

  • #2
    Regular Coder
    Join Date
    Jun 2009
    Posts
    134
    Thanks
    3
    Thanked 19 Times in 19 Posts
    1) Sanatize your data before sending it to the DB

    2) You do not need two forms, using radio buttons (or a drop down) they can change their option, which would mean you could then preset what they are currently set to when you load the form in the first place.

    3) Dont know how much trouble shooting you have done, but when a variable is failing to be passed into the DB query you must ask yourself why not. Putting an echo like

    PHP Code:
    echo "Var online = ".$online
    would immediately tell you if the variable is blank and if it isnt then you have an SQL problem.

    Try changing your query to

    PHP Code:
    $sql "UPDATE oc_t_user
     SET online_status='$online'
     WHERE s_username='Promotec'"


  • Users who have thanked itxtme for this post:

    promotec (05-21-2013)

  • #3
    New Coder
    Join Date
    May 2013
    Posts
    24
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Many thanks itxtme this is great help, the form I will update to select / radio or drop downs, it was just a quick form to use to get the syntax of the processing routine correct, now I can go through and perfect elements like that now the processing routine is working, I really appreciate your help thankyou
    Regards Chris


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •