-1

After I fill up the PHP form, the data is not showing in mysql. What is wrong with my code and how can i fix it?

These are all my codes. Please help me I am still a beginner in php. I tried searching my error in other websites however it is not working.

This is the code for the form index.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
  <!-- C R E A T E  D A T A -->
  <form class="" action="createdatatry.php" method="post">
    <h3>ENTER THE FOLLOWING SUPPLIER INFORMATION:</h3>
    <input type="text" name="Supplier_Name" placeholder="Enter Supplier Name" required/>
    <input type="text" name="Supplier_Contact" placeholder="Enter Contact No." required/>
    <input type="text" name="Supplier_StreetNo" placeholder="Enter Street No." required/>
    <input type="text" name="Supplier_Province" placeholder="Enter Province" required/>
    <input type="text" name="Supplier_PostalCode" placeholder="Enter Postal Code" required/>
    <input type="text" name="Supplier_Country" placeholder="Enter Country" required/>
    <input type="submit" name="create" value="CREATE">
  </form>
</body>
</html> 

This is the code for the mysql connection database.php

<?php
    
$host = 'localhost';
$user = 'root';
$password = '';
$database = 'sourcingdb';

$connection = mysqli_connect($host, $user, $password, $database);

if (mysqli_connect_error()) {
    echo "Error Unable to connect to MySQL server <br>";
    echo "Message: ".mysqli_connect_error()."<br>";
} 


?>

This is the code in creating/ inserting data into mysql createdatatry.php

<?php

require('./database.php');

if (isset($_POST['create'])) {
    $Supplier_Name = $_POST['Supplier_Name'];
    $Supplier_Contact = $_POST['Supplier_Contact'];
    $Supplier_StreetNo = $_POST['Supplier_StreetNo'];
    $Supplier_Prov = $_POST['Supplier_Prov'];
    $Supplier_PostalCode = $_POST['Supplier_PostalCode'];
    $Supplier_Country = $_POST['Supplier_Country'];

    $queryCreate = "INSERT INTO supplierinfo (`Supplier_Name`, `Supplier_Contact`, `Supplier_StreetNo`, `Supplier_Province`, `Supplier_PostalCode`, `Supplier_Country`) VALUES ('$Supplier_name', '$Supplier_Contact', '$Supplier_StreetNo', '$Supplier_Prov', '$Supplier_PostalCode', '$Supplier_ountry')";
    $sqlCreate = mysqli_query($connection, $queryCreate);

    echo '<script>alert("Successfully created!")</script>';
    //echo '<script>window.location.href = "/sourcing/index.php"</script>';
}

?>
Barmar
  • 741,623
  • 53
  • 500
  • 612

4 Answers4

0

Problem solved: Apparently, I did not check the structure of my table (ex. data types) that is why the data is not visible in mysql.

-1

You have given wrong file name in forms action on index.php

You have to write action="createdata.php" in form on index.php

-1

Form action should be like this :

form action = "createdata.php" method="POST"

Your query should be like this :

$queryCreate = "INSERT INTO supplierinfo (Supplier_Name, Supplier_Contact, Supplier_StreetNo, Supplier_Province, Supplier_PostalCode, Supplier_Country) VALUES ('$Supplier_name', '$Supplier_Contact', '$Supplier_StreetNo', '$Supplier_Prov', '$Supplier_PostalCode', '$Supplier_Country')";

Vincent
  • 1
  • 3
-1

For your query

$queryCreate = "INSERT INTO supplierinfo (`Supplier_Name`, `Supplier_Contact`, `Supplier_StreetNo`, `Supplier_Province`, `Supplier_PostalCode`, `Supplier_Country`) VALUES ('$Supplier_name', '$Supplier_Contact', '$Supplier_StreetNo', '$Supplier_Prov', '$Supplier_PostalCode', '$Supplier_ountry')";
    $sqlCreate = mysqli_query($connection, $queryCreate);

You only assigned mysqli_query($connection, $queryCreate) to a PHP variable , but you didnt execute it.

Try this

if(mysqli_query($connection, $queryCreate)){
  echo '<script>alert("Successfully created!")</script>';
}
Steve Kush
  • 143
  • 1
  • 13