使用php将数据插入到mysql中 [英] Inserting data into mysql using php

查看:127
本文介绍了使用php将数据插入到mysql中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将表单数据加载到我的数据库时遇到了困难。
我试图使用下面的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'];

$ b $ 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 =剧院创建。;
}
}
?>

以下是我的html代码:

 <!DOCTYPE html> 
< html>

< body>

<! - 创建影院的表格 - >
< div class =register-form>
<?php
if(isset($ msg)&!empty($ msg)){
echo $ msg;
}
?>
< form action =theatredb.phpmethod =POST>
< p>< label>剧院名称:< / label>
< input type =textname =theatre_nameplaceholder =剧院名称/>< / p>

< p><标签>电话号码:< / label>
< input type =textname =phone_numberplaceholder =Phone Number/>< / p>

< p>< label>网站:< / label>
< input type =textname =websiteplaceholder =Website/>< / p>

< p>< label>屏幕数量:< / label>
< input type =textname =num_screensplaceholder =屏幕数量/>< / p>

< p>< label>地址:< / label>
< input type =textname =addressplaceholder =Address/>< / p>

< p>< label>城市:< / label>
< input type =textname =cityrequired placeholder =City Name/>< / p>




< input class =btn registertype =submitname =submitvalue =done/>
< / form>
< / div>
< / body>
< / html>

我想知道是否有人可以给我一些指导,说明我做错了什么。我一直坚持这个问题几个小时,并不知道我在做什么错。



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

您的查询变量应该如下所示:

  $ 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() mysql_query()从PHP v5.5开始折旧。 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.

解决方案

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.

这篇关于使用php将数据插入到mysql中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆