Member Avatar for Eneswar

Hey all, my first post in this brilliant website, got a lot of ideas from here.
So I'm a student and currently we are learning a bit of php and right now I'm stuck.

I have a database with URL adresses, there is 3 fields, id, name and adress.
I have added 10 adresses in the database and I can show them on a website with no problem.

Now I want to show them in a combobox where they I can select any of the adresses, I have written some code but I dont think it can read from the database, the combobox is empty.
What am I doing wrong?

<select name="batch" onchange="submit();">
<option value="-1" >-- Select Adress --</option>
<?php
mysql_connect("localhost" , "root" , "");
mysql_select_db("persontb");
$sql="SELECT * FROM adresser ORDER BY namn ASC";
$result=log_mysql_query($sql);
while($item=mysql_fetch_assoc($result))
{
 echo '<option value="'.$item['id'].'" '.isSelected($item['id'],$batch).'>'.$item['namn'].'</option>';
}
?>
</select>

"log_mysql_query" typo perhaps ? Or is this a function you created ?

Member Avatar for Eneswar

Yea, thats supose to be $result=mysql_query($sql); I think....

But it still doesnt show me the URLs in the combobox..

Member Avatar for Eneswar

This is what I have now, however there is a blank list, dont think it fetches the names from database...?

<select name="batch" onchange="submit();">
<option value="-1" >-- Select Adress --</option>
<?php
mysql_connect("localhost" , "root" , "");
mysql_select_db("persontb");
$sql="SELECT * FROM adresser ORDER BY namn ASC";
$result=mysql_query($sql);
while($item=mysql_fetch_assoc($result))
{
 echo '<option value="'.$item['id'].'" '.$item['namn'].'</option>';
}
?>
</select>

Try this to see if you get any errors:

<select name="batch" onchange="submit();">
<option value="-1" >-- Select Adress --</option>
<?php
mysql_connect('localhost' , 'root' , '') or die(mysql_error());
mysql_select_db('persontb') or die(mysql_error());
$sql = 'SELECT * FROM adresser ORDER BY namn ASC';
$result = mysql_query($sql) or die(mysql_error());
while ($item = mysql_fetch_assoc($result))
{
  echo '<option value="'.$item['id'].'" '.isSelected($item['id'],$batch).'>'.$item['namn'].'</option>';
}
?>
</select>
Member Avatar for Eneswar

I dont get any error at all.

Member Avatar for Eneswar

I got it working now, I had an tag error, missed a ending tag, final code:

<select name="batch" onchange="submit();">
<option value="-1" >-- Select Adress --</option>
<?php
mysql_connect("localhost" , "root" , "");
mysql_select_db("persontb");
$sql="SELECT * FROM adresser ORDER BY namn ASC";
$result=mysql_query($sql);
while($item=mysql_fetch_assoc($result))
{
 echo '<option value="'.$item['id'].'" >'.$item['namn'].'</option>';
}
?>
</select>

Now I need to figure out how to make the URLs open when I choose one of the names in a combobox...


Edit: How would I edit this line:

echo '<option value="'.$item['id'].'" >'.$item['namn'].'</option>';

to include "<a href="$e">" </a> or similar?

Edit 2:
$e is the row I used on a normal page I show the data:

$e = $row["urladresser"];

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.