I am new to php and dropdown menus, my query works, but when displaying it is always missing the last row entered. So if I have two rows entered, the drop down will only display one? What am I doing wrong?
<?php
require("********");
$query=mysql_query("select * from types");
echo "<table >
<tr align='left'>
<th><font color='red'>Description</th>
</tr>";
$options='';
while($dbfield = mysql_fetch_array($query))
{
$options .= '<option>'.$dbfield['Description'].'</option>';
echo "
<form method='post'>
<td><select name='Description'><? echo $options; ?></select>
</tr>";
You don't close (}) your while loop.
You're also not consistent
with your table structure.
mysql_ functions are also deprecated.
<?php
require("********");
$query=mysql_query("select * from types");
echo "<form method='post'>
<table>
<tr align='left'>
<th><font color='red'>Description</th>
</tr>";
$options='';
while($dbfield = mysql_fetch_array($query)) {
$options .= '<option>'.$dbfield['Description'].'</option>';
}
echo "<tr>
<td><select name='Description'><? echo $options; ?></select>
</tr>";
Related
I need suggestion.
I have an list of position from database, displayed in table and in the last table row is an option to select "TAK" - YES and "NIE" - NO, please tell me is it possible and if it, how to send and receive it all selected to "TAK" fields at file wypozyczalnia.php
<form action='wypozyczalnia.php' method='post'>
<table width="1000px" align="center" border="1">
<tr>
<th width="250px">Tytuł</th>
<th width="250px">Autor</th>
<th width="300px">Opis</th>
<th width="200px">Dostępność</th>
</tr>
<?php
//sprawdzanie czy taka zmienna jest ustawiona w sesji, jest to dla nowo wchodzących na stronę
if(isset($_SESSION['blad'])) echo $_SESSION['blad'];
//wyświetlanie wyników gdy zmienna ile zwróciła przynajmniej 1 rząd
if ($ile>=1)
{
//pętle wyświetlająca wszystkie zwrócone z zapytania wpisy
for ($i = 1; $i <= $ile; $i++)
{
//pobranie rzędu jako tablicę asocjacyjną
$row = mysqli_fetch_assoc($rezultat);
//przypisanie każdej kolumny do odpowiedniej zmiennej
$title = $row['title'];
$author = $row['author'];
$description = $row['description'];
$available = $row['available'];
echo
"<tr>
<td> ".$title." </td>
<td> ".$author." </td>
<td> ".$description." </td>
<td>";
if($available==1)
{
echo "TAK";
}
else
{
echo "NIE";
}
"</td>
</tr>";
if(isset($_SESSION['zalogowany']) && ($_SESSION['zalogowany']==true) && $available==1)
{
echo
"<select name='idksiazki[]' >
<option name='NIE'>NIE</option>
<option name='TAK'>TAK</option>
</select>";
}
}
}?>
</table>
<?php
if(isset($_SESSION['zalogowany']) && ($_SESSION['zalogowany']==true))
{
echo "<br /> <input type='submit' name='zapis' value='wypożyczam'></form>";
}
?>
</form>
At the moment you have multiple forms - one per table row, so each <select is inside its own form. You can't submit multiple forms at once from a HTML page. Also your existing forms seem to be missing the </form> tag so they will be invalid anyway.
Therefore, to be able to submit all the <select values at the same time, you need a single form, which has the entire table contained within it.
For example:
<form action='wypozyczalnia.php' method='post'>
<table>
<?php
if ($ile>=1)
{
//pętle wyświetlająca wszystkie zwrócone z zapytania wpisy
for ($i = 1; $i <= $ile; $i++)
{
//pobranie rzędu jako tablicę asocjacyjną
$row = mysqli_fetch_assoc($rezultat);
//przypisanie każdej kolumny do odpowiedniej zmiennej
$title = $row['title'];
$author = $row['author'];
$description = $row['description'];
$available = $row['available'];
echo
"<tr>
<th> ".$title." </th>
<th> ".$author." </th>
<th> ".$description." </th>
<th>";
if($available==1)
{
echo "TAK";
}
else
{
echo "NIE";
}
"</th>";
if(isset($_SESSION['zalogowany']) && ($_SESSION['zalogowany']==true) && $available==1)
{
echo "<th>
<select name='idksiazki[]' >
<option value=$i;>NIE</option>
<option value=$i;>TAK</option>
</select>
</th>";
}
echo "</tr>";
}
}
?>
</table>
</form>
P.S. A note about HTML semantics: <th> is mean to indicate a table heading, e.g. the title of a column, or the main heading within a row. It should not be used for every cell in your table. <td> is for normal table cells. If you wish to make the text bold, use CSS instead.
I have the following code:
$sql = "SELECT * FROM Tickets WHERE stat='Open'";
$result = mysql_query($sql);
mysql_close($con);
?>
<!DOCTYPE>
<html>
<body>
<table class="striped">
<tr class="header">
<td>Username</td>
<td>Title</td>
<td>Description</td>
<td>Admin Name</td>
<td>Category</td>
<td>Status</td>
<td>Urgency</td>
<td>Time In</td>
<td> </td>
</tr>
<?php
while ($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>".$row[username]."</td>";
echo "<td>".$row[title]."</td>";
echo "<td>".$row[description]."</td>";?>
<td><select>
<?php
echo "<option value'".$row[admin_name]."'>".$row[admin_name]."</option>";
$sql = mysql_query("SELECT username FROM Users WHERE user_type='admin'");
while ($u = mysql_fetch_array($sql)){
echo "<option value='".$u['username']."'>".$u['username']."</option>";
}
?>
</select></td>
<?php
echo "<td>".$row[category]."</td>";
echo "<td>".$row[stat]."</td>";
echo "<td>".$row[urgency]."</td>";
echo "<td>".$row[time_in]."</td>";
echo "<td><a href='close.php'>Close Ticket</a></td>";
echo "</tr>";
}
?>
</table>
<a href='update.php'>Update</a>
</body>
</html>
I have two links on this page. Both of them need to update a SQL database. The Close ticket link needs to just update the single row, while the update link should update all of them. I am not sure how to get the info from one php to the next. It seems like you can put the individual row information into a Post array for the close ticket link, but I am not sure how. For the update link it needs to take the value of the dropdown in the table and change the admin_name field to that value.
<?php
require 'database.php';
$query = "SELECT id, date, ponumber FROM so";
$result = $mysqli->query($query) or die(mysqli_error($mysqli));
if ($result) {
echo "<form method='post' action='delete.php'>";
echo "<table cellspacing='0' cellpadding='15' border='1'>
<th >DELETE</th>
<th >VIEW</th>
<th >ID</th>
<th >DATE</th>
<th >PO NUMBER</th>";
while ($row = $result->fetch_object()) {
$date = $row->date ;
$ponumber = $row->ponumber;
$id = $row->id;
//put each record into a new table row with a checkbox
echo "<tr>
<td>
<input type='checkbox' name='checkbox[]' id='checkbox[]' value=$id />
</td>
<td>
$id
</td>
<td>
view
</td>
<td>
$date
</td>
<td>
$ponumber
</td>
</tr>";
}
echo "</table><p><input id='delete' type='submit' class='button' name='delete'
value='Delete Selected Items'/></p></form>";}
?>
i have a sort of an online order form which enable the sales rep to input sales order,
i have done the insert and delete using the code above now i want every row to be a hyperlink so that when they click view it will display only row that has been clicked, in my code above if you click :view" all the detail will display, how can i display only the row that i will click will display the detail of the record!
you need to pass the id in the url and you need to read it if it's there.
e.g.
<?php
require 'database.php';
$query = "SELECT id, date, ponumber FROM so";
/* Edit 1 */
if (!empty($_GET['id'])) {
$query .= " WHERE id = " . mysql_real_escape_string($_GET['id']);
}
/* Edit 1 end */
$result = $mysqli->query($query) or die(mysqli_error($mysqli));
if($result) {
echo "<form method='post' action='delete.php'>";
echo "<table cellspacing='0' cellpadding='15' border='1'>
<th >DELETE</th><th >VIEW</th><th >ID</th><th >DATE</th><th >PO NUMBER</th>";
while ($row = $result->fetch_object()) {
$date = $row->date ;
$ponumber = $row->ponumber;
$id = $row->id;
//put each record into a new table row with a checkbox
echo "<tr>
<td><input type='checkbox' name='checkbox[]' id='checkbox[]' value=$id /></td>
<td>$id</td>
<td>";
/* Edit 2 */
echo "<a href='view.php?id=$id'>view</a>";
/* Edit 2 End */
echo "</td>
<td>$date</td>
<td>$ponumber</td></tr>";
}
echo "</table><p><input id='delete' type='submit' class='button' name='delete' value='Delete Selected Items'/></p></form>";}
?>
A style suggestion:
Don't do/stop doing this:
echo "<form method='post' action='delete.php'>";
echo ...
while
Where what you are echoing is a static string. Instead, do:
?>
<form method='post' action='delete.php'>
...
<?php
while
it's simply easier to read and maintain.
<?php
session_start();
include '../dbconnect_form_fields.php';
$res = mysql_query("SELECT * FROM form_fields") or die(mysql_error());
echo "<form id='list' action='form_calc.php' method='post'>
<table width='100%' border='1'>
<tr><td><select>";
while($row = mysql_fetch_assoc($res)){
echo "<option value='".$row['id']."'>".$row['field']." ".$row['price']."</option>";
}
echo "</select>
</td></tr>
<tr><td><select>";
$res1 = mysql_query("SELECT * FROM form_fields") or die(mysql_error());
while($row1 = mysql_fetch_assoc($res1)){
echo "<option value='".$row1['id']."'>".$row1['field']." ".$row1['price']."</option>";
}
?>
For some reason, when i change that line that says $res1 = mysql_query blah blah blah, It doesnt seem to work, the select field is empty with no options. It seems as though I would have to define $res as a mysql_fetch and for a second select box, access the DB a second time but using a different variable...
How can I make the $res variable carry across the loops without having to access the DB so many times... I play to have six of these loops... Help me gurus!
I think you might need to clarify what your intentions are. From your code you are creating two of the same select elements. If you want to output the same select element twice, you could try
$res = mysql_query("SELECT * FROM form_fields") or die(mysql_error());
echo "<form id='list' action='form_calc.php' method='post'>
<table width='100%' border='1'>
<tr>
<?php for($i=0;$i<1;$i++) { //Loop twice to create a second select element ?>
<td><select>";
while($row = mysql_fetch_assoc($res)){
echo "<option value='".$row['id']."'>".$row['field']." ".$row['price']."</option>";
}
echo "</select>
</td>
<?php } //End of for loop ?>
</tr>
Alternatively, you could just copy/paste the same while loop twice without changing any variables
<form id='list' action='form_calc.php' method='post'>
<table width='100%' border='1'>
<tr>
<td><select>
<?php
while($row = mysql_fetch_assoc($res)){
echo "<option value='".$row['id']."'>".$row['field']." ".$row['price']."</option>";
}
?>
</select>
</td>
<td><select>
<?php
while($row = mysql_fetch_assoc($res)){
echo "<option value='".$row['id']."'>".$row['field']." ".$row['price']."</option>";
}
?>
</select>
</td>
</tr>
</table>
The code below isn't working as intended. I was wanting 3 columns with the first column being a site name, the second column being the category, and the third being the URL for RSS (haven't gotten to that part yet). The data is in the database but when I view the result of the statements below, the site name results link to the current web page I am on. I would greatly appreciate any assistance with getting each column of data to display on the page correctly. Thank you
<?php
$query="SELECT * FROM SOMETABLE";
$result=mysql_query($query);
$num = mysql_numrows($result);
echo "
<table border='1'>
<th>Site Name:</th>
<th>Category:</th>
<th>RSS:</th>";
$i=0;
while ($i < $num) {
$siteName =mysql_result($result,$i,"siteName");
$category =mysql_result($result,$i,"category");
$category =mysql_result($result,$i,"url");
$rss =mysql_result($result,$i,"rss");
echo "
<tr>
<td><a href='$url'>$siteName</a></td>
<td>$category</td>
<td>$rss</td>
</tr>";
$i++;
}
?>
echo "<table>";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<tr>";
echo "<td>$line ["siteName"]</td>";
echo "<td>$line ["category"]</td>";
echo "<td>$line ["url"]</td>";
echo "<td>$line ["rss"]</td>";
echo "</tr>";
}
echo "</table>";
skip $num, mysql_resuls ( the way how you do it ) open row TR in headers ( I skipped it ) and close table
echo "
<table border='1'>
<th>Site Name:</th>
<th>Category:</th>
<th>RSS:</th>";
Should be:
echo "
<table border='1'>
<tr>
<th>Site Name:</th>
<th>Category:</th>
<th>RSS:</th>
</tr>";
To start with.
Or just use some tool like SDTable.com or jqGrid and let them do all job for you =)