Skip to content Skip to sidebar Skip to footer

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.

Solution 1:

try this

<?phprequire('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.

Solution 2:

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.

Post a Comment for "Inserting Data Into Mysql Using Php"