2013-06-25 42 views
0

我已经调整和打破了今天已经100万次! 我基本上试图检查数据库的用户名和激活, 如果用户被激活显示祝贺消息 如果用户是一个用户,但没有激活显示激活消息 如果他们不是记录匹配的用户然后显示无效信息。 任何帮助重新编码和纠正我的错误将非常感激,我觉得我只是绕圈子转!PHP简单的激活形式MYSQL

串(74), “更新成员SET Check_Activation = '' WHERE用户名= '' 和活化= ''”

<form name="form1" method="post" action="check-activation.php"> 
    <div align="center"> 
    <table width="35%" border="0"> 
     <tr> 
     <td>Members Number</td> 
     <td>:</td> 
     <td><label> 
      <input name="username" type="text" id="username" value="<?php echo $username; ?>"> 
     </label></td> 
     </tr> 
     <tr> 
     <td>Activation Code</td> 
     <td><label>:</label></td> 
     <td><input name="activation_code" type="text" id="activation_code" value="<?php echo $activation_code; ?>"></td> 
     </tr> 
     <tr> 
     <td>&nbsp;</td> 
     <td>&nbsp;</td> 
     <td><label> 
      <input type="submit" name="Submit" value="Submit"> 
      <input type="reset" name="Submit2" value="Cancel"> 
     </label></td> 
     </tr> 
    </table> 
    </div> 
    </form> 

这是形式

<?php 
$db_host = "*******"; 
$db_name = "*******"; 
$db_use = "*******"; 
$db_pass = "*******"; 
$link = mysql_connect($db_host, $db_use, $db_pass); 
mysql_select_db($db_name, $link); 
$command = "UPDATE members SET Check_Activation='$activation_code' WHERE Username='$username' AND Activation='$activation_code'"; 
$result = mysql_query($command); 
if ($result) { 
echo "Congratulations. Your membership has been activated ..."; 
}else{ 
echo ("You've entered an invalid username/activation code - please retry"); 
} 
?> 

这是检查数据库并显示结果

enter image description here enter image description here

+0

您将在编写代码时遇到问题 - 执行没有受到影响的行的UPDATE是有效的SQL语句。您需要统计有多少行受到影响。 – andrewsi

+0

尝试在mysql函数之后添加一个模子,以检查数据库连接是否正确mysql_func()或死亡(mysql_error());' – quidage

+0

[**请勿在新代码中使用'mysql_ *'函数* *](http://bit.ly/phpmsql)。他们不再被维护[并被正式弃用](https://wiki.php.net/rfc/mysql_deprecation)。看到[**红框**](http://j.mp/Te9zIL)?学习[*准备的语句*](http://j.mp/T9hLWi),并使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [这篇文章](http://j.mp/QEx8IB)将帮助你决定哪个。如果你选择PDO,[这里是一个很好的教程](http://www.brightmeup.info/article.php?a_id=2)。 – insertusernamehere

回答

2

没有更多的支持mysql_*功能,它们是officially deprecated不再保持,将在未来removed。您应该使用PDOMySQLi来更新您的代码,以确保您的项目未来的功能。


正如你可以在每一个步骤,我们有一个验证知道,如果我们能够连接到数据库见下文,如果SQL查询是OK,如果你想给的参数都OK,我们也抓住机会,防止注入准备好的声明,并最终执行它。

现在注意我们执行它之后,我使用$update->affected_rows来验证有多少行受到影响,以确定它是否正常。

受影响的行,可以返回:

  • -1,表明该查询返回错误
  • 0表示没有记录被更新
  • 及以上0的数字,表示多少行受影响

代码:

$db_host = "*******"; 
$db_name = "*******"; 
$db_use = "*******"; 
$db_pass = "*******"; 

// Read the form values you just submitted 
$username = $_POST['username']; 
$activation_code = $_POST['activation_code']; 

$con = mysqli_connect($db_host,$db_use,$db_pass,$db_name); 
// Output an error message if it fails to connect 
if($con->connect_error) 
     die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error()); 

// Here we prepare your query and make sure it is alright 
$sql = "UPDATE members SET Check_Activation = ? WHERE Username = ? AND Activation = ?"; 
if (!$update = $con->prepare($sql)) 
    die('Query failed: (' . $con->errno . ') ' . $con->error); 

// Here we define the field types with 'sss' which means string, string, string 
// and also set the fields values 
if (!$update->bind_param('sss', $activation_code, $username, $activation_code)) 
    die('Binding parameters failed: (' . $update->errno . ') ' . $update->error); 

// Now we finally execute the data to update it to the database 
// and if it fails we will know 
if (!$update->execute()) 
    die('Execute failed: (' . $update->errno . ') ' . $update->error); 

if ($update->affected_rows > 0) 
{ 
    echo "Congratulations. Your membership has been activated ..."; 
} 
else 
{ 
    echo ("You've entered an invalid username/activation code - please retry"); 
} 
// And finally we close the connection 
$update->close(); 
$con->close(); 

注意查询中的询问符号,bind_param, s表示字符串,i表示整数,you can read more here

+0

我已经尝试了以上,我得到一个空白的屏幕。 ? – Delete

+0

<?php和DB的详细信息在哪里检查过,它们没有显示任何错误。 – Delete

+0

@AdamWadsworth这是我的错误,将'$ update-> affected_rows()'更改为'$ update-> affected_rows' :),只是从中删除括号。我还更新了上述代码以反映这一变化。 – Prix

1

为了得到答案,如果任何行插入/更新,你应该使用mysql_affected_rows()

if (mysql_affected_rows()) { 
echo "Congratulations. Your membership has been activated ..."; 
}else{ 
echo ("You've entered an invalid username/activation code - please retry"); 
} 

这种代码也可以简化为

echo mysql_affected_rows() ? "Congratulations. Your membership has been activated ..." : "You've entered an invalid username/activation code - please retry"; 

该函数返回成功,受影响的行数如果最后一次查询失败,则返回-1。

但是,当你到手册有一个大的红色框与消息,mysql_ *功能dephased这就是为什么你应该考虑使用PDO或Mysqli来代替。