I need to get the values of the selected select
like the orderno of the changed row
The table looks like this
if I change order no 1002 into sold
$query = mysql_query("select * from orders");
while($row = mysql_fetch_assoc($query))
{
echo '<tr>';
echo '<td>'.$row['orderno'].'</td>';
echo '<td>'.$row['total'].'</td>';
echo '<td>';
if($row['status'] == "sold")
{
echo '<select name = "status[]">';
echo '<option value = "sold" selected>Sold</option>';
echo '<option value = "cancelled">Cancelled</option>';
echo '</select>';
}
else
{
echo '<select name = "status[]">';
echo '<option value = "sold">Sold</option>';
echo '<option value = "cancelled" selected>Cancelled</option>';
echo '</select>';
}
echo '</td>
echo </tr>';
}
if(isset($_POST['status']))
{
//sample only
echo "You change orderno ".$orderno_here." with the
total of ".$total_of_order." into ".$selected_status_of_orderno_here;
}
There are many records in the table and the selects are the same
how do I get the values of the selected select
Following your question, whatever i understood, according to that i am supposing you are getting data from form like tags in your page. If that is the case, you know that which value is changed to be reflected in db. So you can get that selected select from this.
you can check the change through this code:
if(isset($_POST['element'])){
//if this block executed, the <element> named element is changed
}
ignore this answer if this is not the case you are using in your code..
Related
I have the following query and I'm trying to select only the rows where I have picture.
In table kat_gl_picture I have 3 categories, but I don't have picture in all 3 categories yet!
All work just fine, but I have printed name of third category, where I don't have picture.
I tried WHERE link LIKE '%$first_var%'AND NOT (link <=> NULL)
....IS NOT NULL - but nothing yet worked.
Tabele1 and 2 and web problem solved
<?
include("connection.php");
$kategorije = mysql_query("SELECT * FROM kat_gl_picture ORDER BY rbr");
while ($red=mysql_fetch_array($kategorije))
{
$first_var = $red['kat'];
$result = mysql_query("SELECT id, naziv, ime, tekst, username, link, file_name, datum FROM Tab_Pic_Pic
WHERE link LIKE '%$first_var%'
ORDER BY id");
echo '<table>';
echo '<tbody>';
echo $first_var;
echo '<tr>';
echo '<TD valign="top">';
while ($row=mysql_fetch_array($result))
{
list($x, $y) = getimagesize("admin /upload/".$row['file_name']);
if ($x>$y) {
$y=($y/$x)*150;
$x=150;
}
else
{
$x=($x/$y)*115;
$y=115;
}
$ID_broj = $row["id"];
$tekst_broj = $row["tekst"];
?>
<? echo '<img src="admin /upload/'.$row['file_name'].'" height="'.$y.'" width="'.$x.'"/>';?>
<?
}
echo '</td>';
echo '</tr>';
echo '</tbody>';
echo '</table>';
}
The problem is that I cannot determine if there is a foreign key in your Tab_Pic_Pic table. You can however join on a LIKE.
Try this:
SELECT * FROM Tab_Pic_Pic JOIN kat_gl_picture ON Tab_Pic_Pic.link LIKE CONCAT('%', kat_gl_picture.kat, '%') ORDER BY kat_gl_picture.kat, Tab_Pic_Pic, rbr
It should give you a list of all pictures including the kat_gl_picture.kat field which you can just use a local variable to detect change.
Would be easier to provide a more accurate example if I had the full table info. Standard method is to have a foreign key in Tab_Pic_Pic that references the corresponding primary key from the kat_gl_picture table.
Alternatively, if you simply want to omit the blank category it can be done in your PHP code like this:
if(mysqli_num_rows($result)>0){
echo '<table>';
echo '<tbody>';
echo $first_var;
echo '<tr>';
echo '<TD valign="top">';
while ($row=mysql_fetch_array($result))
{
list($x, $y) = getimagesize("admin /upload/".$row['file_name']);
if ($x>$y) {
$y=($y/$x)*150;
$x=150;
}
else
{
$x=($x/$y)*115;
$y=115;
}
$ID_broj = $row["id"];
$tekst_broj = $row["tekst"];
?>
<? echo '<img src="admin /upload/'.$row['file_name'].'" height="'.$y.'" width="'.$x.'"/>';?>
<?
}
echo '</td>';
echo '</tr>';
echo '</tbody>';
echo '</table>';
}
Wrapping the output of your HTML in a simple condition like this should illustrate that the script itself is inefficient. Trust me when I say it can all be accomplished with one query. Do not get frustrated. You'll figure it out. Programming is hard.
I have a dropdown list (HTML select box) which gets values from this MySQL query:
"SELECT cdID, cdTitle FROM CD ORDER BY cdID"
The result is then stored in an associative array, which is then output to the dropdown list:
<?php
echo '<select name= "list" id="list">';
while ($row = mysqli_fetch_assoc($result)){
echo '<option value="'.$row['cdTitle'].'">'.$row['cdTitle'].'</option>';
}
echo '</select>';
?>
My issue is that I would like the user to see the title of the CD, but for the actual value to be "cdID" as that is the foreign key used in my database.
Just change the attribute echoed out for value -
<?php
echo '<select name= "list" id="list">';
while ($row = mysqli_fetch_assoc($result)){
echo '<option value="'.$row['cdID'].'">'.$row['cdTitle'].'</option>';
}
echo '</select>';
?>
why not set the option value with the cdID.
echo '<option value="{$row['cdID']}">{$row['cdTitle']}</option>';
or
echo sprintf('<option value="%s">%s</option>',$row['cdID'],$row['cdTitle']);
I'm currently outputting a select menu using the following code;
while($row = mysqli_fetch_array($result) ) {
echo '<option value="'.$row['id'].'">'.$row['title'].'</option>';
}
I'm wondering how I can make the one that is selected in the database appear as selected, seeings as the options are dynamically drawn and added?
Edit: I still want the other options that aren't selected to appear in the list so it can be changed.
You try this way:
while($row = mysqli_fetch_array($result) ) {
$selected=$row['id']==$current_id? "selected": ""; //$current_id is which you want to selected..
echo '<option value="'.$row['id'].'" '. $selected.' >'.$row['title'].'</option>';
}
Make an if condition and then use selected Attribute
while($row = mysqli_fetch_array($result) ) {
if (your condition) {
echo '<option value="'.$row['id'].'" selected>'.$row['title'].'</option>';
} else {
echo '<option value="'.$row['id'].'">'.$row['title'].'</option>';
}
}
I've set up a php form that registers a project to our database, it has a drop down that populates from our customer/supplier databases.
I've also set up a function to edit these projects, the problem I have is that when I go to my edit page it just displays the customer/supplier name and not in the drop down but a value box - is there a way to have the edit page display the dropdown but also be selected on the original supplier/customer?
Register project page
<?php
$result = mysql_query('SELECT name FROM customers ORDER BY name ASC');
echo '<select name='client'>';
while($row = mysql_fetch_assoc($result))
{ `
echo '<option value = ''.$row[name].''>'.$row[name].'</option>';
}`
echo '</select>';
?>
Edit page
<input type='text' name='client' value='<?php echo $client; ?>'/>
I tried a few tutorials and code tweaks but kept getting errors. I am aware of my sql injection problem, at the moment this site is internal.
Any help would be appreciated.
thanks
instead of $row[name] you should use $row['name']
$client= "<select name='client'>"; // you had error here also.
while($row = mysql_fetch_assoc($result))
{
$client.= "<option value = '".$row['name']."'>'".$row['name'].'</option>';
}
$client.= '</select>';
now echo $client to get dropdown.no need of constructing separate select tag now.
for selected use like this:
$client1= "<select name='client'>";
while($row = mysql_fetch_assoc($result))
{
if($row['name'] == $clientValue){
$client.= "<option selected='selected' value = '".$row['name']."'>'".$row['name'].'</option>';
}else{
$client1.= "<option value = '".$row['name']."'>'".$row['name'].'</option>';
}
}
$client1.= '</select>';
on echo of $client1 you will get selected based on the value $clientValue which you have to pass.
On your edit page:
<?php
$result = mysql_query('SELECT name FROM customers ORDER BY name ASC');
echo "<select name=\"customer\">";
while($row = mysql_fetch_assoc($result))
{
if ($row['name'] == $client)
{
echo "<option selected value=\"" . $row['name'] . "\">" . $row['name'] . "</option>";
}
else
{
echo "<option value=\"" . $row['name'] . "\">" . $row['name'] . "</option>";
}
}
echo "</select>";
?>
I also suggest that you change the old extension for mysql. I can't see no SQL Injection problem for now, but you should take care of it even if it is internal, because, from different reasons you will forget to sanitize it later. If you are writing it, then write it correctly.
Now for the problem, you are not using the quotes correctly, hence the errors. Do not use the same type of quotes, but change them, like so:
echo '<select name="client">';
Or if you use double quotes for concatenation, use single inside.
In case you have to use the same, escape them with \
For starters, you have a syntax error here:
echo '<select name='client'>';
(There are probably more quoting errors throughout the code, but I digress...)
As for using a drop-down, what you're looking for is the selected attribute. When you're building the page elements to display the form on the "edit" page, presumably you have the values that you're looking to display. When your loop finds an element which matches the value, select it:
while($row = mysql_fetch_assoc($result))
{
if ($knownValue == $row[name]) {
echo '<option selected value = ''.$row["name"].''>'.$row["name"].'</option>';
} else {
echo '<option value = ''.$row["name"].''>'.$row["name"].'</option>';
}
}
I am creating a table with rows which has options in each cells. The options are received from database table. Since each row has a options cell, I have to receive the value from each row. In select tag, the 'name' is incremented with the rows. But I am only getting the value of the last row.
Attaching the code section here.
for ($i = 1; $i <= $_GET['pno']; $i++) {
echo'<tr>';
echo "<td>$i</td>";
echo '<td><select name="prod_$i">'; echo "prod_$i";
//Query for production table gets processed.
$qry_pr="SELECT * FROM production";
$result_pr=mysql_query($qry_pr);
$rows_pr=mysql_affected_rows($con);
//Production options get filled from the table data.
for($j=0;$j<=$rows_pr;$j++)
{
$res_arr_pr=mysql_fetch_array($result_pr);
echo "<option value=$res_arr_pr[0]>$res_arr_pr[1]</option>";
//$res++;
}
//mysql_close($con);
echo '</select></td>';
echo '<td><select name="prod_mu_$i">';
//Query for measurement_unit table gets processed.
$qry_mu="SELECT * FROM measurement_unit";
$result_mu=mysql_query($qry_mu);
$rows_mu=mysql_affected_rows($con);
//Unit options get filled from the table data
for($k=0;$k<=$rows_mu;$k++)
{
$res_arr_mu=mysql_fetch_array($result_mu);
echo "<option value=$res_arr_mu[0]>$res_arr_mu[1]</option>";
}
echo '</td>';
echo '</tr>'; echo "prod_$i";
}
echo'</table><br>';
Hope I am clear with the query.
Thank you.
You have placed the SQL Query inside the for loop making it heavy. Because it will perform the same query over and over again. If you tweak the code a little you can use perform a single query and use that for all loop iterations.
<?php
//initialize blank
$productions = $measurements = '';
// create the production select box
$qry_pr="SELECT * FROM production";
$result_pr=mysql_query($qry_pr);
if( mysql_num_rows($result_pr) )
{
$productions .= "<select name='prod_%index%'>";
while( $rec_pr = mysql_fetch_row($result_pr) )
{
$productions .= "<option value='{$rec_pr[0]}'>{$rec_pr[1]}</option>";
}
$productions .= "</select>";
}
// create the measurement select box
$qry_mu="SELECT * FROM measurement_unit";
$result_mu=mysql_query($qry_mu);
if( mysql_num_rows($result_mu) )
{
$measurements .= "<select name='prod_mu_%index%'>";
while( $rec_mu = mysql_fetch_array($result_mu) )
{
$measurements .= "<option value='{$rec_mu[0]}'>{$rec_mu[1]}</option>";
}
$measurements .= "</select>";
}
?>
<table>
<?php for($i=1;$i<=$_GET['pno'];$i++): ?>
<tr>
<td><?php echo str_replace('%index%',$i,$productions); ?></td>
<td><?php echo str_replace('%index%',$i,$measurements); ?></td>
</tr>
<?php endfor; ?>
</table>
Change:
echo '<td><select name="prod_$i">'; echo "prod_$i";
to:
echo '<td><select name="prod_' . $i . '[]">'; echo "prod_$i";
mysql_affected_rows will only give you the number of affected rows by the last INSERT, UPDATE, REPLACE or DELETE query. Your performing a SELECT, so the $rows_mu value will be wrong.
Instead of:
$rows_mu=mysql_affected_rows($con);
//Unit options get filled from the table data
for($k=0;$k<=$rows_mu;$k++)
{
$res_arr_mu=mysql_fetch_array($result_mu);
echo "<option value=$res_arr_mu[0]>$res_arr_mu[1]</option>";
}
try this:
//Unit options get filled from the table data
while ($res_arr_mu = mysql_fetch_array($result_mu))
{
echo "<option value=$res_arr_mu[0]>$res_arr_mu[1]</option>";
}