简体   繁体   English

PHP不显示MYSQL时间戳搜索的结果

[英]PHP not Displaying Results from MYSQL Timestamp Search

Below is my code for searching a date range from MYSQL database. 下面是我的代码,用于从MYSQL数据库搜索日期范围。 I am able to search the dates and it brings up the table headings (Product Desc, New or Own and Date) but it doesn't display any of the results from the database? 我能够搜索日期,并且它会显示表标题(产品描述,新建或拥有和日期),但是它不显示数据库中的任何结果?

Any Ideas on how I need to edit my code in order for it to display the results from the selected date range? 关于如何编辑代码以使其显示所选日期范围内的结果的任何想法?

<?php
if(!isset($_POST['find']))
{
?>
<form method = "post" action = "<?php echo $_SERVER['PHP_SELF'];?>">
<table width = "450" align = "center">
    <tr>
    <td><b><i>Please enter date in the field below (i.e. 11-09-2012)</i></b></td>
</tr>
<tr>
    <td>
    From&nbsp;:&nbsp;
    <input type = "text" name = "small">
    To&nbsp;:&nbsp;
    <input type = "text" name = "large"></td>
</tr>
<tr>
    <td align = "center">
        <input type = "submit" name = "find" value = "SEARCH">
        <input type = "reset" value = "CLEAR FORM">
        </td>   
    </tr>
</table>
</form>
<?php
}
else
{
$small = trim($_POST['small']);
$large = trim($_POST['large']);

$connection = mysql_pconnect("localhost", "user_name", "password") or die("Connection failed. ".myslq_error());
 mysql_select_db("stock") or die("Unable to select db. ".mysql_error());
//Add 1 to the upper range, $large, else it won't make it inclusive
$query = "SELECT * FROM main_stock WHERE curr_timestamp BETWEEN DATE_FORMAT(curr_timestamp,'%".$small."') AND DATE_FORMAT(curr_timestamp, '%".($large+1)."') ORDER BY curr_timestamp";
$result = mysql_query($query) or die(mysql_error());

echo "<table width = '500' align = 'center'>";
echo "<tr><b>";
    echo "<td>Product Description</td>";
    echo "<td>New or Own?</td>";
    echo "<td>Date</td>";
echo "</b></tr>";
while($record = mysql_fetch_object($result))
{
    echo "<tr>";
        echo "<td>".$record->product_desc."</td>";
        echo "<td>".$record->newown."</td>";
        $year_part_of_date = explode('-', $record->curr_timestamp);
    echo "<td>".$year_part_of_date[0]."</td>";
        //if you want the full date replace the $year_part_of_date[0] with $record->date
    echo "</tr>";
}
echo "</table>";

}

?>

I would guess your query is not valid. 我猜您的查询无效。

Assuming that curr_timestamp is a MySQL datetime field in format of YYYY-MM-DD HH:MM:SS , you need to change your input to be in this format for easy comparison. 假设curr_timestampYYYY-MM-DD HH:MM:SS格式的MySQL日期时间字段,您需要将输入更改为这种格式以便于比较。 If you can elaborate on the format of $small and $large that would be helpful. 如果您可以详细说明$small$large的格式,那将很有帮助。

You probably want your query to be in this format 您可能希望查询采用这种格式

SELECT * FROM main_stock
WHERE curr_timestamp BETWEEN '$small_formatted' AND '$large_formatted'
ORDER BY curr_timestamp ASC

Where $small_formatted and $large_formatted are properly formatted times using something like date('Ymd H:i:s', $input_timestamp) 其中$small_formatted$large_formatted是使用date('Ymd H:i:s', $input_timestamp)类的格式正确格式化的时间

  1. Switch the order of your logic. 切换逻辑顺序。 You do not want your process to be an else . 您不希望您的过程成为else
  2. It seems like you are combining procedural and object styles. 似乎您正在组合过程和对象样式。 Pick one. 选一个。
  3. You should avoid selecting all ( SELECT * ). 您应该避免选择全部( SELECT * )。 Always specify a column list. 始终指定列列表。
  4. The dates are not formatted correctly. 日期格式不正确。 You should format your input date to match the database format ( YYYY-MM-DD ). 您应该格式化输入日期以匹配数据库格式( YYYY-MM-DD )。 I am assuming that curr_timestamp is YYYY-MM-DD . 我假设curr_timestampYYYY-MM-DD
  5. You need to stop using mysql_ functions as they are being deprecated . 您将不再使用mysql_函数,因为它们已被弃用
  6. Use prepared statements to prevent SQL injections. 使用准备好的语句来防止SQL注入。

I have not tested this, but this fixes the issues I've listed above: 我尚未对此进行测试,但这可以解决上面列出的问题:

<?php
if(isset($_POST['find']))
{
    $link = mysqli_connect("localhost", "user_name", "password", "stock");

    if (mysqli_connect_error()) {
    die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
    }

    $stmt = mysqli_prepare($link, 'SELECT * FROM main_stock WHERE curr_timestamp ' . 
    "BETWEEN DATE_FORMAT(?, '%m-%d-%Y') " . 
    "AND DATE_FORMAT(?, '%m-%d-%Y') ORDER BY curr_timestamp");

    mysqli_bind_param($stmt, 'ss', $_POST['small'], $_POST['large']) or die(mysqli_error($dbh));

    $result = mysqli_stmt_execute($stmt) or die(mysqli_error($link));

    echo "<table width = '500' align = 'center'>";
    echo "<tr><b>";
    echo "<td>Product Description</td>";
    echo "<td>New or Own?</td>";
    echo "<td>Date</td>";
    echo "</b></tr>";

    while($record = mysqli_fetch_assoc($result)) {
    echo "<tr>";
    echo "<td>" . $record[product_desc] . "</td>";
    echo "<td>" . $record[newown] . "</td>";

        $year_part_of_date = explode('-', $record[curr_timestamp]);

    echo "<td>".$year_part_of_date[0]."</td>";

        //if you want the full date replace the $year_part_of_date[0] with $record->date
    echo "</tr>";
    }

    echo "</table>";

   mysqli_close($link);
}
else
{
?>
<form method = "post" action = "<?php echo $_SERVER['PHP_SELF'];?>">
<table width = "450" align = "center">
    <tr>
    <td><b><i>Please enter date in the field below (i.e. 11-09-2012)</i></b></td>
</tr>
<tr>
    <td>
    From&nbsp;:&nbsp;
    <input type = "text" name = "small">
    To&nbsp;:&nbsp;
    <input type = "text" name = "large"></td>
</tr>
<tr>
    <td align = "center">
        <input type = "submit" name = "find" value = "SEARCH">
        <input type = "reset" value = "CLEAR FORM">
        </td>   
    </tr>
</table>
</form>
<?php
}

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM