简体   繁体   中英

Inserting Data from Grabbing URL Parameter - PHP PDO

I have, for example, this URL with spesific paramater in the end:

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.

$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));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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