2
<?php
$link = mysqli_init();
$db = mysqli_real_connect($link, 'localhost', 'bp6am', 'bp6ampass', 'expense_report') or 
    die('Unable to connect');
mysqli_select_db($link, 'expense_report');

?>
<html>
<head>
<title>Expenditure Log Form</title>
</head>

<body>
<form action="commit.php" method="post">
<table>

<tr>
<td>Name</td>
<td><input type="text" name="name"/><br></td>
</tr>

<tr>
<td>Category</td>
<td><select name="category">
<?php 
//get the expenses categories
$query = 'SELECT cat_id, cat_name
            FROM exp_category
            ORDER BY cat_name';
        $result = mysqli_query($query, $db) or
            die(mysqli_connect_error($db));

//populate the dropdown menu for expenses category
while($row = mysqli_fetch_assoc($result)) {
        foreach ($row as $value) {
            echo '<option value="' . $row['cat_id'] . '">';
            echo $row['cat_name'] . '</option>';
        }
}
?>
</select></td>      
</tr>

<tr>
<td>Amount</td>
<td><input type="text" name="amount"/></td>
</tr>

<tr>
<td>Payment Mode</td>
<td><select name="payment">
<?php
//retrieve and populate
$query = 'SELECT pay_id, pay_type
            FROM pay_mode
                ORDER BY pay_id';
        $result = mysqli_query($query, $db) or
            die(mysqli_connect_error($db));
//populate
while($row = mysqli_fetch_assoc($result)) {
        foreach ($row as $value) {
            echo '<option value="' . $row['pay_id'] . '">';
            echo $row['pay_type'] . '</option>';
        }
}
?>
</select></td>
</tr>

</table>
</form>
</body>

</html>

When I run the file in browser, only the Name field is constructed. After that there is the Category text with the select icon but no menu drops down on clicking it. Rest of the page is blank!! Recently started with PHP please help.

I'm wondering whether the connection is established or not. Even if no connection was established why other HTML elements are not being displayed after the Category field?

I checked my connection parameters. Even if they were wrong I should get the message "Unable to connect" right?

rhlchd
  • 31
  • 1
  • 6

1 Answers1

0

If you had turned error_reporting on, you'd know this from looking at the serverlogs, take a look at How to get useful error messages in PHP (StackOverflow).

Also, take a look at the mysqli_query documentation at PHP.net, it'll tell you that the connection comes first, then the query-string.

You currently use this:

$result = mysqli_query($query, $db)

while it should be

$result = mysqli_query($db, $query)

And to answer your question, yes you would get a Unable to connect if it couldn't connect (wrong parameters).

You also don't need a foreach-loop in your while-loop.

while($row = mysqli_fetch_assoc($result)) {
        foreach ($row as $value) {
            echo '<option value="' . $row['cat_id'] . '">';
            echo $row['cat_name'] . '</option>';
        }
}

The code above should be this instead:

while($row = mysqli_fetch_assoc($result)) {
    echo '<option value="' . $row['cat_id'] . '">';
    echo $row['cat_name'] . '</option>';
}
Community
  • 1
  • 1
Qirel
  • 25,449
  • 7
  • 45
  • 62
  • Thanks Qirel. I just replaced mysql_real_connect with mysql_connect and corrected the mysqli_query as you mentioned. It worked. – rhlchd Sep 16 '15 at 07:02
  • Just one doubt what exactly is the difference between mysqli_connect and mysqli_real_connect? PHP documentation says mysqli_real_connect requires mysqli_init() to be executed before connection and a mysqli variable as a parameter is required in mysqli_real_connect(). But what exactly is the purpose of doing this two step procedure? – rhlchd Sep 16 '15 at 07:04
  • Yes, when using `real_connect` you have to put a MySQLi object as parameter one (the `mysqli_init`). But you also have a `flag` parameter (8th), and you can use `mysqli_options()` with it (you can't with `mysql_connect`). But for normal connecting, it's really not worth the trouble. I also noticed that you have `mysqli_select_db($link, 'expense_report');` in your code. It should be `$db` instead of `$link` in your current code, just a comment. And a tip: use if-statements instead of `die` to check if connection, query, etc returns true. – Qirel Sep 16 '15 at 11:21