In the PHP code below i want to select all the column values of a table and make them options of a select form. The result is that i dont get any options at all. Could someone help? Thanks
<?php
// ....
$userid=$_SESSION['userid'];
echo "<select>";
$sql = "SELECT * FROM users where userid='".$userid."'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo "<option>" .$row['company']. "</option>";
}
echo "</select>";
mysqli_close();
?>
It may just be a typo but you have mixed mysql and mysqli functions, you have stated mysql_query and mysql_fetch_array, whereas you have closed it with mysqli_close, could be an issue depending on what you wrote above this code or indeed if this is not just a typo.
Other things to try would be try the query in your mysql client / phpmyadmin and see if it comes up with any results or errors.
Related
I think I am nearly there with this problem, but I can't quite see why my solution isn't working. I'm trying to pre-select an item from a php drop down list that uses a MySQL table. The drop down list populates as expected, but the pre-select doesn't work. My code:
echo $Trans1E; // Display Existing value
echo '<p>Trans1: ';
$q = "SELECT TR1_Name FROM sb_TR1 ORDER BY TR1_Name";
$r = #mysqli_query ($dbc, $q);
$row = mysqli_fetch_array ($r, MYSQLI_NUM);
echo "<select name='Trans1' value=''>Trans1</option>"; // list box select command
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC))
{//Array of records stored in $row
$selected='';
if($row[TR1_Name]==$Trans1E) //determine if the row value is the same as the Existing
{//if it it then mark as selected
echo "<option value=$row[TR1_Name] selected='selected'>$row[TR1_Name]</option>";
}
else
{// if it is not then just inlcude it in the drop down list
echo "<option value=$row[TR1_Name] >$row[TR1_Name]</option>";
}
}
echo "</select>";// Closing of list box
echo '
</p>';
I've seen similar things on this forum for drop down lists which I've incorporated in my code or at least experimented with but without success, I suspect an error in: if($row[TR1_Name]==$Trans1E), but have run out of thinsg to try.
It seems I was closer to the correct code than I thought, I resolved the problem by adding the line:
$Trans1E=mysqli_real_escape_string($dbc, trim(htmlentities($Trans1E)));
before the code.
Despite displaying the content of Trans1E, it seems there was an unwanted element in it, which I hadn't spotted so I thought the code further down was at fault.
Thanks for you comments. Hope this helps others.
I am writing code to register a new project via a html form. I want to be able to click a dropdown box which pulls the values from a table on the database.
At the moment a dropdown box displays but with no values.
PLEASE NOTE: I am a learning the basics so apologies if this is a simple question/answer scenario.
My code is below, any help is appreciated, I connect to the database via a php include script. The table is called 'customers' and the item I want to list is 'name';-
<?php
$result = mysql_query("SELECT customers FROM name");
echo "<select name='client'>";
while($row = mysql_fetch_assoc($result))
{
echo "<option value = '".$row[name]."'>".$row[name]."</option>";
}
echo "</select>";
?>
"The table is called 'customers' and the item I want to list is 'name';" -
Do SELECT name FROM customers instead of SELECT customers FROM name
Using mysql_error() to mysql_query()
would have shown you the error that the table name does not exist.
Plus,
[name] are missing quotes inside them => ['name'] which are being treated as constants.
in
echo "<option value = '".$row[name]."'>".$row[name]."</option>";
as caught and kudos to devdesign
echo "<option value = '".$row['name']."'>".$row['name']."</option>";
However, you are using a deprecated MySQL library. If you are still not getting results, then this could mean that you need to use (and should use) mysqli_ or PDO instead.
Here are a few links on the subject:
mysqli with prepared statements
PDO with prepared statements.
Your code should be. Also note the single quotes within $row['name']. You missed that.
<?php
$result = mysql_query("SELECT name FROM customers");
echo "<select name='client'>";
while($row = mysql_fetch_assoc($result))
{
echo "<option value = '".$row['name']."'>".$row['name']."</option>";
}
echo "</select>";
?>
I'm trying to create a select box full of options based on data from an array that is built from an sql query, I've tried researching where I'm going wrong but the closest I've managed to get is echoing alot of empty option boxes my code is as follows:
$result = mysqli_query($cons,"SELECT user_name FROM users");
while($row = mysqli_fetch_array($result)){
foreach($row as $key => $value){
echo '<option value="'.$value.'"</option>';}}
If anybody could point me in the right direction that would be great.
EDIT: I can't believe it was something as simple as that! Thank you very much.
On a separate note It's actually giving all of the data twice.
I'll mark as correct answer as soon as I can.
echo '<option value="'.$value.'">'.$value.'</option>';
or
echo "<option value=\"{$value}\">{$value}</option>";
You never closed the opening option tag. If you view your source you'll see it's wonky.
BTW, You don't need the value="" part if you're using the same value in the text of the option.
echo "<option>{$value}</option>";
Entire code as I would do it
$result = mysqli_query($cons,"SELECT user_id, user_name FROM users");
while($row = mysqli_fetch_assoc($result)){
echo "<option value=\"{$row['user_id']}\">{$row['user_name']}</option>";
}
i have a problem:
if(isset($_POST['send'])){
$id = mysql_real_escape_string($_POST['id']);
$query = mysql_query("select * from somewhere where id='$id'");
$row = mysql_fetch_array( $query );
if(!mysql_num_rows($query)==1){
echo('error');
}
}
After this i have this echo from db:
<input type="text" name="up_name" value="<?php echo $row['name'];?>" id="up_name"/>
and this echo:
<select>
<?php
$up_id=$_POST['up_id'];
$sqlDateUser=mysql_query("SELECT `something` from `somewhere` where id='".$id."'");
$res=mysql_fetch_assoc($sqlDateUser);
$somethig_selected=$res['something'];
$something=mysql_query("SELECT `den` FROM `jud`");
while($row = mysql_fetch_row( $something)){
$selected=($row[0]==$somethig_selected)?'selected':'';
echo "<option value='".$row[0]."' ".$selected.">".$row[0]."</option>";
}
?>
</select>
In this order all it is ok but if i change it, first echo doesn't work. I need to display and others rows after these and i don't know what is the problem with the second echo. Can someone tell me what is the problem?
It may be a naming issue. For example, you have your second query assigning your values for mysql_fetch_row into a variable named $row.
That also is the same name you've given your first query, where you are assigning your mysql_fetch_array.
I would probably try naming them something different like $row_get_user_info and $row_get_den to differentiate them. Also it would make it easier if, when you moved your first echo line, that you also move the query that goes along with it. (Keep them together.)
When I click on any link it opens all movies in my database. I want only that movie which begins with that letter and I don't know where I've made a mistake. Here is my code:
$azRange = range('A', 'Z');
foreach ($azRange as $letter){
echo ''.$letter.' | ';
}
if(isset($_GET["task"]) && $_GET["task"] == "view"){
$naslov = $_GET['naslov'];
$query = "SELECT filmovi.naslov, filmovi.godina, filmovi.trajanje, filmovi.slika
FROM filmovi
ORDER BY naslov";
$result = mysql_query($query)
or die ('SQL Greska: '.mysql_error());
if($result){
while($filmovi = mysql_fetch_array($result)){
echo '<center><b>';
echo '<td><img src="img/'.$filmovi["slika"].'" border="0" width="100" /></td>';
echo '</br>';
echo '<td>'.$filmovi["naslov"].'</td>';
echo '<td> ('.$filmovi["godina"].')</td>';
echo '<br>';
echo '<td>Trajanje: '.$filmovi["trajanje"].' min</td>';
echo '</b></center>';
echo '</tr>';
}
You are not passing the letter to the database query at any point.
$query =
"SELECT filmovi.naslov, filmovi.godina, filmovi.trajanje, filmovi.slika
FROM filmovi
WHERE naslov LIKE '$naslov%'
ORDER BY naslov";
Your query
$query = "SELECT filmovi.naslov, filmovi.godina, filmovi.trajanje, filmovi.slika
FROM filmovi
ORDER BY naslov";
is fetching all the movies from the database. There is no filtering here. Add some where conditions to this query and you'll get the expected result.
Changing to this query might help:
SELECT filmovi.naslov, filmovi.godina, filmovi.trajanje, filmovi.slika
FROM filmovi
WHERE `naslov` LIKE '{$naslov}%'
ORDER BY naslov
Since others have already answered your question (missing WHERE clause), I just want to mention that the <center> HTML tag is deprecated, and you should use CSS instead.
The mysql driver for PHP is also outdated, so instead of using:
mysql_query($query);
you should use
mysqli_query($link, $query);
for better security, OOP support, prepared statements, and transactions.
You can read about it here
Even if you are a beginner and you don't care about what those features mean, you should try and get into the habit of using mysqli anyway, so that when the day comes that you learn to appreciate it, you don't have to go back and update all of your code.