duanhong4274 2016-07-19 09:05
浏览 26
已采纳

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

I am working on an email tracking script which inserts a blank GIF linked to my record.php file into the email body, and is supposed to run a script whenever the recipients opens the mail.

The email part is working fine, but I cannot manage to get the verification check nor the data insert working. Anyone mind having a look?

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条回答 默认 最新

  • dprlv04662 2016-07-19 09:13
    关注

    After you SELECT with your first query, replace:

    if( $result == 0 )
    

    With

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

    Additional Notes:

    filesize('blank.gif') will only work if the image is in the same directory as the script. If not, use a full path to the image instead. You can test whether the image file was correctly located with:

    if(!file_exists('blank.gif')) die('Unable to locate blank.gif');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line