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 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程