简体   繁体   English

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

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

I have, for example, this URL with spesific paramater in the end: 例如,我的URL末尾带有特殊的参数:

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

The value [id_user=**84759832475**] is created by myself and I have declared it inside my script. [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));

In this case, the question mark ?????? 在这种情况下,问号?????? makes me confused of what to write. 让我对写什么感到困惑。 I'm sorry if my English is too bad to explain this question. 如果我的英语太糟糕了,无法解释这个问题,我感到抱歉。

Just add another named placeholder inside that other prepared statement, just like the others: 只需在其他准备好的语句内添加另一个命名的占位符,就像其他语句一样:

$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