PHP Tutorial Step by Step
PHP Tutorial Step by Step
<!DOCTYPE html>
<html>
<body>
<form action="submit.php" method="post">
First Name:<br>
<input type="text" name="firstname">
<br>
Last Name:<br>
<input type="text" name="lastname">
<br><br>
<input type="submit" value="submit">
</form>
</body>
</html>
Then check :
open browser search ” localhost/test/index.php/ ” .
Step 3 : Write Submite.php To insert data to database Database name:
db1 Table Name: user
Open text editor and create a new file named submit.php in the
C:\Xampp\htdocs\test directory.
Write these Code.
<?php
$x = $_POST['firstname'];
$y= $_POST['lastname'];
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db1";
//Create connection
$conn = new mysqli($servername, $username, $password,
$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sql = "INSERT INTO `user` (`fname`, `lname`) VALUES ('$x', '$y')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close(); ?>
Then check :
open browser search ” localhost/test/index.php/ ” .
Then typing insert data (First name: & Last name: ) and Click the
"Submit " button .
if ($result->num_rows > 0) {
//output data of each row
echo "<table>";
while ($row = $result->fetch_assoc()) {
echo "<tr><td> id: " . $row["id"] . "</td><td> Name: " .
$row["fname"] . " " . $row["lname"] . "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
</body>
</html>
Then check : open browser search ” localhost/test/index.php/ ” .
Then typing insert data (First name: & Last name: ) and Click the
"Submit " button .