I'm having a problem creating a dropdown list and populating it with information from a database. When I display the page below I get this PHP code displayed in place of the drop down menu:
"
\n"; while ($row = mysqli_fetch_array($result)) { $str .= '' . $row . '
' . "\n"; } $str .= ''; echo $str ?>"

<html>
<head><link rel="stylesheet" type="text/css" href="styles.css" />
<title>Add A New Game to the Database</title</head>
<body>

<h3>Add A New Game to the Database</h3>

<form enctype="multipart/form-data" action="process_games.php" method="post">
<p><table border=0>
<TR>
<TD>Select A System</TD>
<TD>

<?php
  require_once ('mysqli_connect.php');

  $sql = "select systemid, systemName
           from systems
           order by systemName";

  $result = mysqli_query($dbc, $sql) or die( "Could not execute query: $query" );

  $str = "<SELECT NAME=systemid><BR>\n";
  while ($row = mysqli_fetch_array($result)) {
        $str .= '<OPTION VALUE=' .  $row['systemid'] . '>' . $row['systemName'] . '<BR>' . "\n";
  }
  $str .= '</SELECT>';

  echo $str
?>

</TD>
</TR>

<TR>
<TD>Select A Genre</TD>
<TD>

<?php
  require_once ('mysqli_connect.php');

  $sql = "select genreid, genre
           from genre
           order by genre";

  $result = mysqli_query($dbc, $sql) or die( "Could not execute query: $query" );

  $str = "<SELECT NAME=genreid><BR>\n";
  while ($row = mysqli_fetch_array($result)) {
        $str .= '<OPTION VALUE=' .  $row['genreid'] . '>' . $row['genre'] . '<BR>' . "\n";
  }
  $str .= '</SELECT>';

  echo $str
?>

</TD>
</TR>

<TR>
<TD>What is the condtion of the game?</TD>
<TD>

<?php
  require_once ('mysqli_connect.php');

  $sql = "select conditionid, condition_name
           from condition_tbl
           order by condition_name";

  $result = mysqli_query($dbc, $sql) or die( "Could not execute query: $query" );

  $str = "<SELECT NAME=conditionid><BR>\n";
  while ($row = mysqli_fetch_array($result)) {
        $str .= '<OPTION VALUE=' .  $row['conditionid'] . '>' . $row['condition_name'] . '<BR>' . "\n";
  }
  $str .= '</SELECT>';

  echo $str
?>

</TD>
</TR>

<input type="hidden" name="MAX_FILE_SIZE" value="1000000">

<TR>
<TR>
<TD>Game Title:</TD>
<TD><input type="text" name="product_name" size="41"></TD>
</TR>
<TR>
<TD>Product Quantity:</TD>
<TD><input type="text" name="quantity" size="41"></TD>
</TR>
<TR>
<TD>Product Price:</TD>
<TD><input type="text" name="price" size="41"></TD>
</TR>
<TR>
<TD>Upload game image: </TD>
<TD><input type="file" name="filename" size="41"></TD>
</TR>

</table>
<BR><BR>
<input type="submit" value="Submit">
</form>

</body>
</html>

Recommended Answers

All 7 Replies

Seems to be a simple mistake :

Add </option> at line 50 and 75 :)

Vivi
First, solve the problem. Then write the code.

Member Avatar for rajarajan2017

Yes at 50 & 75

$str .= '<OPTION VALUE=' .  $row['conditionid'] . '>' . $row['condition_name'] . '</OPTION>'.'<BR>' . "\n";

I believe I stated the same, rajrajan :)

it's still not working. now it says this:
Select A System "; while ($row = mysqli_fetch_array($result)) { $str .= '' . $row . ''.'
' . "\n"; } $str .= ""; echo $str ?>
Select A Genre
\n"; while ($row = mysqli_fetch_array($result)) { $str .= '' . $row . ''.'
' . "\n"; } $str .= ''; echo $str ?>
What is the condtion of the game?
\n"; while ($row = mysqli_fetch_array($result)) { $str .= '' . $row . ''.'
' . "\n"; } $str .= ''; echo $str ?>

I know the php works on other pages i've tried. it seems for some reason, the php is not working on this page.

updated code:

<html>
<head><link rel="stylesheet" type="text/css" href="styles.css" />
<title>Add A New Game to the Database</title</head>
<body>

<h3>Add A New Game to the Database</h3>

<form enctype="multipart/form-data" action="process_games.php" method="post">
<p><table border=0>
<TR>
<TD>Select A System</TD>
<TD>

<?php
  require_once ('mysqli_connect.php');

  $sql = "select systemid, systemName
           from systems
           order by systemName";

$str = "<select name='systemid'>";
while ($row = mysqli_fetch_array($result)) {
        $str .= '<OPTION VALUE=' .  $row['systemid'] . '>' . $row['systemName'] . '</OPTION>'.'<BR>' . "\n";
}
$str .= "</select>";


  echo $str
?>

</TD>
</TR>

<TR>
<TD>Select A Genre</TD>
<TD>

<?php
  require_once ('mysqli_connect.php');

  $sql = "select genreid, genre
           from genre
           order by genre";

  $result = mysqli_query($dbc, $sql) or die( "Could not execute query: $query" );

  $str = "<SELECT NAME=genreid><BR>\n";
  while ($row = mysqli_fetch_array($result)) {
        $str .= '<OPTION VALUE=' .  $row['genreid'] . '>' . $row['genre'] . '</OPTION>'.'<BR>' . "\n";
  }
  $str .= '</SELECT>';

  echo $str
?>

</TD>
</TR>

<TR>
<TD>What is the condtion of the game?</TD>
<TD>

<?php
  require_once ('mysqli_connect.php');

  $sql = "select conditionid, condition_name
           from condition_tbl
           order by condition_name";

  $result = mysqli_query($dbc, $sql) or die( "Could not execute query: $query" );

  $str = "<SELECT NAME=conditionid><BR>\n";
  while ($row = mysqli_fetch_array($result)) {
        $str .= '<OPTION VALUE=' .  $row['conditionid'] . '>' . $row['condition_name'] . '</OPTION>'.'<BR>' . "\n";
  }
  $str .= '</SELECT>';

  echo $str
?>

</TD>
</TR>

<input type="hidden" name="MAX_FILE_SIZE" value="1000000">

<TR>
<TR>
<TD>Game Title:</TD>
<TD><input type="text" name="product_name" size="41"></TD>
</TR>
<TR>
<TD>Product Quantity:</TD>
<TD><input type="text" name="quantity" size="41"></TD>
</TR>
<TR>
<TD>Product Price:</TD>
<TD><input type="text" name="price" size="41"></TD>
</TR>
<TR>
<TD>Upload game image: </TD>
<TD><input type="file" name="filename" size="41"></TD>
</TR>

</table>
<BR><BR>
<input type="submit" value="Submit">
</form>

</body>
</html>
Member Avatar for diafol

Rename the file with a php extension (e.g. page.php). If you've already done this - are you sure that the server is running php?

Rename the file with a php extension (e.g. page.php). If you've already done this - are you sure that the server is running php?

Renaming it took care of the problem, thanks. Now two of the drop down menus work but the system one gives this error:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\Program Files\IndigoPerl\apache\htdocs\add_game.php on line 22

<html>
<head><link rel="stylesheet" type="text/css" href="styles.css" />
<title>Add A New Game to the Database</title</head>
<body>

<h3>Add A New Game to the Database</h3>

<form enctype="multipart/form-data" action="process_games.php" method="post">
<p><table border=0>
<TR>
<TD>Select A System</TD>
<TD>

<?php
  require_once ('mysqli_connect.php');

  $sql = "select systemid, systemName
           from systems
           order by systemName";

$str = "<select name='systemid'>";
while ($row = mysqli_fetch_array($result)) {
        $str .= '<OPTION VALUE=' .  $row['systemid'] . '>' . $row['systemName'] . '</OPTION>'.'<BR>' . "\n";
}
$str .= "</select>";


  echo $str
?>

</TD>
</TR>

<TR>
<TD>Select A Genre</TD>
<TD>

<?php
  require_once ('mysqli_connect.php');

  $sql = "select genreid, genre
           from genre
           order by genre";

  $result = mysqli_query($dbc, $sql) or die( "Could not execute query: $query" );

  $str = "<SELECT NAME=genreid><BR>\n";
  while ($row = mysqli_fetch_array($result)) {
        $str .= '<OPTION VALUE=' .  $row['genreid'] . '>' . $row['genre'] . '</OPTION>'.'<BR>' . "\n";
  }
  $str .= '</SELECT>';

  echo $str
?>

</TD>
</TR>

<TR>
<TD>What is the condtion of the game?</TD>
<TD>

<?php
  require_once ('mysqli_connect.php');

  $sql = "select conditionid, condition_name
           from condition_tbl
           order by condition_name";

  $result = mysqli_query($dbc, $sql) or die( "Could not execute query: $query" );

  $str = "<SELECT NAME=conditionid><BR>\n";
  while ($row = mysqli_fetch_array($result)) {
        $str .= '<OPTION VALUE=' .  $row['conditionid'] . '>' . $row['condition_name'] . '</OPTION>'.'<BR>' . "\n";
  }
  $str .= '</SELECT>';

  echo $str
?>

</TD>
</TR>

<input type="hidden" name="MAX_FILE_SIZE" value="1000000">

<TR>
<TR>
<TD>Game Title:</TD>
<TD><input type="text" name="product_name" size="41"></TD>
</TR>
<TR>
<TD>Product Quantity:</TD>
<TD><input type="text" name="quantity" size="41"></TD>
</TR>
<TR>
<TD>Product Price:</TD>
<TD><input type="text" name="price" size="41"></TD>
</TR>
<TR>
<TD>Upload game image: </TD>
<TD><input type="file" name="filename" size="41"></TD>
</TR>

</table>
<BR><BR>
<input type="submit" value="Submit">
</form>

</body>
</html>

on line 20 Add

$result = mysqli_query($dbc, $sql) or die( "Could not execute query: $query" );

You are using mysqli_fetch_array($result) on line 22 but $result is not defined when it is executed.

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.