<?php
echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";  

echo "<td width='100' align='center'>date in</td><td width='100' align='center'>date out</td>
<td width='100' align='center'>model</td>
";  

echo "</tr>";  

include("proses/dbcon.php");

$date_in = $_POST['date_in'];
$date_out = $_POST['date_out'];

if(isset($submit))
{
$sql = "select * from inventory where createdate >= '$date_in' and createdate <= '$date_out' ";
}
$result= mysql_query($sql);

        while($row=mysql_fetch_array($result))  
        {

echo "<tr>";  
echo "<td align='center' width='200'>" . $row['date_in'] . "</td>";   
echo "<td align='center' width='200'>" . $row['date_out'] . "</td>";  
echo "<td align='center' width='200'>" . $row['model'] . "</td>"; 
echo "</tr>";  
}  
echo "</table>";  

?> 

Recommended Answers

All 8 Replies

I hope anyone can help me to solved this code... the problem is at line 21 "while($row=mysql_fetch_array($result)) "

can you put if(mysql_num_rows($result) > 0) before the while()

You have to initialize the variables: if $submit is not set then $sql is not initialized and the query will fail.

Also for the query you can use the column BETWEEN value AND value condition. So change your code to:

$submit = $_POST['submit'];

# all code under this condition
if($submit)
{
    $sql = "SELECT * FROM inventory WHERE createdate BETWEEN '$date_in' AND '$date_out'";

    $result = mysql_query($sql) or die(mysql_error());

    if(mysql_num_rows($result) > 0)
    {
        while($row = mysql_fetch_array($result))  
        {
            echo "
            <tr>

                <td align='center' width='200'>
                {$row['date_in']}
                </td>

                <td align='center' width='200'>
                {$row['date_out']}
                </td>

                <td align='center' width='200'>
                {$row['model']}
                </td>

            </tr>
            ";  
        }
    }
}

echo "</table>";

For more information check:

You should use prepared statements. Take a look especially to the second link. Bye!

This Code will definitely help you as it is certified by PHP 5.5 developers.
you can check it by right clicking in NetBeans Project windows and click on INSERT -> CONNECTION TO DATABASE then sleect your desired database and walk through the wizard to finesh it. when you click the finish you will see a databse connection php code. then do again this. INSERT -> DATABASE TABLES
then select desired table along with table fields. then click ok
you will see code to fetch data from table. there you go here is code

<?php

$conn = mysqli_connect('localhost', 'root', '', 'mysql?zeroDateTimeBehavior=convertToNull', '3306');
if (!$conn) {
    die('Could not connect to MySQL: ' . mysqli_connect_error());
}


mysqli_query($conn, 'SET NAMES \'utf8\'');
echo '<table>';
echo '<tr>';
echo '<th>event_time</th>';
echo '<th>user_host</th>';
echo '<th>thread_id</th>';
echo '<th>server_id</th>';
echo '<th>command_type</th>';
echo '<th>argument</th>';
echo '</tr>';
$result = mysqli_query($conn, 'SELECT event_time, user_host, thread_id, server_id, command_type, argument FROM general_log');


//here is your mistake..........the correct way to fetch records.
while (($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) != NULL) {
    echo '<tr>';
    echo '<td>' . $row['event_time'] . '</td>';
    echo '<td>' . $row['user_host'] . '</td>';
    echo '<td>' . $row['thread_id'] . '</td>';
    echo '<td>' . $row['server_id'] . '</td>';
    echo '<td>' . $row['command_type'] . '</td>';
    echo '<td>' . $row['argument'] . '</td>';
    echo '</tr>';
}
mysqli_free_result($result);

echo '</table>';
mysqli_close($conn);

?>
Member Avatar for diafol

A few things.

1) Try to avoid mixing html/php wherever possible.
2) Use mysqli/PDO instead of mysql.
3) Don't echo every little thing. Instead concatenate your output and make one echo.

it was still didnt display any of output as i expected. i had tried the solution from "cereal" but the output still remain the same as displayed data for all of the dates records but it did not displayed the data from starting date to end date.

Ok, few questions:

  1. createdate is a datetime column?
  2. if yes, can you show the date format sent by the form?
  3. can you show the table schema? I'm not sure if you want to compare the date_in and date_out columns.

If the where condition is applied, it shouldn't return rows out of the range, an example here: http://sqlfiddle.com/#!2/6f8dbc/5

If the schema matches then you can try to modify the example on sqlfiddle to get your desired results.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.