2016-10-11 52 views
2

下面的代码,如果我取消注释行工作得很好,我真的不知道,为什么它没有显示任何东西,当我包括注释行不显示结果。感谢您的时间和协助。当我包括我的注释行

我需要一定的一段代码来检查每一个电话号码进行搜索,这样它会自动更新表“备注”到过期让我知道登记已过期时间。

<?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

死亡白屏:错误检查\显示已关闭,请打开它们以查看错误。在你的php页面的顶部添加:'ini_set('display_errors','On'); ini_set('html_errors',0); error_reporting(-1);' – nogad

+0

你好,它在我的完美运行,它只是当我取消我已评论的代码块,它不会显示任何输出了。 评论:http://prntscr.com/cscsov Uncommented:http://prntscr.com/cscsww 注:我已经添加了您提供的代码,没有错误显示。只要我评论代码块,它就可以按预期工作。 –

+0

好吧,这不是没有输出,这显然是一些输出 – nogad

回答

0

的注释部分之前添加此行:

$verification_result_clone = clone($verification_result); 

并更换

$row = mysqli_fetch_array($verification_result, MYSQLI_ASSOC); 

$row = mysqli_fetch_array($verification_result_clone, MYSQLI_ASSOC); 

UPDATE:

我不知道mysql_result类是不可复制的。对不起。

好吧,我想你应该运行查询两次。试试这个:

从我的回答上述更换

$verification_result_clone = clone($verification_result); 

$verification_result_clone = mysqli_query($conn,$verification); 

。这应该现在工作。

+0

你好@someone,我试着你的建议,我得到了以下错误。 '致命错误:未捕获错误:试图在C:\ xampp \ htdocs \ testing \ sql.php中克隆类mysqli_result的uncloneable对象:#49堆栈跟踪:#0 C:\ xampp \ htdocs \ testing \ sql.php (18):sql_entry('7083281100')#1 {main}扔在第49行的C:\ xampp \ htdocs \ testing \ sql.php中# –

+0

@CyanHijirikawa我已经更新了我的答案。 – someone

1

您需要倒带结果集,所以它可以再次重复或建立数据结构显示和更新一次迭代(其实你可以不用从数据库中提取数据更新 - 查询使用条件)。后退将需要更少的代码更改 - 只需在评论部分的末尾添加:

mysqli_data_seek($verification_result, 0); 

Btw。您的UPDATE仅适用于第一个返回的行,稍后您将尝试迭代,因为可能会有更多结果。如果是这种情况(没有倒带),则首先更新并显示其余部分。

+0

这比我的答案要好。 +1 – someone