[英]Don't insert to MySQL with php
I'm creating simple registration form. 我正在创建简单的注册表格。 But I have problems with inserting data to MySQL table.
但是我在将数据插入MySQL表时遇到问题。
if (isset($_POST['name']) && isset($_POST['email'])){
$userName = $_POST['name'];
$email = $_POST['email'];
$company = $_POST['company'];
include '../template/sql-connect.php';
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if(!db_server) { die("Query error"); }
$query = "INSERT INTO users VALUES" .
"('$userName', '$email', '$company')";
if (!mysql_query($query, $db_server))
echo "INSERT failed: $query<br>" .
mysql_error() . "<br><br>";
mysql_close($db_server);
}
Help me to resolve this problem, please. 请帮助我解决这个问题。 Thank you!
谢谢!
I usually make a special file for database connection like this: 我通常为数据库连接制作一个特殊的文件,如下所示:
<?php
$db_name='name';
$server='server';
$username='name';
$password='pass';
$link=mysqli_connect($server,$username,$password,$db_name);
if(!link){
die('Something went wrong'.mysqli_error($link));
} ?>
And then include it in the page I want to make database inserts or selections from. 然后将其包含在我要从中进行数据库插入或选择的页面中。 So if I need to make a insertion on lets say login_do.php I will just include the database file in like this
因此,如果需要插入一个文件,例如login_do.php,我将在其中包含数据库文件
<?php
include_once 'databaseConn.php';
.
.
. and so on ...
And try making your SQL statement like this. 并尝试使您的SQL语句像这样。
$query = "INSERT INTO users (username,email,company) VALUES" .
"('".$userName."','".$email."','".$company."')";
You need to specify to which index in the database you are sending some value and then write them in the same order. 您需要指定要向数据库中的哪个索引发送一些值,然后以相同的顺序将它们写入。
I hope this works for you and I answered your question. 希望这对您有用,并回答了您的问题。 Good luck and keep coding ;)
祝你好运,并保持编码;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.