繁体   English   中英

php表单数据未提交到数据库

[英]php form data not being submitted to DB

这是我的第三篇文章,我只想感谢大家的耐心和帮助。

所以这是我的问题,我正在尝试将此表单提交给自己,然后在提交时将数据传递给数据库。

我没有在提交时看到添加到数据库的任何记录。

<!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>

首先尝试一下,您首先要更改表单操作

<form class="insert" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

并将插入查询移到

if (isset($_POST['submit']))
{

  // insert query
}

您拥有的是$ sql_site,用于存储mySQL命令。 $ 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);

?>

声明$sql_site后添加此行

mysqli_query($con,$sql_site);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM