为什么testTime一会跳两个数字,一会儿又减少一个数字
其他问答
1
<?php
$servername = "--";
$username = "---";
$password = "--";
$dbname = "--";
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
} else {
echo '连接成功!<br>';
}
echo "userSex:".$_COOKIE['userSex'].'<br>';//从登陆页面获取
echo 'testTime:'.$_COOKIE['testTime'];//从登陆页面获取
/*
if( $_COOKIE['userSex']=='male'){
echo $_COOKIE['userSex'];
}
echo '<br>';
echo 'userSex'. $_COOKIE['userSex'];
if($_COOKIE['userSex']=='female'){
echo $_COOKIE['userSex'];
}
*/
$userName = $_COOKIE['userName'];
$userPassword = $_COOKIE['userPassword'];
$sql = "SELECT * FROM user_table WHERE userName='{$userName}' and userPassword='{$userPassword}'";//php的双引号里面可以放变量
$result = $conn->query($sql);
$row=$result->fetch_assoc();
if ($result->num_rows > 0) {
setcookie('cautionWord', '健康开心每一天');
setcookie('userRight', 'normalRight');
setcookie('userSex', $row['userSex']);
setcookie('testTime', $row['testTime'] + 1);
//make sure the time of test
$testTime = $_COOKIE['testTime'];
$sql = "UPDATE user_table SET testTime='{$testTime}' WHERE userName='{$userName}'";//insert the testTime
$result = $conn->query($sql);
//echo 'testTime:'.$tesTime;
$userName = $_COOKIE['userName'];
echo 'userName:' . $userName;
$sql = "UPDATE user_table SET testTime='{$testTime}' WHERE userName='{$userName}'";//insert the testTime
if ($result = $conn->query($sql)) {
echo 'insert successfully';
} else {
echo 'please insert again.';
}
}else{
echo 'there is no such customer!';
}
?>
-
登录页面怎么设置testTime这个cookie的?代码发来看下。
一般记录登录次数直接sql语句+1就行了,不需要从cookie获取值来更新。要不客户端修改了cookie值代码逻辑全乱了。而且更新代码怎么执行了2 次。。
if ($result->num_rows > 0) { setcookie('cautionWord', '健康开心每一天'); setcookie('userRight', 'normalRight'); setcookie('userSex', $row['userSex']); setcookie('testTime', $row['testTime'] + 1); //make sure the time of test $testTime = $_COOKIE['testTime']; ////////////直接数据库字段+1 就行了,而且额不需要执行2次 $sql = "UPDATE user_table SET testTime=testTime+1 WHERE userName='{$userName}'";//insert the testTime //$result = $conn->query($sql); //echo 'testTime:'.$tesTime; // $userName = $_COOKIE['userName']; // echo 'userName:' . $userName; //$sql = "UPDATE user_table SET testTime='{$testTime}' WHERE userName='{$userName}'";//insert the testTime if ($result = $conn->query($sql)) { echo 'insert successfully'; } else { echo 'please insert again.'; } }else{ echo 'there is no such customer!'; }
-
testTime
字段是在数据库表中的,这个字段的作用是干嘛的。 有没有其他地方修改testTime
这个字段的值。
发表回复