PHP Inserting Data into a MySQL Database Table

Posted by w3diy - 1 year ago

index.php

<!DOCTYPE html>
<html>
<body>

<h2>Stuidents Forms</h2>

<form action="/stud/results.php">
  <label for="fname">Name:</label><br>
  <input type="text" id="fname" name="studname"><br>
  <label for="lname">Email:</label><br>
  <input type="text" id="lname" name="studemail"><br><br>
    <label for="lname">Mobile:</label><br>
  <input type="text" id="lname" name="studmobile"><br><br>
    <label for="lname">Address:</label><br>
  <textarea id="w3review" name="studaddress" rows="4" cols="50">

</textarea><br><br>
  <input type="submit" value="Submit">
</form> 
</body>
</html>

results.php

<?php

$servername = "localhost";
$username = "root";
$password = "XXXXXXX";
$dbname = "students";

$name = $_REQUEST['studname'];
$mobile = $_REQUEST['studmobile'];
$email = $_REQUEST['studemail'];
$address =$_REQUEST['studaddress'];

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO stud (name, email, mobile, address)
VALUES ('$name', '$email', '$mobile', '$address')";

if ($conn->query($sql) === TRUE) {
  echo "New record created successfully";
} else {
  echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();



//print_r($_REQUEST);



?>