Having a lot of trouble not uselessly accessing my DB - php

<?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>

Related

How to list out two different product's details at the same table with php

I only can list out all the data row by row but I want to list out those 2 products like this from top to bottom, not row by row within
index.php
while ($row = mysqli_fetch_array($results)) {
echo '<td><'.$row['name'].'></td>';
}
server.php
$db = mysqli_connect('localhost','root','','crud');
$results = mysqli_query($db, "SELECT * FROM info");
If I really understand, you want each product to be in a different line. If yes, you could create new rows directly inside your loop like this
while ($row = mysqli_fetch_array($results)) {
echo '<tr><td>'.$row['name'].'</td></tr>';
}
<?php
while($row = mysqli_fetch_array($results)){
$detail_row.="<td>" . $row['items'] . "</td>";
$category_row.="<td>" . $row['items'] . "</td>";
}
?>
<table>
<tr>
<td>Details</td>
<?php echo $detail_row; ?>
</tr>
<tr>
<td>Categories</td>
<?php echo $category_row; ?>
</tr>
</table>

Getting the first row from database refreshes the page continuously

I'm trying to get the first row from phpmyadmin. After trying to do it different ways, I thought I would ask. Right now, the problem is that it is refreshing the browser continuously.
i've tried to add a die, but I'm not very good at mysqli/php
$sql2="SELECT * FROM stellingen WHERE REGIOID=1;";
$records2 = mysqli_query($con, $sql2);
$row = mysqli_fetch_assoc($records2);
while ($stellingen = mysqli_data_seek($records2, 0)){
echo "<p>".$stellingen['Stelling']."</p>";
}
I expect the php to fetch the first data. In the context of a loop where the next data will nneed to come later in the page.
some more code
<div class="stellingen">
<div class="stelling-wrapper" id="btn1">
<img src="images/button.svg" alt="Show more..." class="btn" id="bton">
<p type="button" class="stelling">
<?php
$sql2="SELECT * FROM stellingen WHERE REGIOID=1;";
$records2 = mysqli_query($con, $sql2);
$row = mysqli_fetch_assoc($records2);
while ($stellingen = mysqli_data_seek($records2, 0)){
echo "<p>".$stellingen['Stelling']."</p>";
}
/*
if ($recordscheck2 > 0){
while ($stellingen = mysqli_fetch_assoc($records2)){
echo "<p>".$stellingen['Stelling']."</p>";
}
}*/
?>
</div>
<div id="p1">
<table id="tabel" border=1px class="data">
<tr>
<th>Title</th>
<th>Source</th>
<th>Weight</th>
<th>Date</th>
</tr>
<?php
$sql1="SELECT * FROM stelling WHERE stelling_iD=1;";
$records1 = mysqli_query($con, $sql1);
$recordscheck = mysqli_num_rows($records1);
if ($recordscheck > 0){
while ($stelling = mysqli_fetch_assoc($records1)){
echo "<tr>";
echo "<td>".$stelling['Title']."</td>";
echo "<td>".$stelling['Source']."</td>";
echo "<td>".$stelling['Wheight']."</td>";
echo "<td>".$stelling['Timer']."</td>";
echo "</tr>";
}
}
?>
</table>
</div>
Okey, so your using mysqli_data_seek() in a while() loop, which is wrong...
<?php
if($result = mysqli_query($con, "SELECT * FROM stellingen WHERE REGIOID=1;")) {
mysqli_data_seek($result, 0);
$row = mysqli_fetch_assoc($result);
echo "<p>{$row["Stelling"]}</p>";
}
?>
Should work for you.
[Edit] I corrected my variable names.

Updating a row of SQL with html table

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.

How can get the value of the selected cell from a table created by php?

After I printed those values by using PHP, I got confused how to get the value of a selected cell.
<div>
<php
$query = mysql_query(select * from items, $conn);
echo "<table border='1'>
<th width ='120'>itemID</th>
<th width ='120'>itemName</th>";
while(row = mysql_fetch_array($query))
{
echo "<tr>";
echo "<td>".$row['itemID']."</td>";
echo "<td>".$row['itemName']."</td>";
}
echo "</table>";
?>
</div>
seems you doing fine, iv'e just edited some typos, added tags,and changed to: "mysql_fetch_assoc" function. try this:
<div>
<?php
$query = mysql_query("select * from items", $conn);
echo "<table border='1'>
<tr>
<th width ='120'>itemID</th>
<th width ='120'>itemName</th>
</tr>";
while($row = mysql_fetch_assoc($query))
{
echo "<tr>";
echo "<td>".$row['itemID']."</td>";
echo "<td>".$row['itemName']."</td>";
echo "</tr>";
}
echo "</table>";
?>
</div>

Dropdown menu missing last row inserted

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>";

Categories