简体   繁体   English

MYSQL PHP插入表

[英]MYSQL PHP inserting into table

<?php
$conn= new mysqli("localhost", "my_user", "my_password", "world"); //changed for the sake of this question

$username = $_POST['Username'];
$password = sha1($_POST['Password']);
$email = $_POST['Email'];
$firstname = $_POST['FirstName'];
$lastname = $_POST['LastName'];

$insert = 'INSERT INTO User(FirstName, LastName, Email, Username, Password, Type) VALUES ("'.$firstname.'", "'.$lastname.'", "'.$email.'", "'.$username.'", "'.$password.'", 'User');';

$result = $conn->query($insert);

?>
<form method='post' action='regprocess.php'>
<fieldset class="register">
<h2>Register</h2>
<ul>
    <li><label for="FirstName">First Name: </label> <input type="text" name="FirstName" id="FirstName"></li>
    <li><label for="LastName">Last Name: </label> <input type="text" name="LastName" id="LastName"></li>
    <li><label for="Email">Email: </label><input type="email" name="Email" id="Email"></li>
    <li><label for="Username">Username: </label><input type="text" name="Username" id="Username"></li>
    <li><label for="Password">Password: </label><input type="password" name="Password" id="Password"></li>
    <li><input type="submit" value="Register"></li>
</ul>
</fieldset></form>

The form and the top sql code are in separate files. 表单和最上面的sql代码位于单独的文件中。

Hello everybody, I'm trying to insert into an mysql table, and it won't insert into my table. 大家好,我正尝试插入mysql表中,而不会插入到我的表中。 I'm trying to get it to insert through a registration table. 我正在尝试使其通过注册表插入。 And I'm not quite sure why it's not working. 而且我不太确定为什么它不起作用。 Some insight would be great. 一些见识会很棒。 If you need me to provide the table I will, but I don't think it's part of the reason it's not working. 如果您需要我提供桌子,我会提供,但我不认为这是无法使用的部分原因。

It's a good thing you're using mysqli , but you're using it incorrectly and are exposing yourself to a number of very serious SQL injection bugs, the consequences of which could be severe. 使用mysqli是一件好事,但使用不正确,会使自己暴露于许多非常严重的SQL注入错误中,其后果可能很严重。

This is what you should be doing to actually fix the numerous problems present in your example: 这是您要实际解决示例中存在的众多问题时应采取的措施:

$stmt = $conn->prepare('INSERT INTO User(FirstName, LastName, Email, Username, Password, Type) VALUES (?,?,?,?,?,?)');

$stmt->bind_param($firstname, $lastname, $email, $username, $password, 'User');
$stmt->execute();

$result = $stmt->get_result();

The primary advantage of placeholders is not having to worry about how to properly quote data, it's done for you automatically. 占位符的主要优点是不必担心如何正确引用数据,它会自动为您完成。 It also largely avoids having to use two different kinds of quotes within your statement. 它还在很大程度上避免了在语句中使用两种不同类型的引号。

If you do not use placeholders for ANY and ALL data being put into your SQL you may end up in serious trouble. 如果您不对SQL中放入的ANYALL数据使用占位符,则可能会遇到严重的麻烦。 You must be vigilant about this. 您必须对此保持警惕。

Your last parameter value is in single quotes. 您的最后一个参数值用单引号引起来。 Replace with double quotes so that it reads ..."'.$password.'", "User");'; 替换为双引号,使其读为...“'。$ password。'”,“ User”);';

I don't know PHP. 我不懂PHP。 I will try helping you with MySQL though. 我会尽力帮助您使用MySQL。

I think there is a problem with quotation marks in the insert query. 我认为插入查询中的引号有问题。

Try removing single quotes from your values clause. 尝试从values子句中删除单引号。

As an advice, always use " from the start to the end of the sql query text; 作为建议,请在sql查询文本的开头到结尾始终使用“;

$insert = 'INSERT INTO User(FirstName, LastName, Email, Username, Password, Type) VALUES ("'.$firstname.'", "'.$lastname.'", "'.$email.'", "'.$username.'", "'.$password.'", 'User');';

put it like: 像这样说:

$insert = "INSERT INTO User(`FirstName`, `LastName`, `Email`, `Username`, `Password`, `Type`) VALUES ('$firstname', '$lastname', '$email', '$username', '$password', 'User');";

In your query, you had 'User' and you need to escape the ' as \\' 在您的查询中,您拥有“用户”,并且需要将“ as”转义

And dont forget to always sanitize your content before adding it to the database 并且不要忘记在将内容添加到数据库之前总是对其进行清理

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

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