0

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>
5
  • 2
    You still have to execute your insert query. Commented Apr 16, 2014 at 14:30
  • 1
    SQL injection ALERT Commented Apr 16, 2014 at 14:33
  • Thought I'd seen something very similar recently : stackoverflow.com/questions/22865880/php-email-form-not-working/… ... <input type="submit" /> won't actually pass a variable called submit through to PHP unless you give it the name submit, so isset($_POST['submit']) will never evaluate to true and your code will never execute... except it your case it's just that none of the variables in that block will be set. Commented Apr 16, 2014 at 14:40
  • I'm surprised this hasn't thrown you an error [‘site_code’] along with all the others having those funky quotes. Commented Apr 16, 2014 at 15:28
  • I think it's just the way it copied and pasted out Commented Apr 16, 2014 at 15:40

3 Answers 3

1

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

?>
Sign up to request clarification or add additional context in comments.

Comments

1

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
}

Comments

0

Add this line after declaring $sql_site

mysqli_query($con,$sql_site);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.