Hi,

I am trying to create a simple form submission using php and mysql. I am stuck at very basic level of mysql and php code.

Here is what i am trying to do.

  1. index page where name and email data is shown.

  2. add-data.php page where i am adding the email and name using the form.

  3. send the data to database and update the page index.php.

  4. I am not planning to use ajax but just want to redirect to the index page after the successful submission.

Not sure how to submit the data to the database and redirect to the index page. Any help?

Here is my code.

index.php

<?php

//Code to connect to database
$con=mysql_connect("localhost","xxx","xxx") or die(mysql_error());
echo "Connected to database";

//select database
$select=mysql_select_db("test") or die(mysql_error());

//get the result from database
$result=mysql_query("SELECT* from demo");


//display result by looping through each row
while($row=mysql_fetch_array($result))
{

echo "<br/>";

//use row to fetch the element of each column
echo "<br/>"."ID : ".$row['id']."<br/>"." Name :".$row['name']. "<br/>"." Email :".$row['email']."<br/>";

}

mysql_close($con);


?>

add-data.php

<form method="post" action="index.php">

<label>Name :</label><input type="text" name="name" id="name"><br/>
<label>Email: </label><input type="text" name="email" id="email"><br/>
<br/><input type="submit" name="Add">
<br/>
</form>

I have managed to add the form. But not sure how to get the data from the form and add into the database and redirect to index.php

Any help with code to guide me in right direction is appreciated.

The concept is that your form posts the data (When method=post) within the form to the file specified in "action=file.php". The file that is to recieve the data (file.php in the case of this example) should be able to recieve the data through the $_POST superglobal.

Each piece of data is accessed by their names in the $_POST array e.g. $_POST['name'] will give you access to whatever was posted in the name field in your form.

What you'll want to do in file.php is check if the form has been submitted (This can be done by checking if the submit button is set in the post array - if (isset($_POST['Add'])) { //PROCESS FORM HERE }), do any validation you need and then do stuff with the data, which in your case will be adding it to the database. You will most likely want to use MySQL/MySQLi functions that PHP provides to execute statements with your data.

Lastly if you want to perform a redirect to another page you can use the header() PHP function e.g. header('Location: index.php'); will redirect the user to index.php

add-data.php (Note I've made it post to itself instead of index.php):

<?php
    if (isset($_POST["add"])) {
        $name = $_POST["name"];
        $email = $_POST["email"];

        // Do some validation and error checking here

        // Execute your database statements, making sure to escape values and check errors etc

        // If database insert was successful:
        header("Location: index.php");


    }
?>

<?php 
    // Display errors if there are any
?>
    <form method="post" action="add-data.php">
    <label>Name :</label><input type="text" name="name" id="name"><br/>
    <label>Email: </label><input type="text" name="email" id="email"><br/>
    <br/><input type="submit" name="add" value="Add">
    <br/>
    </form>

Thanks I have managed to execute the program now :)
Here is the code

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {


// Do some validation and error checking here
     if (empty($_POST["name"])) {
            $nameErr = "Missing";
        }
        else {
            $name = htmlspecialchars($_POST["name"]);
        }
        if (empty($_POST["email"])) {
            $nameErr = "Missing";
        }
        else {
            $email = htmlspecialchars($_POST["email"]);
        }


// Execute your database statements, making sure to escape values and check errors etc
$con=mysql_connect("localhost","n","n") or die(mysql_error());

$select=mysql_select_db("test") or die(mysql_error());

$query = "Insert INTO `demo` (name, email) values('$_POST[name]', '$_POST[email]')";

$result = mysql_query($query) or die(mysql_error());

// If database insert was successful:
header("Location: index.php");
}

?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.