Reading and Overwriting radio button values in mysql database - php

I'm describing the correcting functioning of the process.
First the user posts an order, and the status initially is set to NOT APPROVED, but his manager is able to see the order request and can change the status to APPROVED, and then it is forwarded to the IT guy and he can change status to UNDER PROCUREMENT.
I am using radio buttons for gm and IT guy for changing the status. However, i am unable to change the status. Here's the code for better understanding
echo'<th>Product</th><th>Type</th><th>Quantity</th><th>Order Status</th>';
while ($row = mysqli_fetch_array($result)){
$type=$row["type"];
$make=$row["make"];
$quantity=$row["quantity"];
echo "<tr>";
echo "<td>" . $row['type'] . "</td>";
echo "<td>" . $row['make'] . "</td>";
echo "<td>" . $row['quantity'] . "</td>";
echo '<form method="post">';
//Radio buttons used here
echo '<td><input type="radio" name="status[' . $row['order_id'] . ']" value="not approved" checked >Not Approved<br>
<input type="radio" name="status[' . $row['order_id'] . ']" value="approved">Approved</td>';
echo '</form>';
$_SESSION['radio'] = $_POST['status[]'];
$qr = "UPDATE order_info SET status = '".$_SESSION['radio']."' WHERE username = '".$_SESSION['username']."'";
$rs = mysqli_query($con3,$qr) or die(mysqli_error($con3));
}
echo "</table>";
Also, I want the radio button to be checked by default according to the status of the order.

The problem is with your input names:
echo '<td><cb><input type="radio" name="status[' . $row['order_id'] . ']" value="not approved" checked >Not Approved<br>
<input type="radio" name="status[' . $row['order_id'] . ']" value="approved">Approved</cb></td>';
echo '</form>';
You're creating an array. You can see this with this example:
<form method ='post'>
<input type='radio' name='status["25"]' value='not approved'>Not Approved
<input type='radio' name='status[25]' value='approved'>Approved
<input type='submit' value='Submit'>
</form>
<pre>
<?
session_start();
print_r($_POST);
$_SESSION['radio'] = $_POST['status'];
print_r($_SESSION);
?>
</pre>
This may have been to keep the order_id, but your update query is not specifying an order_id and will update all rows for that username. If you're only updating one row at a time, you can pass the order id with a hidden field (if you even need it?):
echo '<input type="hidden" name="order_id" value="'.$row['order_id'].'">';
Which means you're probably missing an "and order_id =" qualifier in your update.

Related

PHP Update always update empty string

let me explain the code I'm getting data from the db and display the data in text input then when the user make changes I get the user input and update the table however when it update it update to empty data
<?php
echo "<form action='' method='get'>";
echo "<td class='data'><input type='product_name' id='product_name' value=" . $row['product_name'] . "> </td>";
echo "<td class='data'><input type='products_price' id='products_price' value=" . $row['products_price'] . "> </td>";
echo "<td class='data'><input type='products_desc' id='products_desc' value=" . $row['products_desc'] . "> </td>";
echo "</form>";
echo "</tr>";
echo "</table>";
echo "<form action='' method='POST'>
<input name='update' type='submit' value='Update' style='margin-left: 720px'>
</form>";
$product_name = isset($_GET['product_name']) ? $_GET['product_name'] : '';
$products_desc = isset($_GET['products_desc']) ? $_GET['products_desc'] : '';
$products_price = isset($_GET['products_price']) ? $_GET['products_price'] : '';
if (isset($_POST["update"])) {
$sql1 = "UPDATE tbl_products SET products_desc='" . $products_desc . "',product_name='" . $product_name . "',products_price='" . $products_price . "' WHERE products_id='" . $product_id . "'";
mysqli_query($conn, $sql1) or die(mysqli_error($conn));
echo "yess";
}
?>
Your input elements have no name attribute, so things like $_GET['product_name'] will always be empty. The name attribute is the key in the key/value pair sent to the server.
Additionally, the type attributes are all broken. (Though I suspect the browser is automatically "correcting" that by defaulting to a text input.)
Add the name attribute (and fix type):
<input type='text' name='product_name' id='product_name' ...
Additionally, you have two forms. So when you click your button, that form doesn't submit any of the inputs. Because they're in a different form.
Put them all into the same form, and decide whether you want to use GET or POST (since your server-side code is going to need to know that).
You defined your data form in one form tag and submit form in the another form tag:
<form action='' method='POST'>
<input name='update' type='submit' value='Update' style='margin-left: 720px'>
</form>
echo "<form action='' method='get'>";
echo "<td class='data'><input type='product_name' id='product_name' value=" . $row['product_name'] . "> </td>";
echo "<td class='data'><input type='products_price' id='products_price' value=" . $row['products_price'] . "> </td>";
echo "<td class='data'><input type='products_desc' id='products_desc' value=" . $row['products_desc'] . "> </td>";
echo "</form>";
When you submit your first form but your data is on another form
this is what is wrong

Php array looping, creating dynamic variable for purchasing form

Still getting used to stackoverflow excuse my rookie-ness.. :)
Have an SQL query that returns a data put into a table in php. I want this table to be used for purchasing.
My idea was to use the product id, meaning i would use a dynamic php variable (not sure if I'm doing that right now) believe I saw a post something like $.$varaible$.$ it wasn't very clear and was a different subject.
My code is as follows:
$result = mysqli_query($con, "SELECT * FROM Product WHERE Type = 'Game'");
?>
<div class="wrapper">
<h1 class="headGame">Buy Some Games Man</h1>
</div>
<br />
<div class="wrapper">
<?php
echo
"<table border='1'>
<tr>
<th> Name </th>
<th> Picture </th>
<th> Console </th>
<th> Description </th>
<th> Price </th>
<th> Amount </th>
</tr>";
echo '<form id="gamesOrder" action="purchase.php">';
while($row = mysqli_fetch_array($result)) {
$id = $row['Pd_Key'];
echo"<tr>";
echo"<td>" . $row['Name'] . "</td>";
echo"<td>" . '<img class="prdPic" src="'. $row['Picture']. '">' . "</td>";
echo"<td>" . $row['Type2'] . "</td>";
echo"<td>" . $row['Description'] . "</td>";
echo"<td>" . $row['Price'] . "</td>";
echo"<td>" . '<input type="number" min="0" max="100"; name="'.$id.'" value=0>' . "</td>";
echo"</tr>";
}
echo '<input type="submit" value=" BUY ">';
echo '</form>';
?>
When I click the submit it changes the url but nothing happens, it doesn't redirect.
Any advice on how to get this entire process to work. The variable being used in a purchasing form, via a php file ie (purchase.php) and the variable to use for this.
EDIT - Had minor errors, but still not 100% on variable %id, won't that get redefined each loop, how can I have it dynamic so it can be used in a form to identify what the user wants to buy.
Now redirects but not to purchase.php
URL is ~/purchase.php?1=0&2=0&3=0&4=0&5=0&6=0&7=0&8=0&9=0&10=0&11=0&12=0&13=0&14=0&15=0&16=0&17=0&18=0&19=0&20=0&21=0
Thanks you legends you!! =D
You are missing the closing form caret:
echo '<form id="gamesOrder" action"purchase.php"';
should be:
echo '<form id="gamesOrder" action="purchase.php">';
Also you concatenation for ID is incorrect:
echo"<td>" . '<input type="number" min="0" max="100"; name=".$id." value=0>' . "</td>";
should just be:
echo"<td>" . '<input type="number" min="0" max="100"; name="id" value="' .$id. '" value=0></td>';
To access the id in purchase.php use the following code:
$id = isset($_GET['id']) ? $_GET['id'] : null;
And you need to assign the action to the form with an equals sign:
action="myaction.php"
And you don't pass the id right...
echo"<td>" . '<input type="number" min="0" max="100"; name=".$id." value=0>' . "</td>";
should be
echo"<td>" . '<input type="number" min="0" max="100"; name="'.$id.'" value=0>' . "</td>";
Ough, and on form you need to put action =
echo '<form id="gamesOrder" action="purchase.php">';
Ok, first you've got some HTML error, lets see:
In the Form element you need to close it and also include the method (as POST), see:
< form id="gamesOrder" action="purchase.php" method="POST">
To send data using a form you will need to include the data from the data base in form fields like this:
echo '< input type="text" name="myFieldName" value="'.$row['Price'].'">';
Any question let me know...
Cheers.

How do i integrate a form with a table?

Here is my current TABLE:
print "<table cellpadding=12px>";
print "<th>Name</ th>";
print "<th>Price</ th>";
print "<th>Image</ th>";
if ($cat == 'Clothes'){
$query = "SELECT * FROM Products WHERE Type = 'Clothes' ORDER BY ProductPrice";
$result = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($result))
{
echo " <tr>";
echo "<td>" . $row['ProductName'] . "</td>";
echo "<td>" . '&pound'. $row['ProductPrice'] . "</td>";
echo "<td>" . '<img width=200px height=200px src="./ProductsPic/' . $row['ProductPic'] . '" />' . "</td>";
echo "<td> Add to Cart</td>";
echo "</tr>";
}
print "</table>";
However, when i click on the "add to cart" link, it only sends the ID of the product to the cart. I somehow need to send the price and name of that product (possibly hidden data?). So would it be easy to integrate a form into my table, or would there be another way i can add the name and price of the product into my cart array?
If the ID field is the unique key that represents the product, then you can simply fetch its other details from the database. Otherwise you can do the following:
You could add more parameters to the query string.
echo "<td> Add to Cart</td>";
You could create a form with hidden values, and convert the Add to Cart link to a submit button.
echo '
<form action="cart.php">
<input type="hidden" name="price" value="'.$row['ProductPrice'].'">
<input type="hidden" name="name" value="'.$row['ProductName'].'">
<input type="hidden" name="id" value="'.$row['ID'].'">
<button type="submit">Add to cart</button>
</form>
';

Delete button for each table row

I manage to succesfully read and display data from my database with the following code:
http://pastebin.com/rjZfBWZX
I also generate a delete button for each row of the table :) Clicking the delete button calls "obrisi.php" which is supposed to delete that row but I messed something up :S Here's obrisi.php code:
http://pastebin.com/mrFy1i7S
I'm getting a Parse error: syntax error, unexpected 'FROM' (T_STRING) :S
Let's try and do it with _GET instead of _POST.
In your main code you need to change line 39 (the input) from:
echo "<td>" . " <input type='submit' id= '$id' . ' value='Delete' >" . "</td>";
to:
echo "<td><a href='obrisi.php?id=$id'>Delete</a></td>";
In your obrisi.php change line 3 (the id setup) from:
$id = $_POST['id'];
to:
$id = $_GET['id'];
And finally as a nice addition, redirect back to the main page by adding the following line at the end of the obrisi.php file before the closing of the php tag.
header('location:index.php');
Where index.php the name of the main page you have.
echo "<td>" . " <input type='submit' id= '$id' . ' value='Delete' >" . "</td>";
some simple errors here. this would output (with $id = 1):
<td><input type='submit' id= '1' . ' value='Delete' ></td>
this line should be corrected to
echo '<td><input type="submit" id="' . $id . '" value="Delete" ></td>';
this is also going wrong.
echo "<form action="obrisi.php" method="post">";
should be like:
echo '<form action="obrisi.php" method="post">';
But the main problem is that there is no field id given in the post. The id of a html element is not sent on submit. it is basically to identify that element in the HTML structure.
And when using a submit button you will have to limit the scope of the form to that row and use a hidden input field, or use a link like thanpa suggests
to clarify: if you want to do it with a post (but i would sugget using the $_GET)
while ($row = mysqli_fetch_array($result) )
{
$id = $row['id'];
echo "<tr>";
echo '<form action="obrisi.php" method="post">';
echo "<td>" . $row['Ime'] . "</td>";
echo "<td>" . $row['Prezime'] . "</td>";
echo "<td>" . $row['Grad'] . "</td>";
echo "<td>" . $row['Drzava'] . "</td>";
echo "<td>" . $row['Obavijesti'] . "</td>";
echo "<td>" . $row['Tekst'] . "</td>";
echo "<td>"
echo '<td><input type="hidden" name="id" value="' . $id . '"/><input type="submit" value="Delete" ></td>';
echo "</form>
echo "</tr>";
}
and remove the echo's for the form from start and end of script.
as an additional note here, if this is going to be at some point being used in a live system you need to be checking $id in obrisi.php that it is actually an ID and not something nasty and unexpected like more sql, look up sql injection.

dynamically set value for a radio button with php

Hello Coders,
I have a dynamically created table in PHP and for each row there is a radio button. The intention is that depending upon which radio box is selected, the user can perform multiple functions like, Delete, Amend etc. I can make this all work with hard coded "Value" for the radio button like
echo "<td>" . '<input type="radio" name="radioSelect" value="2" checked="checked" />' . "</td>";
What I am looking for is a way to set the "2" dynamically. For example:
while($row = mysql_fetch_array($result) and $i<=100)
{
echo "<tr>";
$sno= $row['SNo'];
echo "<td>" . '<input type="radio" name="radioSelect" value= $sno checked="checked" />' . "</td>";
}
What is the syntax for this? Is this possible at all?
Thanks for your help.
Yes, it's called concatenation, and you're already doing it with the . operator after "<td>", for example.
echo "<td>" . '<input type="radio" name="radioSelect" value="' . $sno . '" checked="checked" />' . "</td>";
I'm not sure why you put it that way, though.
echo '<td><input type="radio" name="radioSelect" value="' . $sno . '" checked="checked" /></td>';
Also, note that only one radio button with the same name can be checked at a time, so you might want to rethink what you're doing there.
You can do like this ? if you are having a problem in CONCATENATION.
<?php
while($row = mysql_fetch_assoc($result) and $i<=100)
{ $sno= $row['SNo'];?>
<table>
<tr>
<td><input type="radio" name="radioSelect" value= <?php echo $sno; ?> checked="checked" /> <?php } ?></td>
</tr>
</table>

Categories