【问题标题】:Inserting data into mysql using php使用php将数据插入mysql
【发布时间】:2014-05-14 14:59:54
【问题描述】:

我在尝试将表单数据加载到我的数据库时遇到了困难。 我正在尝试使用以下 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>      

我想知道是否有人可以就我做错的事情给我一些指导。我已经被这个问题困扰了几个小时,不知道我做错了什么。

编辑:我没有得到一个错误的说法,但数据没有上传到数据库中。由于某种原因,我的查询不起作用。

【问题讨论】:

标签: php html mysql database forms


【解决方案1】:

试试这个

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

} ?>

链接single quoted

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

【讨论】:

    【解决方案2】:

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

    $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.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-25
      • 2012-04-20
      相关资源
      最近更新 更多