0

I'm making a page to register damage mobile phones. I made a search query to search data in database which matched datepicker's date. Here is my source code. (Only relevant to this question, )

<?php
 $con = mysql_connect("localhost", "root", "");
if ($con) {
$db = mysql_select_db('mobile', $con);
 } else {
die('Could not connect: ' . mysql_error());
}

 if (array_key_exists('myday', $_POST)) {
$mydate = $_POST['mydate'];
$myday = "SELECT * FROM mobile_rep WHERE sdate='{$mydate}' ";
}

 mysql_close($con)
  ?>

<html>
<head> </head>
<body>
<form action="index.php" method="POST" class="form-inline">
  <div class="span12" style="border-radius: 5px; border-style: solid; border-width: 1px; margin-left: 20px;" >
    <h5 style="margin-left: 10px; "> <b> Current User List </b></h5>
    Select By Date
    <input type="date" name="mydate" id="mydate" class="input-medium" />
    &nbsp;
    <input type="submit" class="btn-primary  " name="myday" value="Search by date" id='myday' />
    <br/> <br/>
    <table border="1" width="300" cellspacing="1" cellpadding="1" class="table table-striped" style=" border-radius: 5px; border-style: solid; border-width: 1px; ">
      <thead>
      <tr>

        <th>sdate</th>
        <th>model_no</th>
        <th>fault</th>
        <th>rp_fee</th>
        <th>sp_fee</th>
        <th>total</th>
      </tr>
      </thead>
      <tbody>
      <?php
      while ($row1 = mysql_fetch_array($myday)) {
        echo "<tr>";
        echo "<td>" . $row1['sdate'] . "</td>";

        echo "<td>" . $row1['model_no'] . "</td>";
        echo "<td>" . $row1['fault'] . "</td>";
        echo "<td>" . $row1['rp_fee'] . "</td>";
        echo "<td>" . $row1['sp_fee'] . "</td>";
        echo "<td>" . $row1['total'] . "</td>";
        echo "</tr>";
      }
      ?>
      </tbody>
    </table>
  </div>
</form>
</body>
</html>


And I'm having
Undefined variable: myday in C:\wamp\www\mobile\index.php on line 227
Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in C:\wamp\www\mobile\index.php on line 227
Line 227 means while ($row1 = mysql_fetch_array($myday)) {

I also uploaded an image of my page too. enter image description here
Please help me.

m59
  • 43,214
  • 14
  • 119
  • 136
Dilini
  • 149
  • 2
  • 4
  • 13
  • Might be because you closed your connection (prematurely) above that line (while ($row1 = mysql_fetch_array($myday))) `mysql_close($con)` - put it after your query has finished doing its job. – Funk Forty Niner Dec 14 '13 at 14:35
  • :( sadly not working.. @Fred -ii- – Dilini Dec 14 '13 at 14:41
  • Hm.... I see that you're trying to execute everything from the same page. If so, then set your action to `
    – Funk Forty Niner Dec 14 '13 at 14:49
  • You could also move your `$mydate = $_POST['mydate'];` above your `if (array_key_exists('myday', $_POST)) {` or remove the `if` condition altogether. I think the `$mydate = $_POST['mydate'];` may be outside the scope. – Funk Forty Niner Dec 14 '13 at 15:01
  • @fred-ii- by the way I found the answer, it was a error on db column's name. so sorry to bother you. so thank you guys so much. – Dilini Dec 14 '13 at 15:03
  • Not a problem, that was going to be my next question, whether there were actual results from/in DB. Am glad you found the answer, cheers :) and you're welcome. – Funk Forty Niner Dec 14 '13 at 15:05
  • so sorry to bother you guys.. :) – Dilini Dec 14 '13 at 15:06
  • No permanent damage ;-) – Funk Forty Niner Dec 14 '13 at 15:06
  • possible duplicate of [mysql\_fetch\_array() expects parameter 1 to be resource, boolean given in select](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-boolean-given-in-select) – John Conde Dec 19 '13 at 01:46

1 Answers1

0

When you enter your page first time, $myday variable is not set. If you send your POST code then if (array_key_exists('myday', $_POST)) { } is executed and $myday is created.

Just check before while loop if your variable exists, otherwise it pass null value to mysql_fetch_data function

if(isset($myday)) { while ($row1 = mysql_fetch_array($myday)) ... } 
Sean Doe
  • 277
  • 2
  • 9
  • I'll keep that it mind.. by the way I found the answer, it was a error on db column's name. so sorry to bother you. so thank you guys so much. – Dilini Dec 14 '13 at 15:06