[英]Can not Insert data into MySQL from PHP form
After I fill up the PHP form, the data is not showing in mysql.在我填写 PHP 表格后,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.
请帮助我,我仍然是 php 的初学者。 I tried searching my error in other websites however it is not working.
我尝试在其他网站上搜索我的错误,但它不起作用。
This is the code for the form index.php这是表格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这是 mysql 连接数据库的代码。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这是在 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>';
}
?>
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.问题已解决:显然,我没有检查表的结构(例如数据类型),这就是为什么数据在 mysql 中不可见的原因。
You have given wrong file name in forms action on index.php您在 forms 对 index.php 的操作中给出了错误的文件名
You have to write action="createdata.php" in form on index.php您必须在 index.php 上的表单中写入 action="createdata.php"
Form action should be like this:表单动作应该是这样的:
form action = "createdata.php" method="POST"表单动作=“createdata.php”方法=“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')"; $queryCreate = "插入供应商信息(Supplier_Name,Supplier_Contact,Supplier_StreetNo,Supplier_Province,Supplier_PostalCode,Supplier_Country)值('$Supplier_name','$Supplier_Contact','$Supplier_StreetNo','$Supplier_Prov','$Supplier_PostalCode','$Supplier_Country ')";
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.您仅将 mysqli_query($connection, $queryCreate) 分配给 PHP 变量,但您没有执行它。
Try this尝试这个
if(mysqli_query($connection, $queryCreate)){
echo '<script>alert("Successfully created!")</script>';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.