简体   繁体   中英

Inserting data into mysql using php

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.

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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