Eric Rochon
Eric Rochon

Reputation: 15

php form data not being submitted to DB

this is my 3rd post, I just wanted to thank everybody for being patient and helping me out.

So here's my issue, i'm trying to have this form submit to itself and pass the data to the DB on submit.

I'm not seeing any records added to the DB on the submit.

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

Upvotes: 0

Views: 106

Answers (3)

brietsparks
brietsparks

Reputation: 5006

What you have is $sql_site storing a mySQL command. The mySQL command in $sql_site needs to be run with the mysqli_query function

//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);

?>

Upvotes: 1

Mehul Jethloja
Mehul Jethloja

Reputation: 637

Try This, First Of all you have change the form action

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

and move the insert query inside the

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

  // insert query
}

Upvotes: 1

Matt The Ninja
Matt The Ninja

Reputation: 2731

Add this line after declaring $sql_site

mysqli_query($con,$sql_site);

Upvotes: 0

Related Questions