PHP + MySQLi =查询num_rows并插入表中

时间:2016-07-19 09:05:34

标签: php email mysqli

我正在制作一个电子邮件跟踪脚本,它将链接到我的record.php文件的空白GIF插入到电子邮件正文中,并且应该在收件人打开邮件时运行脚本。

电子邮件部分工作正常,但我无法进行验证检查,也无法使数据插入正常工作。有人介意看看吗?

record.php

<?php
error_reporting(E_ALL);

//Begin the header output
header('Content-Type: image/gif');

if(isset($_GET['type']) && $_GET['type'] == 'Poststay') {

    // Start MySQLi connection
    include '../../../plugins/MySQL/connect_db.php';
    $mysqli = new mysqli($dbhost,$dbuser,$dbpass,$dbname);

    if($mysqli->connect_errno > 0){
    die('Unable to connect to database [' . $mysqli->connect_error . ']'); }

    // define and sanitize GET variables    
    $type = $_GET['type'];
    $holidex = $_GET['holidex'];
    $user = $_GET['user'];
    $sent_to = $_GET['sent_to'];
    $crs = $_GET['crs'];

    // check if submitted record already exists
    $sql = "SELECT Holidex FROM qci_email_log WHERE Holidex = '".$holidex."' AND CRS = '".$crs."'";
    if(!$result = $mysqli->query($sql)) {
    die('Unable to check for existing records in DB [' . $mysqli->error . ']'); }

    if( $result == 0 ) {
        $sql = "INSERT INTO `qci_email_log`(`Type`,`Holidex`,`User`,`Sent_To`,`CRS`) VALUES ('".$type."','".$holidex."','".$user."','".$sent_to."','".$crs."')";
        if(!$result = $mysqli->query($sql)) {
        die('Unable to insert email logging into database [' . $mysqli->error . ']'); }
    }

    // free result and close connection
    $result->free();
    $mysqli->close();
}

// disregarding if the database was affected, output an empty image

// URI to the image
$graphic_http = 'http://www.qcisource.com/ihg/dist/img/blank.gif';

// Get image filesize for header
$filesize = filesize( 'blank.gif' );

// Image output
header( 'Pragma: public' );
header( 'Expires: 0' );
header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
header( 'Cache-Control: private',false );
header( 'Content-Disposition: attachment; filename="blank.gif"' );
header( 'Content-Transfer-Encoding: binary' );
header( 'Content-Length: '.$filesize );
readfile( $graphic_http );

?>

1 个答案:

答案 0 :(得分:1)

SELECT第一次查询后,请替换:

if( $result == 0 )

使用

if($result->num_rows===0)

附加说明:

只有当图像与脚本位于同一目录中时,

filesize('blank.gif')才有效。如果没有,请使用图像的完整路径。您可以使用以下命令测试图像文件是否正确定位:

if(!file_exists('blank.gif')) die('Unable to locate blank.gif');