更改mysqli检查退出并插入到pdo语句中

时间:2017-04-06 07:10:56

标签: mysqli pdo

我想改变我的mysqli检查存在并插入到pdo ..我的声明如下所示:

 <?php

$addSearch = preg_replace('/[^a-zA-Z0-9_ %\[\]\.\(\)%&-]/s', '', $search);


if ($CheckMusic = $mysqli->query("SELECT * FROM search WHERE term='$addSearch'")) {

    $CheckRow = mysqli_fetch_array($CheckMusic);

    $CheckId = $CheckRow['id'];

    $CheckCnt = $CheckMusic->num_rows;

    $CheckMusic->close();

} else {

    printf("Error: %s\n", $mysqli->error);
}

$Now = strtotime("now");


$addSearch = $mysqli->escape_string($addSearch);


if ($CheckCnt < 1) {


    $mysqli->query("INSERT INTO search (term, datetime) VALUES('$addSearch','$Now') ") or die(mysqli_error());

} else {

    $mysqli->query("UPDATE search SET datetime='$Now' WHERE id='$CheckId'") or die(mysqli_error());
}


?>

希望有人可以提供样品帮助; q谢谢你的预付款!

1 个答案:

答案 0 :(得分:0)

<?php
    $addSearch = preg_replace('/[^a-zA-Z0-9_ %\[\]\.\(\)%&-]/s', '', $search);
    $CheckMusic = $mysqli->query("SELECT * FROM search WHERE term='$addSearch'");
    $Now = strtotime("now");
    if($CheckMusic->num_rows == 0){
        $mysqli->query("INSERT INTO search (term, datetime) VALUES('$addSearch','$Now') ") or die(mysqli_error());
    }else{
        $CheckRow = mysqli_fetch_array($CheckMusic);
        $CheckId = $CheckRow['id'];
        $addSearch = $mysqli->escape_string($addSearch);
        $mysqli->query("UPDATE search SET datetime='$Now' WHERE id='$CheckId'") or die(mysqli_error());
    }

我希望它能奏效。