如果我取消注释注释行,下面的代码工作正常,我真的不确定为什么当我包含注释行时它没有显示任何内容。感谢您的时间和帮助。
我需要在每次搜索电话号码时检查某段代码,以便它会自动将“Notes”表更新为Expired,以便让我知道注册已经过期。
<?php
include_once('assets/inc/db_login.inc');
session_start();
if($_SERVER["REQUEST_METHOD"] == "POST"){
if(empty($_POST["phone"])) {
$unameErr = "Phone is required";
}
else {
$phone = clean_input($_POST["phone"]);
}
}
$check = sql_entry($phone);
/* Functions */
function clean_input($login){
$login = trim($login);
$login = stripslashes($login);
$login = htmlspecialchars($login);
return $login;
}
function sql_entry($phone){
//do not touch anything beyond this part
$conn = mysqli_connect(DB_HOST,DB_USERNAME,DB_PASSWORD,DB_NAME);
//error catcher for connection failure
if($conn->connect_error){
die("Connection failed: " . $conn->connect_error);
}
//clean themmmmmm!
$clean_phone = mysqli_real_escape_string($conn, $phone);
//prepare queries
$verification = "SELECT * FROM ".DB_TBL." WHERE phone = ".$clean_phone;
$verification_result = mysqli_query($conn,$verification); //run query to validate phone number
/*
$row = mysqli_fetch_array($verification_result, MYSQLI_ASSOC);
$startdate = $row['register_date'];
$expire = strtotime($startdate. ' + 182 days');
$today = strtotime("today midnight");
if($today >= $expire){
$update = "UPDATE ".DB_TBL." SET notes='Expired' WHERE phone = ".$clean_phone;
$run_update = mysqli_query($conn,$update);
}
*/
return $verification_result;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> - | User Registration</title>
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
<center><br>
<br>
<br>
<br>
<br>
<br>
<table width="500", cellpadding=5 callspacing=5 border=1>
<tr>
<th>Name</th>
<th>Register Date</th>
<th>Phone</th>
<th>Points</th>
<th>Note</th>
</tr>
<?php while($rows = mysqli_fetch_array($check, MYSQLI_ASSOC)): ?>
<tr>
<td><?php echo $rows['username']; ?></td>
<td><?php echo $rows['register_date']; ?></td>
<td><?php echo $rows['phone']; ?></td>
<td><?php echo $rows['points']; ?></td>
<td><?php echo $rows['notes']; ?></td>
</tr>
<?php endwhile; ?>
</table>
</center>
</body>
</html>
答案 0 :(得分:1)
您需要回滚结果集以便可以再次迭代或者为一次迭代构建数据结构以进行显示和更新(实际上您可以在不从数据库中提取数据的情况下进行更新 - 在查询中使用条件)。倒带需要更少的代码更改 - 只需在注释部分的末尾添加:
mysqli_data_seek($verification_result, 0);
顺便说一下。您的UPDATE
只能使用第一个返回的行,稍后您会尝试迭代,因为可能会有更多结果。如果是这种情况那么(没有倒带)你会先更新并显示其余部分。
答案 1 :(得分:0)
在评论部分之前添加此行:
$verification_result_clone = clone($verification_result);
并替换
$row = mysqli_fetch_array($verification_result, MYSQLI_ASSOC);
与
$row = mysqli_fetch_array($verification_result_clone, MYSQLI_ASSOC);
<强>更新强>
我不知道mysql_result
类不可克隆。对不起。
好吧,我认为你应该运行两次查询。试试这个:
替换
$verification_result_clone = clone($verification_result);
与
$verification_result_clone = mysqli_query($conn,$verification);
从我上面的答案。那现在应该可以了。