简体   繁体   English

PHP脚本未将值插入到mySQL数据库中

[英]PHP script not inserting values into mySQL database

my insert isnt working, It ownt insert anything into the DB no matter what.我的插入不起作用,无论如何它都不会将任何内容插入数据库。

$sql= "INSERT INTO 'users' (`id`, `username`, `password`) VALUES (NULL,   '$newusername', '$newpassword')";

What did i do wrong?我做错了什么? :( And yes i have connected to the DB earlier in the script. :( 是的,我已经在脚本的前面连接到了数据库。

Edit: Heres my whole .php编辑:这是我的整个 .php

<?php
if (isset($_POST['submit'])){

    $newusername = $_POST['newusername'];
    $newpassword = $_POST['newpassword'];
    $newpassword2 = $_POST['confirmnewpassword'];

    $query = "SELECT * FROM users WHERE username = '$newusername' AND password = '$newpassword'";
if(strlen($newusername) > 5 || strlen($newusername)< 25){
echo "username length check is fine<br>";
    if(strlen($newpassword) > 4 || strlen($newpassword2) < 25){
        echo "Pass check worked lel<br>";
        if($newpassword == $newpassword2 ){
            echo "Should all be working!<br>";
            $connect = mysqli_connect("localhost","root","") or die("Couldnt connect!");
            mysqli_select_db($connect,"phplogin") or die("Couldnt find DB.");

            $sql="INSERT INTO users (username,password) VALUES('$newusername','$newpassword')";


        }
        else
            die("Passwords did not match");
    }
    else
        die('Password wasnt long enough');
}
    else
        die("Username was not long enough");

}

Sorry for the messy code.抱歉代码混乱。 i get a feeling ive left something very obvious out.我有一种感觉,我遗漏了一些非常明显的东西。

At the PHP level:在 PHP 级别:

In PHP you should make a habbit of setting up error levels to let you know when things become weird:在 PHP 中,您应该养成设置错误级别的习惯,以便在事情变得奇怪时通知您:

error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('scream.enabled', false);

With these settings you'll start seeing some PHP errors such as trying to access the contents of arrays that you expect to be populated by the DB but in fact are not (and they may not even be arrays).通过这些设置,您将开始看到一些 PHP 错误,例如尝试访问您希望由数据库填充但实际上不是(它们甚至可能不是数组)的数组的内容。 These are tip-offs to get you thinking that an SQL query has failed for some reason.这些提示让您认为 SQL 查询由于某种原因失败了。

At the DB level:在数据库级别:

Most likely you database is telling you exactly what is going on, you're just not checking what it's saying.您的数据库很可能会准确地告诉您发生了什么,而您只是没有检查它在说什么。 Errors from the database manifest themselves in 3 ways: empty results, false instead of expected result, an error message available to you if you check for it.来自数据库的错误以 3 种方式表现出来:空结果,错误而不是预期结果,如果您检查它,您可以获得错误消息。

Check for errors after each step:在每一步之后检查错误:

  • connect to DB;连接到数据库;
  • select DB (in case you're using mysql_* or mysqli_* functions);选择 DB(如果您使用的是mysql_*mysqli_*函数);
  • prepare statement (in case you're using mysqli_ or PDO );准备语句(如果您使用的是mysqli_PDO );
  • execute query;执行查询;

I will assume you're using mysqli_* functions and give an example (original here ):我将假设您正在使用mysqli_*函数mysqli_*一个例子(原文在这里):

/** somewhere in the DB connection file */
$mysqlErrors = array();

/*...*/

/** somewhere in your logic files */
$rows = mysqli_query($link, "SET a=1");
if ($rows === false) {
    $mysqlErrors[] = mysqli_error($link);
}

/*...*/

/** somewhere in your template files */
foreach ($mysqlErrors as $mysqlError) {
    printf("Errormessage: %s\n", $mysqlError);
}
(1) do this,  id as auto increment then no need to include at insert time 

$sql= "INSERT INTO 'users' ( `username`, `password`) VALUES ('$newusername', '$newpassword')";
$sql= "INSERT INTO 'users' ( `username`, `password`) VALUES ('$newusername', '$newpassword')";

// add the following code below the line above

$result = mysqli_query($con, $sql);

Use Apostrophe instead of Grave accent ` = Grave accent, or (because someone needed to invent a word) backtick ' = Apostrophe, or straight single quote使用撇号代替重音符 ` = 重音符,或(因为有人需要发明一个词)反引号 ' = 撇号,或直接单引号

(`id`, `username`, `password`)

You should use this你应该用这个

('id', 'username', 'password')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM