为什么我的$ _GET表单不会将数据(使用PHP)插入到MySql数据库或表中?
Here is the html form:
<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:
<?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. 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)
Mysql functions that you are using are not correct ...
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
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
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');
Corrections.
- close method
attribute
in formmethod="GET"
-
insert statement as below
"INSERT INTO Users(Username, Password, Email) VALUES('".$_GET['username']."','".$_GET['password']."','".$_GET['email']."')";
mssql_connect
should bemysql_connect
andmssql_select_db
should bemysql_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]
this will look for username constant which may not be defined in code.