0

Ive been having difficulties trying to load form data into my database. Im trying to input theatre info using the following php script.

 <?php
     require('connect.php');

    if (isset($_POST['theatre_name']) && isset($_POST['website'])){
        $theatre_name = $_POST['theatre_name'];
        $phone_number = $_POST['phone_number'];
        $website = $_POST['website'];
        $num_screens = $_POST['num_screens'];
        $address = $_POST['address'];
        $city = $_POST['city'];


        $queryd = "INSERT INTO `Theatres` (theatre_name, phone_number, website,
                                           num_screens, address, city)
                  VALUES ('$theatre_name', '$phone_number', '$website', '$num_screens',
                          '$address', '$city')";
        $result = mysql_query($queryd);
        if($result){
            $msg = "Theatre created.";
        }
    }
?>

The following is my html code:

     <!DOCTYPE html>
<html>

<body>

   <!-- Form for creating theaters -->
<div class="register-form">
<?php
  if(isset($msg) & !empty($msg)){
    echo $msg;
   }
 ?>
<form action="theatredb.php" method="POST">
 <p><label>Theater Name : </label>
<input type = "text" name= "theatre_name" placeholder= "Theater Name" /></p>

<p><label>Phone Number : </label>
<input type = "text" name= "phone_number" placeholder="Phone Number" /></p>

<p><label>Website : </label>
    <input type="text" name= "website" placeholder ="Website" /></p>

<p><label> Number of Screens  : </label>
    <input type= "text" name="num_screens" placeholder ="Number of screens" /></p>

<p><label>Address : </label>
<input type="text" name="address" placeholder="Address" /></p>

<p><label>City : </label>
 <input  type="text" name="city" required placeholder="City Name" /></p>




<input class="btn register" type="submit" name="submit" value="done" />
</form>
 </div>
</body>
</html>      

I was wondering if anyone could give me some guidance with regards to what I'm doing wrong. Ive been stuck with this problem for hours and don't know what I'm doing wrong.

EDIT: I dont get an error per say, but the data does not get uploaded into the database. For some reason my query isnt working.

Leo Silence
  • 1,192
  • 11
  • 22
harman litt
  • 47
  • 2
  • 11
  • 2
    What error do you get? You should be checking for errors in your code with `mysql_error()`. – John Conde Apr 03 '14 at 02:05
  • 1
    [Please, don't use `mysql_*` functions in new code](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [red box](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). **You are also wide open to [SQL injections](http://stackoverflow.com/q/60174)** – John Conde Apr 03 '14 at 02:05

2 Answers2

0

try this

     <?php
 require('connect.php');

         if (isset($_POST['theatre_name']) && isset($_POST['website'])){
$theatre_name = $_POST['theatre_name'];
$phone_number = $_POST['phone_number'];
$website = $_POST['website'];
$num_screens = $_POST['num_screens'];
$address = $_POST['address'];
$city = $_POST['city'];

//**change code to below**
$queryd = "INSERT INTO `Theatres` (theatre_name, phone_number, website, num_screens, address, city) VALUES ('{$theatre_name}', '{$phone_number}', '{$website}', '{$num_screens}', '{$address}', '{$city}')";
$result = mysql_query($queryd);
if($result){
    $msg = "Theatre created.";
}

} ?>

link single quoted

Note: Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.

Liuqing Hu
  • 1,970
  • 3
  • 11
  • 15
0

I once had the same issue. Your query variable should look like this:

$queryd = "INSERT INTO `Theatres` (theatre_name, phone_number, website, 
                                 num_screens, address, city)
                VALUES ('".$theatre_name."', '".$phone_number."', '".$website."',
                        '".$num_screens."', '".$address."', '".$city."')";

Explanation: In your original query, you would have just inserted literally $theatre_name, not the variables value. In order to get around this, you have to close the string, with ", concatenate the variable to the preceding string, with . , and then re open the string.

Also, I don't know what version of PHP you are using, but you should be using mysqli_query(). mysql_query() is depreciated as of PHP v5.5. PHP manual entry.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Andrew
  • 53
  • 6