1

这是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>

这是 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.";
}
?>

我是 PHP 和 MySql 的初学者。我检查并重新检查了所有用户名、密码、数据库等。回显有效,它说注册成功,但是当我检查我的数据库时没有数据。


非常感谢所有回答的人!!!它现在工作正常:)。正如我所说,我对所有这些都是新手,所以大部分代码都来自教程片段,我只是略读了一下,看看它是否有意义。再次感谢!(我正在创建一个包含大量 PHP-MySql 的游戏网站......所以我可能很快就会回到这里 xD)

4

4 回答 4

2

改变这个

<form action="register.php" METHOD=GET align="center">

<form action="register.php" METHOD="GET" align="center">
                                   ^   ^

也应该是

$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"); 
于 2013-03-14T05:37:12.563 回答
1

你已经使用了mssql_connectandmssql_select_db但实际的命令是mysql_connectandmysql_select_db

只需用此替换您的代码

//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');
于 2013-03-14T05:38:39.060 回答
1

您使用的 Mysql 函数不正确...

改变

mssql_connect($myServer, $myUser, $myPass)

mysql_connect($myServer, $myUser, $myPass)

mssql_select_db($myDB, $conn)

mysql_select_db($myDB, $conn)

文档链接http://www.w3schools.com/php/php_ref_mysql.asp

于 2013-03-14T05:43:43.743 回答
0

更正。

  1. attribute表单中的关闭方法method="GET"
  2. 插入语句如下

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

  3. mssql_connect应该是mysql_connect而且mssql_select_db应该是mysql_select_db

  4. $result= mysql_query($sql,$conn) or die (mysql_error());

不同之处。

$_GET['username']这将查找在 url 中作为参数传递的用户名值。

$_GET[username]这将查找可能未在代码中定义的用户名常量。

于 2013-03-14T05:39:23.573 回答