简体   繁体   English

PHP INSERT 查询不适用于 web 服务器,但适用于 localhost。 没有返回错误

[英]PHP INSERT query doesn't work on web server but works on localhost. No errors returned

I am fairly new to php and mysql to feel free to suggest any improvements to my code.我对 php 和 mysql 相当陌生,可以随时建议对我的代码进行任何改进。

Recently I finished a project which works completely fine on my localhost XAMPP server, which is why I decided to then upload it to a web server to make it public.最近我完成了一个项目,该项目在我的本地主机 XAMPP 服务器上运行良好,这就是为什么我决定将其上传到 web 服务器以将其公开。 Now the problem is that all my INSERT queries stop working while they are on the web server.现在的问题是我的所有 INSERT 查询在 web 服务器上时都停止工作。 They seem to work fine on the offline XAMPP on localhost.他们似乎在本地主机上的离线 XAMPP 上工作正常。 While the insert queries don't work, there are no errors returned so I'm really frustrated at this point.虽然插入查询不起作用,但没有返回错误,所以我现在真的很沮丧。

Any help on this would be greatly appreciated.对此的任何帮助将不胜感激。 Below is the code I have used for the insert queries.下面是我用于插入查询的代码。

$firstname = $_POST['firstname'];
$job_role  = $_POST['job_role'];
$password  = $_POST['password'];
$username  = $_POST['username'];

$conn = mysqli_connect("localhost", "root", "password", "id11597687_inventory");

mysqli_query($conn, "INSERT into users (`id`, `username`, `password`, `access_level`, `name` ) VALUES('', '$username', '$password', '$job_role', '$firstname')");

Delete 'id' from (...columns) and '' from (...values) .删除(...columns)中的'id'(...values)''

It should looks like它应该看起来像

mysqli_query($conn, "INSERT into users (`username`, `password`, `access_level`, `name` ) 
VALUES('$username', '$password', '$job_role', '$firstname')");

Auto incrementation makes +1 to the new value of defined column, in your case it's id .自动递增使已定义列的新值+1 ,在您的情况下为id

Also, take care of SQL injection另外,注意SQL 注射

  1. Considering that you are using the values posted from an HTML form, you would like to use something like below -考虑到您正在使用从 HTML 表单发布的值,您想使用如下所示的内容 -

     $var= mysqli_real_escape_string ($con,$_POST['var']);
  2. Assuming that the 'id' field is an auto-increment value - use something like -假设 'id' 字段是一个自动增量值 - 使用类似 -

     $sql = "INSERT INTO users (id, username,password, access_level,name) VALUES (NULL,'$_POST[username]','$_POST[password]','$_POST[access_level]','$_POST[name]')"; mysqli_query($conn,$sql)

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

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