Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP cookie未在ajax处理脚本中创建_Php_Mysql_Ajax_Cookies - Fatal编程技术网

PHP cookie未在ajax处理脚本中创建

PHP cookie未在ajax处理脚本中创建,php,mysql,ajax,cookies,Php,Mysql,Ajax,Cookies,如果没有创建cookie,我将尝试执行mysql更新查询 我就是这样尝试的: // Sanitize and validate the data passed in // Check for the ID, comes from ajax: $count = (int)$_POST['count']; // Get existing views: $prep_stmt = "SELECT views FROM phone_number_

如果没有创建cookie,我将尝试执行mysql更新查询

我就是这样尝试的:

// Sanitize and validate the data passed in     
    // Check for the ID, comes from ajax:
    $count = (int)$_POST['count'];      

    // Get existing views: 
    $prep_stmt = "SELECT views FROM  phone_number_views";                                
    $stmt = $mysqli->prepare($prep_stmt);
    if ($stmt) {
        $stmt->execute();    
        $stmt->store_result();      
        // get variables from result.
        $stmt->bind_result($existing_views);
        // Fetch all the records:
        $stmt->fetch(); 
    }
    // Close the statement:
    $stmt->close();
    unset($stmt);       

$n='';
$n=$existing_views+$count;

echo $n;
//if no such cookie exists, assume that its their first time viewing.
if(!isset($_COOKIE['number_viewed'])){

    $q = "INSERT INTO number_views (id, views) VALUES (1, 1)
                    ON DUPLICATE KEY UPDATE views = $n"; 

    // Prepare the statement:
    $stmt = $mysqli->prepare($q);   
    // Bind the variables:
    //$stmt->bind_param('i', $n);
    // Execute the query:
    $stmt->execute();

    // Print a message based upon the result:
    if ($stmt->affected_rows == 1) {
        //set cookie saying they've viewed this number.
        setcookie( 'number_viewed', $n, time()+600, '/');       

        // Print a message and wrap up:
        $messages = array('success'=>true, 'totalViews'=>$n);
    } 

    // Close the statement:
    $stmt->close();
    unset($stmt);   
} 

echo json_encode($messages);

// Close the connection:
$mysqli->close();   
我的问题是这个cookie没有创建。更新查询始终有效

注意:这是一个ajax处理脚本

谁能告诉我这有什么问题吗

希望有人能帮助我


多谢各位

问题是您正在为(id,视图)编写带有值(1,1)的插入查询,但是
id
是您的主键。因此,值1不能指定两次。因此,查询的后面部分每次都在运行。尝试按查询仅插入(视图)列。然后,您将发现您的cookie是否已设置

使用
错误报告(E|ALL | E|u STRICT)
查找错误消息。@dededee,我无法获取任何错误。表中的
id
列是主列key@AnkitAgarwal,是ID是我的主键什么是
$n
?它有定义吗?为什么不检查mysql错误?Ankit Agarwal,是的,现在cookie正在创建。但查询将新记录添加到数据库。我的桌子应该只有一条记录。有没有办法将
where cause
与重复键上的
一起使用?谢谢。如果您希望表中只有一条记录,那么为什么不在mysql查询中使用Update语句呢<代码>更新
查询将非常适合您的场景。