www.webdeveloper.com
Results 1 to 4 of 4

Thread: Insert Form data into MySQL db using sessions

Hybrid View

  1. #1
    Join Date
    Sep 2010
    Posts
    36

    Insert Form data into MySQL db using sessions

    Hello all, hope you are well.

    I have a form split over 4 pages. I want to pass the data from each page to a session and then insert it into the database.

    I am not sure what I've done wrong but fields from the last page get put into the first few fields of the database instead of where they should be, and the rest are blank.

    Here's the codes:

    Top of page 2 before any html code
    PHP Code:
    session_start();
     

    $_SESSION['name'] = $_POST['os0'];
    $_SESSION['number'] = $_POST['os1'];
    $_SESSION['email'] = $_POST['os2']; 
    Page 3
    PHP Code:
    session_start();
     
     
    $_SESSION['collection_date'] = $_POST['os4'];
    $_SESSION['address'] = $_POST['address']; 
    Page 4
    PHP Code:
    session_start();

    $_SESSION['packkit'] = $_POST['os5'];
    $_SESSION['delivery'] = $_POST['on6'];
    $_SESSION['additems'] = $_POST['os7']; 
    php to insert into database
    PHP Code:
    <?php
    session_start
    ();

    $con mysql_connect("XXXX","XXXXX","XXXXX");
    if (!
    $con)
      {
      die(
    'Could not connect: ' mysql_error());
      }

    mysql_select_db("XXXXX"$con);

    $number mysql_real_escape_string(preg_replace('/[^0-9]/'''$_SESSION['number']));

    $realname mysql_real_escape_string($_SESSION['name']);

    $email mysql_real_escape_string($_SESSION['email']);

    $address mysql_real_escape_string(preg_replace('/[\r\n]/',' ',$_SESSION['address']));

    $collection mysql_real_escape_string($_SESSION['collection_date']);

    $packingkit mysql_real_escape_string($_SESSION['packkit']);

    $delivery mysql_real_escape_string($_SESSION['delivery']);

    $additems mysql_real_escape_string($_SESSION['additems']);

    $tandc mysql_real_escape_string($_POST['tandc']);

    $sql="INSERT INTO Orders (Name, Number, Email, Size, Address, Collection_Date, Packing_kit, Delivery_date, Additional_Items, Date_received, Terms, Deposit)
    VALUES
    ('
    $realname','$number','$email','Standard','$address','$collection','$packingkit','$delivery','$additems', date_format(curdate(),'%d-%m-%Y'),'$tandc', 'Yes')";

    if (!
    mysql_query($sql,$con))
    {
    die(
    'Error: ' mysql_error());
    }
    echo 
    "";

    mysql_close($con)
    ?>
    Can anyone see what I'm doing wrong here?

    Thanks in advance for any input, it is much appreciated.

    Kind regards, Mike

  2. #2
    Join Date
    Apr 2008
    Posts
    190
    Nothing in the code that you provided looks wrong. Perhaps it could be in form construction that you didn't provide.

    Have you checked the $_SESSION status at each stage? print_r($_SESSION); at the top of each page to see if it looks as it should.

  3. #3
    Join Date
    Sep 2010
    Posts
    36
    Hello Kurby, thank you for your swift response.

    I am fairly sure my form code is ok as originally I had just one page with the whole form on it. I have used the same code but simply split across the 4 pages. I'll check it though and post it it up just to make sure.

    I like your suggestion to test at each stage, I'll give that a go and post back.

    Thanks again and best regards, Mike

  4. #4
    Join Date
    Sep 2010
    Posts
    36
    Hello again,

    I cracked it (with your help!)

    Printing the session after each stage showed me that it was breaking down after the second page.

    You were right that my form code needed checking! The action= bit was set to a test php file I made earlier! I didn't notice because this php file still linked the third page so everything seemed to be ok!

    Thanks very much for your help.

    Kind regards, Mike

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles