0% found this document useful (0 votes)
27 views4 pages

WT 13

The document contains code for a form that allows users to enter an ID and name. When submitted, the form data is sent to an insert.php file that connects to a MySQL database called student. It inserts the ID and name values into a table called stud_info. If successful, it displays a message that the data was stored and prints the name; if not, it displays an error.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views4 pages

WT 13

The document contains code for a form that allows users to enter an ID and name. When submitted, the form data is sent to an insert.php file that connects to a MySQL database called student. It inserts the ID and name values into a table called stud_info. If successful, it displays a message that the data was stored and prints the name; if not, it displays an error.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Index.

Php:
<!DOCTYPE html>
<html lang="en">
<head>
<title>GFG- Store Data</title>
</head>
<body>
<center>
<h1>Storing Form data in Database</h1>
<form action="insert.php" method="post">
<p>
<label for="id">id:</label>
<input type="text" name="id" id="id">
</p>
<p>
<label for="lastName">Name:</label>
<input type="text" name="name" id="name">
</p>
<input type="submit" value="Submit">
</form>
</center>
</body>
</html>

Insert:
<!DOCTYPE html>
<html>

<head>
<title>Insert Page page</title>
</head>
<body>
<center>
<?php

// servername => localhost


// username => root
// password => empty
// database name => staff
$conn = mysqli_connect("localhost", "root", "", "student");

// Check connection
if($conn === false){
die("ERROR: Could not connect. "
. mysqli_connect_error());
}
$id= $_REQUEST['id'];
$name = $_REQUEST['name'];

// Performing insert query execution


// here our table name is college
$sql = "INSERT INTO stud_info VALUES ('$id','$name')";

if(mysqli_query($conn, $sql)){
echo "<h3>data stored in a database successfully."
. " Please browse your localhost php my admin"
. " to view the updated data</h3>";

echo nl2br("\n$name");
} else{
echo "ERROR: Hush! Sorry $sql. "
. mysqli_error($conn);
}
// Close connection
mysqli_close($conn);
?>
</center>
</body>
</html>

Output:
First:

Second:

You might also like