繁体   English   中英

从抓取URL参数插入数据-PHP PDO

[英]Inserting Data from Grabbing URL Parameter - PHP PDO

例如,我的URL末尾带有特殊的参数:

http://example.com/index.php?id_user=84759832475

[id_user=**84759832475**]由我自己创建,并且已在脚本中声明了它。

$txtemail = strip_tags(isset($_POST['txtemail'])) ? strip_tags($_POST['txtemail']) : '';
$txtemail=strip_tags($txtemail);
$txtname = strip_tags(isset($_POST['txtname'])) ? strip_tags($_POST['txtname']) : '';
$txtname =strip_tags($txtname);
$id_user="84759832475";

$stmt="SELECT * FROM table_name WHERE emailz=:txtemail AND namez=:txtnamez"; 
$pgdata = $myDb->prepare ($stmt);
//bind semua variabel login dalam parameter
$pgdata->bindParam(':txtname', $txtname, PDO::PARAM_STR,31);
$pgdata->bindParam(':txtemail', $txtemail, PDO::PARAM_STR,31);
//eksekusi statemen prepare tadi
$pgdata->execute();
//cek & lihat hasil
//$cekdata = $pgdata->fetchColumn();
if(!$pgdata->rowCount()> 0){
    $pgdata = $myDb->prepare('INSERT INTO table_name (namez,emailz,userid) VALUES (:txtname,:txtemail,?????)');
    $pgdata->execute(array(':namez'=>$txtname, ':emailz'=>$txtemail, ':userid'=>$id_user));

在这种情况下,问号?????? 让我对写什么感到困惑。 如果我的英语太糟糕了,无法解释这个问题,我感到抱歉。

只需在其他准备好的语句内添加另一个命名的占位符,就像其他语句一样:

$txtemail = isset($_POST['txtemail']) ? strip_tags($_POST['txtemail']) : '';
$txtname = isset($_POST['txtname']) ? strip_tags($_POST['txtname']) : '';
$id_user = "84759832475";

$stmt = 'SELECT COUNT(id) AS total FROM table_name WHERE emailz = :txtemail AND namez = :txtnamez'; 
$pgdata = $myDb->prepare($stmt);

$pgdata->bindParam(':txtnamez', $txtname, PDO::PARAM_STR);
$pgdata->bindParam(':txtemail', $txtemail, PDO::PARAM_STR);
$pgdata->execute();

$result = $pgdata->fetch(PDO::FETCH_ASSOC);

if($result['total'] > 0){

    $pgdata = $myDb->prepare('
      INSERT INTO table_name (namez,emailz,userid) 
      VALUES (:txtname, :txtemail, :userid)
    ');
    // just add another named placeholer :userid

    $pgdata->execute(array(':txtname'=> $txtname, ':txtemail'=> $txtemail, ':userid' => $id_user));
}

暂无
暂无

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

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