Jump to content

easy problem, i think ;/ selection menu not showing rows


loxfear
Go to solution Solved by ginerjm,

Recommended Posts

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
 <?php
 //MySQL Database Connect
 include 'sqlconnect.php'; 
?>

<select name="aktivitet" id="aktivitet">
<?php 
 //MySQL Database Connect
 include 'sqlconnect.php'; 
$sql = mysql_query("SELECT title FROM aktiviteter");
while ($row = mysql_fetch_array($sql)){
echo "<option value=\"owner1\">" . $row['title'] . "</option>";
}
?>
</select>
</body>
</html>

why dosnt this work?,   if you cant see what i want to happen, ill post an explaination

 

Link to comment
Share on other sites

Looking at the code it should be populating the menu with the results of the query.

 

For why it is not working there could be an error with your query, which you have not checked for.

$sql = mysql_query("SELECT title FROM aktiviteter");

// check query did not return an error
if(!$sql)
   trigger_error('DB Error: '  . mysql_error(), E_USER_ERROR);

Also you only need to include sqlconnect.php once, there is no need to include it each time you perform a query.

 

Having said that though you should really change your code over to PDO or MySQLi. The mysql_* functions are deprecated and no longer supported, which means they could be removed from future versions of PHP. So this would be good time change.

Link to comment
Share on other sites

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
 <?php
 //MySQL Database Connect
 include 'sqlconnect.php'; 
?>

<select name="aktivitet" id="aktivitet">
<?php 
$sql = mysqli_query($con,"SELECT title FROM aktiviteter");
// check query did not return an error
if(!$sql)
   trigger_error('DB Error: '  . mysql_error(), E_USER_ERROR);

while ($row = mysqli_fetch_array($sql)){
echo "<option value=\"owner1\">" . $row['title'] . "</option>";
}
?>
</select>
</body>
</html>

this is how it looks, i cant detect any difference still doesnt work  

 

http://polterplanner.dk/bestillere.html

 

thats where it is . dont know if that would help?

Link to comment
Share on other sites

  • Solution

OK - try this.  It's your code with some error checking and some re-arrangement to make it more readable.

 

<?php
error_reporting(E_ALL | E_STRICT | E_NOTICE);
ini_set('display_errors', '1');
//MySQL Database Connect
include 'sqlconnect.php';
$sql = mysqli_query($con,"SELECT title FROM aktiviteter");
// check query did not return an error
if(!$sql)
{
     trigger_error('DB Error: '  . mysql_error(), E_USER_ERROR);
     exit();
}
$options = "<select name='aktivitet' id='aktivitet'>";
while ($row = mysqli_fetch_array($sql))
{
    //  NOTE - ALL OF YOUR OPTIONS WILL HAVE THE EXACT SAME VALUE CLAUSE - N.G.!!
    $options .= "<option value='owner1'>" . $row['title'] . "</option>";
}
$options .= "</select>";
//  NOW begin sending html!!!
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
$options
</body>
</html>
Link to comment
Share on other sites

PHP code does work in .html files (unless the server is confgured to do so).

 

I suggest to rename your file so it ends in .php

 

 

 

 but i have to say, im quite a newbie so im not sure if your push will help me

If you are new to PHP I highly recommend you to learn PDO or MySQLi. 

Link to comment
Share on other sites

"; while ($row = mysqli_fetch_array($sql)) { // NOTE - ALL OF YOUR OPTIONS WILL HAVE THE EXACT SAME VALUE CLAUSE - N.G.!! $options .= ""; } $options .= ""; // NOW begin sending html!!! ?> $options

 

what it gave me

Link to comment
Share on other sites

<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
<?php
 //MySQL Database Connect
 include "sqlconnect.php"; 


echo "<select name='aktivitet' id='aktivitet'>"

$sql = mysqli_query($con,"SELECT title FROM aktiviteter");
// check query did not return an error
if(!$con)
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

while ($row = mysqli_fetch_array($sql)){
echo "<option value=\"owner1\">" . $row['title'] . "</option>";
}
echo "</select>"
?>

now thats how it looks :) and php extendtion

Link to comment
Share on other sites

HTH!  Glad you understand what I did - it is good to keep the logic away from the presentation as much as possible.

 

PS - I hope you are doing an error check on the connection in that included code and not doing it as you did in your last posted sample.  Check that the connection worked when you do the connect, and check that the query works when you do the query.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.