duai3681
2013-03-14 05:35
浏览 55
已采纳

为什么我的$ _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)

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • dousao6313 2013-03-14 05:43
    已采纳

    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

    打赏 评论
  • doudun3040 2013-03-14 05:37

    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"); 
    
    打赏 评论
  • douwei3172 2013-03-14 05:38

    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');
    
    打赏 评论
  • dtmjqyfz21793 2013-03-14 05:39

    Corrections.

    1. close method attribute in form method="GET"
    2. insert statement as below

      "INSERT INTO Users(Username, Password, Email) VALUES('".$_GET['username']."','".$_GET['password']."','".$_GET['email']."')";

    3. mssql_connect should be mysql_connect and mssql_select_db should be mysql_select_db

    4. $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.

    打赏 评论

相关推荐 更多相似问题