PHP ERROR无法将数据从表单发送到mysql

时间:2017-01-06 22:10:23

标签: php mysql

我试图以两种方式做到这一点。我在html和php上使用bootstrap将数据发送到数据库。在发布之前,我仔细阅读stackoverflow并尝试在谷歌中找到的所有内容。请帮忙,我是php的新手并且真的想学习它,但这个麻烦让我在这一刻超过3个小时。抱歉我的英语不好(对于这种情况,我不是来自印度) 第一个变种。 HTML:

    <!DOCTYPE html>
<html lang="en">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
        <meta charset="utf-8">
        <title>Landing Page template</title>
        <meta name="generator" content="Bootply" />
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <link href="css/bootstrap.min.css" rel="stylesheet">
        <link href="//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.min.css" rel="stylesheet">
        <!--[if lt IE 9]>
            <script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
        <link href="css/styles.css" rel="stylesheet">
    </head>
    <body>
    <div class="background-image"></div>
<div class="container-full">

      <div class="row">

        <div class="col-lg-12 text-center v-center">

          <h1>Hello</h1>
          <p class="lead"><u>Sign-up and receive.</u></p>
          <p class="lead">No ads, no paid scum. No Bullshit.</p>
          <br><br><br>

          <form class="col-lg-12" action="dbconnection.php" method="POST">
            <div class="input-group" style="width:340px;text-align:center;margin:0 auto;">
            <input class="form-control input-lg" title="Don't worry. We hate spam, and will not share your email with anyone." placeholder="Your email" type="text" name="name">
              <span class="input-group-btn" >
              <button class="btn btn-lg btn-primary"  type="submit"  
      name="submit" value=" Send">SUBMIT</button></span>

            </div>
          </form>
        </div>

      </div> 
      <div class="row text-center v-center">
       <p class="lead">We Deliver: </p>


      </div>
      <!-- /row -->





    <!-- script references -->
        <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
        <script src="js/bootstrap.min.js"></script>
    </body>
</html>

PHP:

<?php
// connect to the database
$user_name = "root";
$password = "";
$database = "emails";
$host_name = "localhost";
$name = $_POST["name"];
$con = mysqli_connect ("localhost","root","","emails") or die("Error ".mysqli_error($con));
//check connection
$name222 = $_POST["name"];
$name=mysqli_real_escape_string($_POST['name']);
mysqli_query("INSERT INTO emails (email) VALUES ('$name')");
echo"record added";
$result = mysqli_query();
if($result){

    echo("<br>Input data is succeed");

} else{

    echo("<br>Input data is fail");

}
echo "Connection opened";

mysql_close($con); 
?>

错误:

  

警告:mysqli_real_escape_string()只需要2个参数,1   在第11行的C:\ OpenServer \ domains \ landing-cpa \ dbconnection.php中给出

     

警告:mysqli_query()需要至少2个参数,1中给出1   第12行记录中的C:\ OpenServer \ domains \ landing-cpa \ dbconnection.php   添加警告:mysqli_query()需要至少2个参数,给定0   在第14行的C:\ OpenServer \ domains \ landing-cpa \ dbconnection.php

     

输入数据是failConnection打开警告:mysql_close()期望   参数1是资源,给定的对象   第26行的C:\ OpenServer \ domains \ landing-cpa \ dbconnection.php

第二种变体: HTML: 相同 PHP:

 <?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "emails";

// Create connection
$conn = new mysqli($servername, $username, $password, $database);
mysql_select_db("$database");

$name = $_POST["name"];
$order = "INSERT INTO emails

        (email)

        VALUES

        ('$name')";


$result = mysql_query($order);

if($result){

    echo("<br>Input data is succeed");

} else{

    echo("<br>Input data is fail");

}


// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?> 

错误:

  

警告:mysql_select_db():拒绝用户访问&#39;&#39; localhost&#39;   (使用密码:NO)in   第12行的C:\ OpenServer \ domains \ landing-cpa \ dbconnection.php

     

警告:mysql_select_db():无法链接到服务器   建立在C:\ OpenServer \ domains \ landing-cpa \ dbconnection.php上   第12行

     

警告:mysql_query():拒绝用户&#39;&#39; localhost&#39; (使用   密码:NO)在C:\ OpenServer \ domains \ landing-cpa \ dbconnection.php上   第24行

     

警告:mysql_query():无法建立指向服务器的链接   在第24行的C:\ OpenServer \ domains \ landing-cpa \ dbconnection.php

     

输入数据已成功连接

1 个答案:

答案 0 :(得分:0)

我已经这样做了,只需在这里阅读mysqli_real_escape_string http://www.w3schools.com/php/func_mysqli_real_escape_string.asp

获得有效的解决方案 主要错误在于我的语法(mysql_string命令),下面的代码完全有用,mb它将帮助未来的人:

S::Foo