使用php将数据插入mysql

时间:2014-04-03 02:04:42

标签: php html mysql database forms

我一直在尝试将表单数据加载到我的数据库中时遇到困难。 我试图使用以下PHP脚本输入剧院信息。

 <?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.";
        }
    }
?>

以下是我的HTML代码:

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

我想知道是否有人可以给我一些关于我做错的指导。我已经坚持这个问题好几个小时了,不知道我做错了什么。

编辑:我没有说出错误,但数据没有上传到数据库。出于某种原因,我的查询无效。

2 个答案:

答案 0 :(得分:0)

试试这个

     <?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.";
}

} ?&GT;

链接single quoted

注意:与双引号和heredoc语法不同,特殊字符的变量和转义序列在单引号字符串中不会被展开。

答案 1 :(得分:0)

我曾经遇到过同样的问题。您的查询变量应如下所示:

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

说明:在原始查询中,您只需插入$ theatre_name,而不是变量值。为了解决这个问题,你必须关闭字符串,用“将变量连接到前面的字符串,用。,然后重新打开字符串。

另外,我不知道您使用的是哪个版本的PHP,但您应该使用mysqli_query()。自PHP v5.5起,mysql_query()已弃用。 PHP manual entry