简体   繁体   English

SQL到PDO,用于将表内容传输到另一个表

[英]SQL to PDO For Transferring Table Contents To Another Table

I am trying to change a email confirmation function written in sql to PDO. 我正在尝试将用sql编写的电子邮件确认功能更改为PDO。 This is the tutorial for your further reference: http://www.phpeasystep.com/phptu/24.html . 这是本教程供您进一步参考: http : //www.phpeasystep.com/phptu/24.html

This particular section will move the user's information from the temporary table to the permanent table after they click on the verification link sent to their email. 在用户单击发送到其电子邮件的验证链接后,此特定部分会将用户的信息从临时表移至永久表。

The problem I am having is under this header within that tutorial: STEP4: confirmation.php 我遇到的问题在该教程的该标题下: STEP4:confirmation.php

My code is similar to that, but I added a few prepared statements as I am trying to prevent SQL injection. 我的代码与此类似,但是我在尝试防止SQL注入时添加了一些准备好的语句。

To be clear: I am looking for direction on what else I need to do to switch this from sql to PDO and prevent any sql injection. 需要明确的是:我正在寻找有关将其从sql切换到PDO并防止任何sql注入的其他方法的指导。 Examples when providing an answer are crucial as I am a visual learner. 提供答案的示例至关重要,因为我是视觉学习者。 Thank you. 谢谢。

Here is my code: 这是我的代码:

<?php

include('config.php');

//Test DB connection
try {
    $conn = new PDO("mysql:host=$Host;db=$svrDb", Username, $Password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} 
catch(PDOException $e) {
    echo 'ERROR: ', $e->getMessage(), "\n";
}

// Passkey that got from link
$passkey=$_GET['passkey'];

$sql1= $conn->prepare("SELECT * FROM temp_members_db WHERE confirm_code ='$passkey'");
$sql_conf1->execute() or die ('Error updating database: '.mysql_error());
$result1=$sql1->fetchall();

// If successfully queried
if($result1){

// Count how many row has this passkey
$count=mysql_num_rows($result1);

// if found this passkey in our database, retrieve data from table "temp_members_db"
if($count==1){

$name=$rows['name'];
$email=$rows['email'];
$password=$rows['password'];
$country=$rows['country'];

// Insert data that retrieves from "temp_members_db" into table "registered_members"
$sql2="INSERT INTO registered_members(name, email, password, country)VALUES('$name', '$email', '$password', '$country')";
$result2=mysql_query($sql2);
}

// if not found passkey, display message "Wrong Confirmation code"
else {
echo "Wrong Confirmation code";
}

// if successfully moved data from table"temp_members_db" to table "registered_members" displays message "Your account has been activated" and don't forget to delete confirmation code from table "temp_members_db"
if($result2){

echo "Your account has been activated";

// Delete information of this user from table "temp_members_db" that has this passkey
$sql3="DELETE FROM temp_members_db WHERE confirm_code = '$passkey'";
$result3=mysql_query($sql3);

}

}
?>

Ok, so you're on the right track but not quite.... 好的,所以您走在正确的轨道上,但不太正确。

The whole reason you use Prepared Statements is so that the database knows what to interpret as SQL, and what not to interpret as SQL. 使用Prepared Statements的全部原因是为了使数据库知道要解释为SQL的内容,而不是要解释为SQL的内容。 Right now, you have the following: 现在,您具有以下功能:

$sql1= $conn->prepare("SELECT * FROM temp_members_db WHERE confirm_code ='$passkey'");
$sql_conf1->execute() or die ('Error updating database: '.mysql_error());
$result1=$sql1->fetchall();

To take full advantage of PDO and Prepared Statements you should change it to something like this: 要充分利用PDO和预准备语句,应将其更改为以下内容:

$sql1= $conn->prepare("SELECT * FROM temp_members_db WHERE confirm_code = ?");
try{
     $sql1->execute(array($passkey));
}catch(PDOException $e){
     print "Error!: " . $e->getMessage() . "<br/>";
     die();
}
$result1=$sql1->fetchall();

There are a couple reasons for this. 有两个原因。 In your code, you send the variable $passkey along with your prepare statement, so in essence the prepare operation is useless because $passkey has not been sanitized. 在您的代码中,您将变量$passkey与您的prepare语句一起发送,因此从本质上讲,由于未对$passkey进行清理,因此prepare操作是无用的。 Notice in my code I replaced it with a ? 请注意,在我的代码中,我将其替换为? . This tells the database: 这告诉数据库:

"Hey, I'm going to be executing a SELECT statement and I'm going to replace the ? with a value from one of my variables, so don't interpret that as SQL!" “嘿,我将执行SELECT语句,并用一个变量中的值替换? ,所以不要将其解释为SQL!”

Here is a great resource for PDO: 这是PDO的重要资源:

http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM