简体   繁体   English

PHP查询插入字符串到数据库

[英]php query to insert string into database

<?php

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

if($username&&$password)
{

$connect = mysql_connect("CiniCraftData.db.55555555.hostedresource.com", "CiniCraftData", "*********") or die("Couldn't Connect");
mysql_select_db("CiniCraftData") or die ("Couldn't Find Database"); 

            $query = "INSERT INTO CiniUsers ('username.CINIDAT') VALUES('$username')";
            $result = mysql_query($query) or die("Error occurred.");

}
else die("Please enter a username and password.");

?>

For this part of the code: 对于这部分代码:

$query = "INSERT INTO CiniUsers ('username.CINIDAT') VALUES('$username')";

The VALUES seem to not be working properly, I need whatever the string value of $username is to be inserted into my CiniUsers database. VALUES似乎无法正常工作,我需要将$ username的字符串值插入CiniUsers数据库中。 What do I need to do to make the code above work? 为了使上述代码正常工作,我需要做什么? I'm very new to php and sql syntax and the guides I'm finding online are all completely different from each other as if they keep updating php. 我对php和sql语法非常陌生,在网上找到的指南彼此完全不同,就好像它们一直在更新php一样。

Try reviewing this part: 尝试查看此部分:

$query = "INSERT INTO CiniUsers ('username.CINIDAT') VALUES('$username')";

The syntax is: 语法为:

$query = "INSERT INTO table (column) VALUES ('$strvar')";

What is the column name you wanted to insert into? 您想插入的列名是什么?
If it is username.CINIDAT then try removing the qoutes. 如果是username.CINIDAT则尝试删除qoutes。

Like this: 像这样:

$query = "INSERT INTO CiniUsers (username.CINIDAT) VALUES ('$username')";

or maybe your column is named username so: 或者您的列被命名为username所以:

$query = "INSERT INTO CiniUsers (username) VALUES ('$username')";

UPDATE 更新
The query from your comment, change it to this: 您评论中的查询,将其更改为:

$query = "INSERT INTO CiniUsers (username.CINIDAT) VALUES ('$username')";

The format for the SQL statement is as so: SQL语句的格式如下:

INSERT INTO nameOfTable (column1, column2, column3, etc) VALUES ('column1', 'column2', 'column3', 'etc')

You MUST make sure that you are using the field names exactly as they are stored in MySQL. 您必须确保您使用的字段名称与存储在MySQL中的字段名称完全相同。 Your SQL could appear like so: 您的SQL可能如下所示:

$query = "INSERT INTO CiniUsers (username) VALUES('$username')";

OR 要么

$query = "INSERT INTO CiniUsers (username) VALUES('{$username}')";

Another thing that may help is that your die() statement is not very helpful. 可能有所帮助的另一件事是,您的die()语句不是很有帮助。 Yes, it is a bummer when your php program quits early, but it will save you a lot of time and frustration if you know why it quit. 是的,当您的php程序提早退出时,它简直是无奈,但是如果您知道退出的原因,它将为您节省很多时间和沮丧。 Although you may still be learning PHP and MySQL and may not know what the errors mean, they will start to make sense the more you see them and can tell you whether your query was bad, the connection failed or many more things. 尽管您可能仍在学习PHP和MySQL,并且可能不知道错误的含义,但是您看到的越多,它们就会变得有意义,并且可以告诉您查询是否错误,连接失败或更多其他内容。 Change to something like this: 更改为以下内容:

$connect = mysql_connect("CiniCraftData.db.55555555.hostedresource.com", "CiniCraftData", "*********") or die("Couldn't Connect: mysql_error()");
mysql_select_db("CiniCraftData") or die ("Couldn't Find Database: mysql_error()");
...
$result = mysql_query($query) or die("Some kind of error occurred...Query failed: mysql_error()");

You find that seeing the mysql_error() will help you solve problems like this much faster. 您发现看到mysql_error()将帮助您更快地解决此类问题。

USE phpMyAdmin to test your query out, your query may be working perfectly. 使用phpMyAdmin测试您的查询,您的查询可能运行良好。 It is really the only way to know for sure. 确实,这是唯一可以肯定的方法。 Use the suggested SQL and replace the PHP variable with some dummy data like "testUsername_1". 使用建议的SQL并将PHP变量替换为一些虚拟数据,例如“ testUsername_1”。 If the query works, you will have manually added the username to the db, if not, the problem lies in SQL statement. 如果查询有效,则将用户名手动添加到数据库中,否则,问题出在SQL语句中。

Here is some documentation on SQL INSERT INTO statements if you need more details: http://www.w3schools.com/sql/sql_insert.asp 如果需要更多详细信息,请参见以下有关SQL INSERT INTO语句的文档: http : //www.w3schools.com/sql/sql_insert.asp

I think you should use mysqli or pdo. 我认为您应该使用mysqli或pdo。 This liberary you are using is deprecated. 您正在使用的此库已弃用。

That said, what is username.CINIDAT? 就是说,username.CINIDAT是什么? I think this is where your problem is. 我认为这就是您的问题所在。 It should be something like this 应该是这样的

$query = "INSERT INTO CiniUsers (username) VALUES('$username')"; $ query =“在CiniUsers(用户名)VALUES('$ username')中插入”

I am assuming that CiniUsers is the table name and username is the column name. 我假设CiniUsers是表名,而username是列名。

The simplest way is to build the query by concatenating the statement with the value. 最简单的方法是通过将语句与值连接来构建查询。

$query = "INSERT INTO CiniUsers ('username.CINIDAT') VALUES('".$username."')";

Without validation, this is not a very good idea, or something like this is very easy. 未经验证,这不是一个很好的主意,或者类似这样是很容易的。

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

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