这是我的第3篇帖子,我只是想感谢大家耐心帮助我。
所以这是我的问题,我正在尝试将此表单提交给自己并在提交时将数据传递给数据库。
我没有在提交中看到任何记录添加到数据库中。
<!DOCTYPE html>
<?php
$con=mysqli_connect("localhost","root","","project1");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// check variables set
if (isset($_POST['submit']))
{
$site_code = $_POST['site_code'];
$site_name = $_POST['site_name'];
$site_address = $_POST['site_address'];
$site_city = $_POST['site_city'];
$site_postalcode = $_POST['site_postalcode'];
$province = $_POST['province'];
$country = $_POST['country'];
}
// Query from Countries table
$query_countries = "select * from countries";
$country_results = mysqli_query($con,$query_countries);
$number_of_returns_country = mysqli_num_rows($country_results);
// Query from Provinces Table
$query_provinces = "select * from provinces";
$provinces_results = mysqli_query($con,$query_provinces);
$number_of_returns_province = mysqli_num_rows($provinces_results);
//insert form values into sites table
$sql_site="INSERT INTO sites (site_code, site_name, site_address, site_city, site_postalcode, id_province, id_county)
VALUES
('$_POST[site_code]','$_POST[site_name]','$_POST[site_address]','$_POST[site_city]','$_POST[site_postalcode]','$_POST[province]','$_POST[country]')";
mysqli_query($con,$sql_site);
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="site.css">
</head>
<body>
<h1>Insert Site into DB</h1>
<h2 class="button"><a href=/index.html>Home</a></h2>
<h2 class="button"><a href=/insert.php>add site</a></h2>
<h2 class="button"><a href=/delete.html>delete site</a></h2>
<h2 class="button"><a href=/search.html>search site</a></h2>
<form class="insert" action="insert.php" method="post">
<h3>Site Info</h3>
Site code: <input type="text" name="site_code"><br>
Site name: <input type="text" name="site_name"><br>
Address: <input type="text" name="site_address"><br>
City: <input type="text" name="site_city"><br>
Postal code: <input type="text" name="site_postalcode"><br>
Province: <select name="province">
<?php while($row = mysqli_fetch_assoc($provinces_results)){ ?>
<option value="<?php echo $row['id'];?>"><?php echo $row['province'];?></option>
<?php } ?>
</select><br>
Country: <select name="country">
<?php while($row = mysqli_fetch_assoc($country_results)){ ?>
<option value="<?php echo $row['id'];?>"><?php echo $row['country'];?></option>
<?php } ?>
</select><br>
<h3>Site Contact Info</h3>
Site contact name: <input type="text" name="site_contact_name"><br>
Phone number 1: <input type="number" name="site_contact_number1"><br>
Phone number 2: <input type="number" name="site_contact_number2"><br>
Email address: <input type="email" name="site_contact_email"><br>
<input type="submit">
</form>
</body>
</html>
答案 0 :(得分:1)
试试这个, 首先,您已经更改了表单操作
<form class="insert" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
并在
中移动插入查询if (isset($_POST['submit']))
{
// insert query
}
答案 1 :(得分:1)
你拥有的是存储mySQL命令的$ sql_site。 $ sql_site中的mySQL命令需要使用mysqli_query
函数
//insert values into sites table
$sql_site="INSERT INTO sites (site_code, site_name, site_address, site_city, site_postalcode)
VALUES
('$_POST[site_code]','$_POST[site_name]','$_POST[site_address]','$_POST[site_city]','$_POST[site_postalcode]')";
mysqli_query($con,$sql_site);
?>
答案 2 :(得分:0)
在声明$sql_site
mysqli_query($con,$sql_site);