mysql_fetch_array problem code not processing - php

My page stops loaded every time I turn on the code below... it looks correct and the tables and fields are correct.
<select name="common" style="width: 136px;">
<?php
$group1 = mysql_fetch_array(mysql_query("SELECT country FROM lang_list WHERE grouping = '1' ORDER BY p_order"));
while($row = $group1){
echo "<option value=\"$group1\">$group1</option>\n";
}
?>
</select>

<?php
$group1 = mysql_query("SELECT country FROM lang_list WHERE grouping = '1' ORDER BY p_order");
while($row = mysql_fetch_array($group1)){
echo "<option value=\"$row[country]\">$row[country]</option>\n";
}
?>

Try this:
<select name="common" style="width: 136px;">
<?php
$recordset = mysql_query("SELECT country FROM lang_list WHERE grouping = '1' ORDER BY p_order") or die("Error found: " . mysql_error());
while($row = mysql_fetch_array($recordset)){
echo "<option value=\"".$row['country']."\">".$row['country']."</option>\n";
}
?>
</select>

Related

Php select (Option list)

I am using these codes
$query = "SELECT date FROM arrival order by date" ;
$result = mysqli_query($con,$query)or die ("Error". mysqli_error($con)) ;
echo "<select name='per1' id='per1'>";
while ($row = mysqli_fetch_array ($result)){?>
<option value=<?php echo $row['date'];?> selected='selected'><?php echo $row['date'];?></option>
</select>
<?php
}
?>
The select (Option list) displays data as
2016-07-01
2016-07-02
2016-07-03
2016-07-04
2016-07-05
2016-07-06
and so on
It is not like a dropdown option list.
How to make it like dropdown list?
I want select one date from option.
You are closing the select in the while multiple times. Move </select> outside.
}?> </select>
You also should quote the attribute value. Full example:
$query = "SELECT date FROM arrival order by date" ;
$result = mysqli_query($con,$query)or die ("Error". mysqli_error($con)) ;
echo "<select name='per1' id='per1'>";
while ($row = mysqli_fetch_array ($result)){?>
<option value='<?php echo $row['date'];?>' selected='selected'><?php echo $row['date'];?></option>
<?php } ?>
</select>
A little disorganized clear it up a bit and try this
$query = "SELECT date FROM arrival order by date" ;
$result = mysqli_query($con,$query)or die ("Error". mysqli_error($con)) ;
$result_array = array();
while($row = mysql_fetch_assoc($result))
{
$result_array[] = $row['date'];
}
echo "<select name='per1' id='per1'>";
foreach($result_array as $name){
echo'<option value="'.$name.'">'.$name.'</option>';
}
echo'</select>';
I believe that will do what you're looking for.

Displaying mysqli_fetch array results in dropdown php

I have a drop down that is being populated from a static select. Then when a choice is made in the first drop down a query runs and the second drop down is populated from the database depending on selection in first select box. Here is the code. I'm having a problem displaying the second drop down with the data.
$selectvalue = mysqli_real_escape_string($mysqli, $_GET['selectvalue']);
$result = mysqli_query($mysqli, "SELECT DISTINCT '$selectvalue' FROM accounts ");
echo '<option value="">Please select...</option>';
while($row = mysqli_fetch_array($result))
{
echo '<option value="'.$row['$selectvalue'].'">' . $row['$selectvalue'] . "</option>";
//echo $row['drink_type'] ."<br/>";
}
mysqli_free_result($result);
mysqli_close($connection);
?>
<?php
$selectvalue = mysqli_real_escape_string($mysqli, $_GET['selectvalue']);
$result = mysqli_query($mysqli, "SELECT DISTINCT * FROM accounts WHERE col_name = '".$selectvalue."' ");
echo '
<select name="some_name">
<option value="">Please select...</option>';
while($row = mysqli_fetch_array($result)) {
echo '<option value="'.$row['col_name'].'">'.$row['col_name']."</option>";
}
mysqli_free_result($result);
mysqli_close($connection);
?>

How do I echo SQL data from database table into a <select> form?

I wonder if anyone can help.
I am trying to echo some SQL data into a <select> form with each <option> as a different row from the table.
For example if my table has two columns 'username' and 'category' and there are two rows for the same username, with the data:
"username: test, category: test1."
& second row as:
"username: test, category: test2."
How could I echo 'test1' and 'test2' as two options in a select form?
The table name is 'testtable' so my current $sql query is ("SELECT 'category' FROM 'testtable' WHERE 'username = \'$user\'")
$user is currently set to the $_SESSION['username']
Code examples would be really helpful and much appreciated. I just cant seem to get them to echo in examples I have found on forums.
try this
<select name="your_select_name" id="your_select_id">
<option value="">Select</option>
<?php
$username = $_SESSION['username'];
$res = mysql_query("SELECT `category` FROM `testtable` WHERE `username` = '$username' ") or die(mysql_error());
while($row = mysql_fetch_assoc($res))
{
echo '<option value="'.$row['category'].'">'.$row['category'].'</option>';
}
?>
</select>
UPDATE 2:
For distinct category use this
$res = mysql_query("SELECT DISTINCT(`category`) as category FROM `testtable` WHERE `username` = '$username' ") or die(mysql_error());
OR
$res = mysql_query("SELECT `category` FROM `testtable` WHERE `username` = '$username' GROUP BY category ") or die(mysql_error());
Try this:
<select>
<?php while($row = mysql_fetch_array($result)
{?>
<option><?php echo $row['category'];?></option>
<?php }?>
</select>
You got from your query all rows:
<?php
$query = "SELECT category FROM testtable WHERE username = '" . $_SESSION['username'] ."'";
$result = mysql_query($query);
$options = array();
while($row = mysql_fetch_array($result))
{
$options[] = "<option>" . $row['category'] . "</option>";
}
?>
<select>
<?php echo implode("",$options); ?>
</select>
Your select query has an error: unpaired single quote before username, and single quotes surrounding category - is that possible that was the reason you couldn't do it?
<?php
$result = mysql_query($query);
echo "<select>";
while($row = mysql_fetch_array($result))
{
echo "<option>".$row['category']."</option>";
}
echo "</select>";
?>

Population a dropdown list with PHP without duplicates

I am trying to embed a drop down list to an update page, it works fine but I am having problems with option selected part. When an option is shown it also duplicates it in the list.
Is there any way I can say if record is used do not use it again with PHP?
<?php
$sql = "SELECT TeamName, TeamID FROM tblTeam";
$result = mysql_query($sql);
$player_id = $_GET['id'];
$current_team = mysql_query("SELECT
tblteam.TeamID,
tblteam.TeamName,
tblplayer.PlayerID,
tblplayer.PlayerTeam,
tblplayer.PlayerName
FROM
tblplayer
INNER JOIN tblteam ON tblplayer.PlayerTeam = tblteam.TeamID
WHERE PlayerID = $player_id LIMIT 1 ");
$my_row = mysql_fetch_array($current_team);
?>
<select name="TeamName">
<option selected value="<?php echo $my_row['TeamID']; ?>"> <?php echo $my_row['TeamName']; ?> </option>
<?php
while ($row = mysql_fetch_array($result)) {
$team_name= $row["TeamName"];
$team_id = $row["TeamID"];
echo "<option value=\"$team_id\">$team_name</option>";
}
echo "</select>";
?>
It seems like simple if condition will solve the case for you:
while ($row = mysql_fetch_array($result)) {
$team_name= $row["TeamName"];
$team_id = $row["TeamID"];
if($team_id != $my_row['TeamID']){
echo "<option value=\"$team_id\">$team_name</option>";
}
}
Additionally you should always sanitize $_GET / $_POST params, in your example:
$player_id = intval($_GET['id']);
Intval will return 0 if the given format is not numeric, so your sql query is safe from this moment.

Keeping lastly selected value from a dropdownlist after button click

Just like the title says i'm having difficulties in achieving it.
Here's my dropdownlist:
<?php
$query = "SELECT data, rel_id FROM $tbl_rel_balansas INNER JOIN $tbl_balansas ON $tbl_rel_balansas.rel_id = $tbl_balansas.id WHERE $tbl_rel_balansas.member_id = '$_SESSION[id]' group by data";
$result = mysql_query ($query);
echo "<select name=data value=''>Data</option>";
while($nt=mysql_fetch_array($result)){
echo "<option value=$nt[data] name=\"blabla\">$nt[data]</option>";
}
echo "</select>";
?>
Here's the buttonclick:
<?php
if(isset($_POST['Submit']))
{
$query = "SELECT SUM(suma), paskirtis FROM $tbl_rel_balansas INNER JOIN $tbl_balansas ON $tbl_rel_balansas.rel_id = $tbl_balansas.id WHERE $tbl_rel_balansas.member_id = '$_SESSION[id]' AND data ='".$_POST['data']."' group by paskirtis";
$result = mysql_query ($query);
echo "<tr><td>Paskirtis:</td><td>Biudzetas:</td><td>Isleista:</td><td>Likutis:</td></tr>";
while($nt=mysql_fetch_array($result)){
if($nt['SUM(suma)'] != null){
$suma = $nt['SUM(suma)'];
}
echo "<tr><td>$nt[paskirtis]</td>
<td><input type=\"text\" name=\"isleista[]\" value=\"Skiriamų pinigų kiekis...\" method=\"post\"></td><td>".$suma." Lt</td><td>--</td></tr> <br>";
}
}
?>
After I press it, it retrieves the data I want from the date I've chosen from the drop down list and also reset whole drop down list showing the first value of the dates from sql database, not the one I selected. If anyone knows how to keep the selected value in the list any help is greatly appriciated!
Try this, you need to place select="selected" in the while loop. See below code how I placed the $selected
<?php
$query = "SELECT data, rel_id FROM $tbl_rel_balansas INNER JOIN $tbl_balansas ON $tbl_rel_balansas.rel_id = $tbl_balansas.id WHERE $tbl_rel_balansas.member_id = '$_SESSION[id]' group by data";
$result = mysql_query ($query);
echo "<select name=data value=''>Data</option>";
while($nt=mysql_fetch_array($result)){
$selected = ($_POST['blabla'] == $nt[data])?'selected="selected"':NULL;
echo "<option value=$nt[data] name=\"blabla\" $selected >$nt[data]</option>";
}
echo "</select>";
?>

Categories