0

I have a form (on page form.php) and when the submit button is clicked it sends the data to mysql using:

  <form name="food."; action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" >

I now want to submit it to mysql then go to 'thankyou.php' page. So I need a server side PHP code to process the form to mysql.

  <form name="food."; action="thankyou.php" method="post" >

thankyou.php will submit the form then display 'thank you, your form has been submitted'.

What would the code need to be on the thankyou.php page to submit the form data on the form.php page.

2

3 Answers 3

1

You should go through some basic tutorials for it: http://www.w3schools.com/php/php_mysql_insert.asp

Here is sample code :

<?php
    $con = mysql_connect("localhost","peter","abc123");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }

    mysql_select_db("my_db", $con);

    $sql="INSERT INTO Persons (FirstName, LastName, Age)
    VALUES
    ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";

    if (!mysql_query($sql,$con))
      {
      die('Error: ' . mysql_error());
      }
   header("Location:thankyou.php");

    mysql_close($con)
    ?> 
Sign up to request clarification or add additional context in comments.

11 Comments

I have followed this tutorial, my code is here pastebin.com/DCJTjCPr . Im getting error 'Not Submitted'. All field names are correct. It is connecting to the db but not storing the data. Do you have any idea why?
you should quote all passed values properly. Like : '".$_POST[attendance1]."','".$_POST[colour1]."',.....
$sql="INSERT INTO user (attendance1, colour1, shade1, attendance2, colour2, shade2) VALUES ('".$_POST[attendance1]."','".$_POST[colour1]."','".$_POST[shade1]."','".$_POST[attendance2]."','".$_POST[colour2]."','".$_POST[shade2]."')";
ok got it working however it creates a new record in mydql. how do I just update the record?
just go through tutorials for updating records. use UPDATE query for that
|
1

you Are using the

    <?php echo $_SERVER['PHP_SELF']?>

meaning you are sending that on the same page,

    if(isset($_POST['submit'])){
    //validate your post
    if (post validated)
    insert to database
    use the header("location:thankyou.php");


    }

Comments

0

First setup your database connection, this information is best saved in a separate file

http://php.net/manual/en/function.mysql-connect.php

depending on what your form fields are, these would need to be added to the database with an INSERT Query

http://www.tutorialspoint.com/mysql/mysql-insert-query.htm

Then use

header("Location:thankyou.php");

(make sure there is no html before the redirect)

You don't need to another form

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.