[英]Why won't my $_GET Form insert the data (using PHP) into the MySql Database or Table?
Here is the html form: 这是html表单:
<form action="register.php" METHOD=GET align="center">
<table border="2" align="center">
<tr>
<td>Desired Username:</td><td><input name="username" type="text" size"20"></input></td>
</tr>
<tr>
<td>Your Email:</td><td><input name="email" type="text" size"30"></input></td>
</tr>
<tr>
<td>Desired Password:</td><td><input name="password" type="password" size"20"></input> </td>
</tr>
<tr>
<td>Confirm Password:</td><td><input name="password2" type="password" size"20"></input> </td>
</tr>
</table>
<input type="submit" value="Register!" align="center"></input>
</FORM>
And here is the PHP code on register.php: 这是register.php上的PHP代码:
<?php
$myServer = "localhost";
$myUser = "Censored";
$myPass = "Censored";
$myDB = "Censored";
//connection to the database
$conn = "mssql_connect($myServer, $myUser, $myPass)
or die('Couldn't connect to SQL Server on $myServer')";
//select a database to work with
$selected = "mssql_select_db($myDB, $conn)
or die('Couldn't open database $myDB')";
//add new user to DB
if($_GET["username"] && $_GET["email"] && $_GET["password"] && $_GET["password2"] )
{
if($_GET["password"]==$_GET["password2"])
{
$sql="INSERT INTO Users(Username, Password, Email) VALUES('$_GET[username]','$_GET[password]','$_GET[email]')";
$result="mysql_query($sql,$conn) or die (mysql_error())";
}
if ($result)
{
echo "<h1 align='center'>Registration Successful!</h1>";
echo "<p align='center'>Click<a href='index.php'> here</a> to log in.</p>";
}
else echo "Oops! Something went wrong. Please <a href='http://www.canilosa.zxq.net/index.php'>go back</a> and fix it.";
}
?>
I am a beginner in PHP and MySql. 我是PHP和MySql的初学者。 I have checked and rechecked all the usernames, passwords, databases, etc. The echo works and it says the registration is successful, but when I check my database there is no data.
我已检查并重新检查所有用户名,密码,数据库等。回声有效,它表示注册成功,但当我检查我的数据库时,没有数据。
Thanks SO much to everyone who answered!!! 非常感谢所有回答的人! It works fine now :).
它现在工作正常:)。 As I said, I am new to all of this so most of the code was from snippets of tutorials and I just kind of skimmed it to see if it made sense.
正如我所说的,我是所有这些的新手所以大部分代码来自教程的片段,我只是撇去它看看它是否有意义。 Thanks again!
再次感谢! (I'm creating a game website with lots of PHP-MySql...so I'll probably be back here soon enough xD)
(我正在创建一个包含大量PHP-MySql的游戏网站......所以我很快就会回到这里xD)
change this 改变这一点
<form action="register.php" METHOD=GET align="center">
to 至
<form action="register.php" METHOD="GET" align="center">
^ ^
also should be 也应该
$conn = mysql_connect($myServer, $myUser, $myPass) or die("Couldn't connect to SQL Server on $myServer");
//select a database to work with
$selected = mysql_select_db($myDB, $conn) or die("Couldn't open database $myDB");
You have used mssql_connect
and mssql_select_db
but the actual command is mysql_connect
and mysql_select_db
您使用过
mssql_connect
和mssql_select_db
但实际命令是mysql_connect
和mysql_select_db
Just replace your code with this 只需用这个代替你的代码
//connection to the database
$conn = mysql_connect($myServer, $myUser, $myPass)
or die('Couldn't connect to SQL Server on $myServer');
//select a database to work with
$selected = mysql_select_db($myDB, $conn)
or die('Couldn't open database $myDB');
Mysql functions that you are using are not correct ... 您正在使用的Mysql函数不正确...
change 更改
mssql_connect($myServer, $myUser, $myPass)
to 至
mysql_connect($myServer, $myUser, $myPass)
AND 和
mssql_select_db($myDB, $conn)
to 至
mysql_select_db($myDB, $conn)
DOCUMENTATION LINK http://www.w3schools.com/php/php_ref_mysql.asp 文档链接http://www.w3schools.com/php/php_ref_mysql.asp
Corrections. 更正。
attribute
in form method="GET"
method="GET"
close方法attribute
insert statement as below 插入语句如下
"INSERT INTO Users(Username, Password, Email) VALUES('".$_GET['username']."','".$_GET['password']."','".$_GET['email']."')";
mssql_connect
should be mysql_connect
and mssql_select_db
should be mysql_select_db
mssql_connect
应该是mysql_connect
而mssql_select_db
应该是mysql_select_db
$result= mysql_query($sql,$conn) or die (mysql_error());
Difference. 区别。
$_GET['username']
this will look for username value passed in url as parameter. $_GET['username']
这将查找在url中传递的用户名值作为参数。
$_GET[username]
this will look for username constant which may not be defined in code. $_GET[username]
这将查找可能未在代码中定义的用户名常量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.