我不能添加数据在我的数据库使用PHP [英] I can't add data in my database using php

查看:93
本文介绍了我不能添加数据在我的数据库使用PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有index.html在send.php中发布数据
send.php代码是
表单数据正确命名&发送页上没有错误

I have index.html to post data in send.php the send.php code is the form data is named correctly & no error on send page

<?php
$servername = "localhost";
$username = "Blah Blah";
$password = "Blah Blah";
$dbname = "Blah Blah";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$name = $_POST['name'];
$famil = $_POST['famil'];
$email = $_POST['email'];
$amount = $_POST['amount'];

$sql = "INSERT INTO users".
"(name, famil, email, amount)".
"VALUES ('$name','$famil','$email','$amount')";

mysqli_close($conn);

但查询无法将数据插入db!

but query can't insert the data into db!

为什么post方法不会在这里工作?

why the post method won't work here?

推荐答案

问题是, href =http://php.net/manual/en/mysqli.query.php =nofollow> 查询 - mysqli_query()缺少的是必要部分。

The problem is that you're not querying - mysqli_query() is missing which is the essential part.

您当前的代码确实有效,缺少 mysqli_query()

Your current code is indeed valid, yet SQL failed silently because of the missing mysqli_query().

$sql = mysqli_query($conn,"INSERT INTO users".
"(name, famil, email, amount)".
"VALUES ('$name','$famil','$email','$amount')");

确保您的表单元素也命名,并且您的表单是一个POST方法。 (

Make sure that your form elements are named also and that your form is a POST method. (An insight)

Ie: name =name $ c> name =famil等和< form method =postaction =handler.php> p>


  • 此外,您的现有代码还可以 SQL注入

    使用 mysqli with prepared statements 已准备好的语句的PDO

    它们更安全使用
  • >

    I.e.: name="name", name="famil" etc. and <form method="post" action="handler.php">

    添加 错误报告 到您文件的顶部,这将有助于发现错误。

    • Plus, your present code is open to SQL injection.
      Use mysqli with prepared statements, or PDO with prepared statements,
      they're much safer to use.
    Add error reporting to the top of your file(s) which will help find errors.

    Sidenote: strong>错误报告应该只能在暂存中完成,而不能生成。

    <?php error_reporting(E_ALL); ini_set('display_errors', 1); // rest of your code

    您也可以使用或者(mysqli_error($ conn) / code>至 mysqli_query()


    发送页面上没有错误

    You can also use or die(mysqli_error($conn)) to mysqli_query()




      <您应该随时检查可能的错误。

      "no error on send page"

      用现有代码添加保护,请使用:

      • You should always check for possible errors.
      • For added protection with your existing code, use:




        • >

        • 这样做也有效:

          $sql = "INSERT INTO users".
          "(name, famil, email, amount)".
          "VALUES ('$name','$famil','$email','$amount')";
          
          mysqli_query($conn,$sql);
          

          if (mysqli_query($conn, $sql)) {
              echo "Data entered successfully.";
          } else {
              echo "Error entering data: " . mysqli_error($conn);
          }
          




          • 如你所见,几种查询方式。

          • 这篇关于我不能添加数据在我的数据库使用PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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