I am trying to display options in select tag from another table while my
first, while loop continues, this is what I have tried so far !!
<?php
$query="SELECT * FROM `customerdata` WHERE takenby='$_SESSION[username]'";
$query_two="SELECT * FROM `vendordriver` WHERE vendoremail='$_SESSION[username]'";
$run_two=$db->query($query_two);
$run=$db->query($query);
while ($row=$run->fetch_assoc()) {
echo " <tr><td>$row[bookingid]</td>
<td>$row[drivername]</td>
<td>$row[cabtype]</td>
<td>$row[carnumber]</td><td><select>";
while ($row_two=$run_two->fetch_assoc()) {
echo "<option>$row_two[drivername]</option>";
}
echo" </select></td>
<td><input type='submit' class='btn btn-success' value='SEND '>
</td>";
}
?>
Any Suggestions Please !!!
You can try this code
<?php
$table_1 = null;
$username = $_SESSION['username'];
$query="SELECT * FROM customerdata WHERE takenby='$username'";
$run=$db->query($query);
if ($result->num_rows > 0) {
while ($row=$run->fetch_assoc()) {
$table_1 = "<tr><td>$row[bookingid]</td>";
$table_1 .= "<td>$row[drivername]</td>";
$table_1 .= "<td>$row[cabtype]</td>";
$table_1 .= "<td>$row[carnumber]</td><td><select>";
echo $table_1;
$query_two="SELECT * FROM vendordriver WHERE vendoremail='$username'";
$run_two=$db->query($query_two);
while ($row_two=$run_two->fetch_assoc()) {
echo "<option>$row_two[drivername]</option>";
}
echo" </select></td>
<td><input type='submit' class='btn btn-success' value='SEND '>
</td>";
}
}
?>
Define the result from the second query as an array variable then with in the first query loop with in that array to echo the drivers names here you execute second query before the first one
$query="SELECT * FROM `customerdata` WHERE takenby='$_SESSION[username]'";
$query_two="SELECT * FROM `vendordriver` WHERE vendoremail='$_SESSION[username]'";
$if(!isset($array_option)){$array_option=array();}
$run_two=$db->query($query_two);
$run=$db->query($query);
while ($row_two=$run_two->fetch_assoc()) {
array_push($array_option,$row_two[driver_name];
}
Then within looping through first query do this
while ($row=$run->fetch_assoc()) {
echo " <tr><td>$row[bookingid]</td>
<td>$row[drivername]</td>
<td>$row[cabtype]</td>
<td>$row[carnumber]</td><td><select>";
for($i=0;$i<count($array_option);$i++){
echo "<option>$array_option[$i]</option>"
}
Then the rest of the code
echo" </select></td><td><input type='submit' class='btn btn-success' value='SEND '></td>";
}
?>
Related
I have a script that displays all the values in a db table. I'd like to be able to update the price value of each row individually on clicking the update button but as it's in a while loop it's only updating the final value successfully. I realise this probably requires foreach but I can't figure where to code it within the loop to send the data from each row to the processing script on the next page. Can anyone help? Thank you!
Display script
<h2>Boiler Prices</h2>
<?php
$sql = "SELECT * FROM emb_prices_boilers ORDER BY id ASC";
$result = $connection->query($sql);
if ($result->num_rows > 0) {
/* Start Form and Table ---------------------------------------------------------*/
echo "<form action='../admin/actions/set-boiler-price.php' method='post'>";
echo "<table id='boiler-prices'>
<tr>
<th>ID</th>
<th>Type</th>
<th>Boiler</th>
<th>Ref</th>
<th>Price</th>
</tr>";
/* Start Loop and Variables --------------------------------------------*/
while($row = $result->fetch_assoc()) {
$id = $row["id"];
$type = $row["type"];
$manufacturer = $row["manufacturer"];
$model = $row["model"];
$boilerref = $row["boilerref"];
$price = $row["price"];
// output data of each row
echo "
<tr>
<td>" .$id. "</td>
<td>" .$type. "</td>
<td>" .$manufacturer. " " .$model. "</td>
<td>" .$boilerref. "</td>
<td>£<input type='text' name='price' value='" .$price. "' /><input type='hidden' name='boilerref' value='" .$boilerref. "' /><input type='submit' value='update' /></td>
</tr>";
}
/* End Loop -------------------------------------------------------*/
echo "</table>";
echo "</form>";
/* End Table and Form -------------------------------------------------------*/
}
else {
echo "0 results";
}
?>
Processing script
<?php
$boilerref = $_POST['boilerref'];
$price = $_POST['price'];
if ($setboilerprice = mysqli_prepare($connection, "UPDATE emb_prices_boilers SET price = ? WHERE boilerref = ?")
) {
/* bind parameters for markers */
mysqli_stmt_bind_param($setboilerprice, "ds", $price, $boilerref );
/* execute query */
mysqli_stmt_execute($setboilerprice);
/* close statement */
mysqli_stmt_close($setboilerprice);
}
else {
echo "error: Failed to write to db";
}
?>
What you need is to create a separate form for each record. Remove <form>/</form> tags from the beginning and the end of <table> and use this code in your last td:
echo "
...
<td>
<form action='../admin/actions/set-boiler-price.php' method='post'>
£<input type='text' name='price' value='" .$price. "' />
<input type='hidden' name='boilerref' value='" .$boilerref. "' />
<input type='submit' value='update' />
</form>
</td>
</tr>";
With such mark up each form will send it's own data to your set-boiler-price.php and updating will work as you expect.
I have written the function for updating my status from 0 to 1 but when I am clicking on button its updating the status of all the rows. I don't know what I did wrong.
function updateStatusenable($id,$value)
{
global $mysqli,$db_table_prefix;
$stmt = $mysqli->prepare("UPDATE gb_users
SET status = ?
WHERE
id = ?
LIMIT 1
");
$stmt->bind_param("ss", $id,$value);
$result = $stmt->execute();
$stmt->close();
return $result;
}
And this function I used in:
<?php
$value=1;
foreach($users as $v1)
{
echo "<tr>
<form action=\"admin_pending_approval_users.php\" method=\"post\">
<input type=\"hidden\" name=\"enable\" value=".$v1['id'].">
<td>".$v1['id']."</td>
<td>".getAccountTypeNameById($v1['account_type'])."</td>
<td>".$v1['name']."</td>
<td>".$v1['user_name']."</td>
<td>".getUserGroupsNameById($v1['group'])."</td>
<td>Rs.".fetchUserBalance($v1['id'])."</td>
<td>";
if($v1['status']==1)
{
echo "<button class=\"green tiny\"><span>ACTIVE</span></button>";
}
else
{
echo "<button class=\"orange tiny\" onclick=".updateStatusenable($v1['id'],$value)." ><span>DISABLED</span></button>";
}
echo "</td>
<td align='right'>
<img src=\"images/icons/small/white/magnifying_glass.png\"></button>
<img src=\"images/icons/small/white/create_write.png\"></button>
</td>
</form>
</tr>";
}
I think your binding order is wrong when you set it like below;
$stmt->bind_param("ss", $id,$value);
instead use
$stmt->bind_param("ss",$value, $id);
as u set status before id
Also use ajax to update so you can send requests for your actions from client to server.
As i dont want to use Ajax I have done some changes like and make it done in PHP.
<?php
$value=1;
foreach($users as $v1)
{
echo "<tr>
<form action=\"admin_pending_approval_users.php\" method=\"post\">
<input type=\"hidden\" name=\"id\" value=".$v1['id'].">
<td>".$v1['id']."</td>
<td>".getAccountTypeNameById($v1['account_type'])."</td>
<td>".$v1['name']."</td>
<td>".$v1['user_name']."</td>
<td>".getUserGroupsNameById($v1['group'])."</td>
<td>Rs.".fetchUserBalance($v1['id'])."</td>
<input type=\"hidden\" name=\"process\" value=\"1\" required>
<td>";
if($v1['status']==1)
{
echo "<button class=\"green tiny\"><span>ACTIVE</span></button>";
}
else
{
echo "<button class=\"orange tiny\" onClick=\"form.submit()\" ><span>DISABLED</span></button>";
}
echo "</td>
<td align='right'>
<img src=\"images/icons/small/white/magnifying_glass.png\"></button>
<img src=\"images/icons/small/white/create_write.png\"></button>
</td>
</form>
</tr>";
}
?>
And in the same php page i added one function as
if(!empty($_POST['process']))
{
$id = $_POST['id'];
updateStatusenable($id,1);
}
And in the end the funcs its remain same
I've got a page where I render a table of values from Mysql using PHP and HTML.
I want to delete rows checked with a checkbox upon clicking a delete button. Unfortunately, nothing happens when clicking the button and I'm at a total loss as to where to look.
Code is below (database connection has been omitted, but it works).
<body>
<?php
$delete = $_POST['checkbox'];
$query = "SELECT * FROM Cards";
$result = mysql_query($query);
echo "<table>";
echo "<tr><td>Name</td><td>Quantity</td><td>Color</td><td>Type</td></tr>";
while ($row = mysql_fetch_array($result)){
echo "<tr><td><input type='checkbox' name='checkbox[]" . $row['id'] . "]'></td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Quantity'] . "</td>";
echo "<td>" . $row['Color'] . "</td>";
echo "<td>" . $row['CardType'] . "</td></tr>";
}
mysql_free_result($result);
echo "</table>";
?>
</body>
<tr>
<td colspan="5" align="center"><input name="delete" type="SUBMIT" id="delete" value="delete" action="POST"></td>
</tr>
<?php
if (isset($_POST['delete'])) {
$checkbox = $_POST['checkbox'];
$count = count($checkbox);
for($i = 0; $i < $count; $i++) {
$id = (int) $checkbox[$i]; // Parse your value to integer
if ($id > 0) { // and check if it's bigger then 0
mysql_query("DELETE FROM Cards WHERE id = $id");
}
}
}
?>
<?php include "../footer.htm";?>
Try this:
<body>
<?php
$connect=mysqli_connect("Host","Username","Password","Database"); /* REPLACE NECESSARY DATA */
if(mysqli_connect_errno()){
echo "Error".mysqli_connect_error();
}
if (isset($_POST['delete'])){
$checkbox = $_POST['checkbox'];
$count = count($checkbox);
for($i=0;$i<$count;$i++){
if(!empty($checkbox[$i])){ /* CHECK IF CHECKBOX IS CLICKED OR NOT */
$id = mysqli_real_escape_string($connect,$checkbox[$i]); /* ESCAPE STRINGS */
mysqli_query($connect,"DELETE FROM Cards WHERE id = '$id'"); /* EXECUTE QUERY AND USE ' ' (apostrophe) IN YOUR VARIABLE */
} /* END OF IF NOT EMPTY CHECKBOX */
} /* END OF FOR LOOP */
} /* END OF ISSET DELETE */
$query = "SELECT * FROM Cards"; /* SELECT FROM Cards TABLE */
$result = mysqli_query($connect,$query); /* EXECUTE QUERY */
echo "<form action='' method='POST'>"; /* SUBMIT PAGE ON ITSELF */
echo "<table>";
echo "<tr><td>Name</td><td>Quantity</td><td>Color</td><td>Type</td></tr>";
while ($row = mysqli_fetch_array($result)){ /* FETCH ARRAY */
$id=mysqli_real_escape_string($connect,$row['id']);
echo "<tr><td><input type='checkbox' name='checkbox[]' value='$id'></td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Quantity'] . "</td>";
echo "<td>" . $row['Color'] . "</td>";
echo "<td>" . $row['CardType'] . "</td></tr>";
}
mysqli_free_result($result);
echo "</table>";
?>
<tr>
<td colspan="5" align="center"><input name="delete" type="SUBMIT" id="delete" value="delete" action="POST"></td>
</tr>
</form>
<?php include "../footer.htm";?>
</body>
I've converted your code to MySQLi from the deprecated MySQL.
And used empty() function to determine a checked checkbox.
You forgot to put a <form> function for your html.
And also forgot to use value tag for your checkbox.
I have put explanations quoted in /* */ inside the code I have provided.
Try this:
<body>
<?php
$delete = $_POST['checkbox'];
$query = "SELECT * FROM Cards";
$result = mysql_query($query);
echo "<form method=post action=''>";
echo "<table>";
echo "<tr><td>Name</td>
<td>Quantity</td>
<td>Color</td>
<td>Type</td></tr>";
while ($row = mysql_fetch_array($result)){
$content .= <<< END
<tr>
<td><input type="checkbox" name="checkbox" value="{$row['id']}"></td>
<td>{$row['Name']}</td>
<td>{$row['Quantity']}</td>
<td>{$row['Color']}</td>
<td>{$row['CardType']}</td>
</tr>
END;
}
echo $content;
mysql_free_result($result);
?>
<tr>
<td colspan="5" align="center"><input name="delete" type="SUBMIT" id="delete" value="delete"></td>
</tr>
</table></form>
</body>
<?php
if (isset($_POST['delete'])) {
$checkbox = $_POST['checkbox'];
foreach($checkbox as $id) {
$id = (int) $id;
mysql_query("DELETE FROM Cards WHERE id = $id");
}
}
?>
<?php include "../footer.htm";?>
I want to Create a Shoping Cart on PHp,
The Code is simple, When the customer fill the QTY and click button Add to cart, the code will save the PRoduct ID and Qty to a Cart Table. But the problem is that Form inside the Looping. And How Can I Get ID and Qty only from The button that Customer Click.
The Program look like this
And The Script Like This
<?php
if(isset($_POST[ADD]))
{
$qty = $_POST[QTY];
$harga = $_POST[HARGA_ASLI];
$id = $_POST[ID];
print_r($_POST);
}
$kolom = 3;
$sql = "SELECT *,FORMAT(harga,0)AS harga_digit FROM item";
$hasil = mysql_query($sql);
echo "<form method=POST action=index.php>";
echo "<table>
<tr>";
$i = 0;
while($data=mysql_fetch_array($hasil))
{
if($i >= $kolom)
{
echo "</tr><tr>";
$i = 0;
}
$i++;
echo "<td align='center'><br><a href='detailBarang.php?ID=$data[ID]'><img src='$data[img]' width='200' height='150'/><br>$data[nama_produk]</a><br>
Rp. $data[harga_digit]<br>
<input type='submit' name='ADD' id='ADD' value='Add to Cart'>
<input type='text' name='QTY' id='QTY' placeholder='Qty' /><br>
<input type='hidden' name='HARGA_ASLI' id='HARGA_ASLI' value='$data[harga]' /><br>
<input type='hidden' name='ID' id='ID' value='$data[ID]' />
<br></td>";
}//end of while
echo "<tr></table>";
echo "</form>";
?>
If i fill the Qty and click Add to Cart, only the last Item can Post The Data.
How To Post The data Only for Customer Choose?
Im very Appreciated Your Answer.
Thanks
First, let's convert your MySQL to MySQLi. More explanation inside the comments /* */:
<?php
$connection=mysqli_connect("YourHost","YourUsername","YourPassword","NameofYourDatabase");
if(mysqli_connect_errno()){
echo "Error".mysqli_connect_error();
}
$res=mysqli_query($con,"SELECT * FROM item");
while($row=mysqli_fetch_array($res)){
$nameofsubmitbutton=$row['ID'];
if(isset($_POST[$nameofsubmitbutton])){
$nameofproduct=$row['namaproduk'];
$nameofnumbersubmitted=$nameofsubmitbutton."number";
$quantity=$_POST[$nameofnumbersubmitted];
if(empty($quantity)){
echo "You wanted to buy a ".$nameofproduct."?<br>Type in a number so you can add it to your cart.";
}
else {
mysqli_query($connection,"INSERT INTO yourTable ('','') VALUES ('$quantity','$nameofproduct')");
echo "You bought ".$quantity." of ".$nameofproduct;
}
} /* END OF IF ISSET */
} /* END OF WHILE LOOP $RES */
$kolom = 3;
$hasil = mysqli_query($connection,"SELECT *,FORMAT(harga,0) AS harga_digit FROM item"); /* YOU SURE WITH THIS QUERY? */
echo "<form method=POST action=''>"; /* SUBMIT ON ITSELF */
echo "<table><tr>";
$i = 0; /* THIS WOULD ALSO SET AS YOUR COUNTER */
while($data=mysqli_fetch_array($hasil))
{
$id=$data['ID'];
if($i >= $kolom){
echo "</tr><tr>";
$i = 0;
} /* END OF IF $i >= $KOLOM */
$i++;
echo "<td align='center'><br><a href='detailBarang.php?ID=$data[ID]'><img src='$data[img]' width='200' height='150'/><br>".$data[nama_produk]."</a><br>Rp. ".$data[harga_digit]."<br>"; /* IF TO ECHO VARIABLES, USE ".$variable." */
$numbername=$id."number";
echo "<input type='number' name='$numbername' id='QTY' placeholder='Qty' /><br>"; /* CHANGE YOUR INPUT TYPE TO NUMBER */
/* NO NEED FOR THE HIDDEN INPUT */
echo "<input type='submit' name='$id' id='ADD' value='Add to Cart'></td>"; /* CHANGE THE NAME OF SUBMIT BUTTON TO THE CORRESPONDING ID FROM YOUR TABLE */
} /* END OF WHILE LOOP */
echo "<tr></table>";
echo "</form>";
?>
I tried it on my local computer. You should too.
Here's a sample screen shot.
This may do the trick:
<?php
$kolom = 3;
$sql = "SELECT *,FORMAT(harga,0)AS harga_digit FROM item";
$hasil = mysql_query($sql);
while($data=mysql_fetch_array($hasil))
{
if(isset($_POST['ADD'.$data[ID]]))
{
$qty = $_POST['QTY'.$data[ID]];
$harga = $_POST['HARGA_ASLI'.$data[ID]];
$id = $_POST['ID'.$data[ID]];
print_r($_POST);
}
}
echo "<form method=POST action=index.php>";
echo "<table>
<tr>";
$i = 0;
while($data=mysql_fetch_array($hasil))
{
if($i >= $kolom)
{
echo "</tr><tr>";
$i = 0;
}
$i++;
echo "<td align='center'><br><a href='detailBarang.php?ID=$data[ID]'><img src='$data[img]' width='200' height='150'/><br>$data[nama_produk]</a><br>
Rp. $data[harga_digit]<br>
<input type='submit' name='ADD'".$data[ID]." id='ADD' value='Add to Cart'>
<input type='text' name='QTY'".$data[ID]." id='QTY' placeholder='Qty' /><br>
<input type='hidden' name='HARGA_ASLI'".$data[ID]." id='HARGA_ASLI' value='$data[harga]' /><br>
<input type='hidden' name='ID' id='ID'".$data[ID]." value='$data[ID]' />
<br></td>";
}//end of while
echo "<tr></table>";
echo "</form>";
?>
this my table code
$mem=mysql_query("SELECT * FROM tbl_user_profile WHERE comp_id= $cmp");
while($result=mysql_fetch_array($mem))
{
echo"<tr>" ;
echo"<td><font color='red' size=4> ".$result['fname']." </font></td>";
echo"<td><font color='red' size=4 >".$result['emp_id']."</font></td>";
inside this table in the third column i tried while loop to show radio buttons.
$query = "SELECT * FROM tbl_house where event_id = $eid";
$hid = mysql_query($query);
while ($row = mysql_fetch_array($hid))
{
$house_id = $row['house_id'];
$house_name = $row['house_name'];
echo "<td> . <input type=\"checkbox\" name=\"q1\" value=\"$house_id\" />$house_name </td>";
}
echo "<td> <a href='house_member.php?operation=dele&id=".$result['profile_id']."'><input type='button' value='add' id='btn'></a></td>";
echo"</tr>" ;
echo"</table>";
}
its not working properly. somebody please help. i'm just a beginner in php.
waiting for a better solution.
if you want them all to be in the same column, don't put <td> around each of them, do that outside the loop. And the type of the input should be radio, not checkbox.
echo "<td>";
while ($row = mysql_fetch_array($hid)) {
$house_id = $row['house_id'];
$house_name = $row['house_name'];
echo "<input type='radio' name='q1' value='$house_id'/>$house_name ";
}
echo "</td>";
please change the <Input type="checkbox"> to <input type "radio">
and
echo "<input type='radio' name='q1' value='$house_id'/>$house_name "; this is the right way to display html code into php
Are you trying to add radio buttons in the third column or do you want to create a column for each radio??
my guess is that you remove the from within the while loop and place the starting and ending tags outside the while loop. something like
<td>
while loop
</td>
hope this helps
You have to use the following code at the place of your while loop.....
<td>
while ($row = mysql_fetch_array($hid))
{
$house_id = $row['house_id'];
$house_name = $row['house_name'];
echo "<input type='radio' name='q1' value='$house_id' />$house_name ";
}
</td>