Having trouble looping through multiple rows in a SQL table and getting the info to display correctly. The closest I have got is the following:
<table border="1" cellpadding="10">
<th>Stock</th><th>Price</th><th>Shares</th><th>Value</th><th>Sell Stock</th>
<?php
$transactions = mysql_query("SELECT * FROM transactions WHERE email='$email'");
while ($rows = mysql_fetch_assoc($transactions)) {
foreach($rows as $row) {
$symbol = $row["symbol"];
$price = $row["price"];
$shares = $row["shares"];
$value = $price * $shares;
?>
<form name="sellStock" action="sell.php" method="get">
<tr>
<td><input name="symbol" type="hidden" value="<?php echo $symbol ?>"><?php echo $symbol ?></td>
<td><input name="price" type="hidden" value="<?php echo $price ?>"><?php echo $price ?></td>
<td><input name="shares" type="hidden" value="<?php echo $shares ?>"><?php echo $shares ?></td>
<td><input name="value" type="hidden" value="<?php $value ?>" /><?php $value ?></td>
<td><input name="sell" type="submit" value="Sell"></td>
</tr>
<?php
}
}
?>
</table>
The while/foreach loop goes on to display the info from the rows into a HTML table, but it displays the first character from every column as opposed to all the characters from the columns I echo to be displayed (symbol, price, and shares). Any ideas?
<table border="1" cellpadding="10">
<th>Stock</th><th>Price</th><th>Shares</th><th>Value</th><th>Sell Stock</th>
<?php
$transactions = mysql_query("SELECT * FROM transactions WHERE email='$email'");
while ($row = mysql_fetch_assoc($transactions)) {
$symbol = $row["symbol"];
$price = $row["price"];
$shares = $row["shares"];
$value = $price * $shares;
?>
<form name="sellStock" action="sell.php" method="get">
<tr>
<td><input name="symbol" type="hidden" value="<?php echo $symbol ?>"><?php echo $symbol ?></td>
<td><input name="price" type="hidden" value="<?php echo $price ?>"><?php echo $price ?></td>
<td><input name="shares" type="hidden" value="<?php echo $shares ?>"><?php echo $shares ?></td>
<td><input name="value" type="hidden" value="<?php $value ?>" /><?php $value ?></td>
<td><input name="sell" type="submit" value="Sell"></td>
</tr>
<?php
}
?>
</table>
You just had one loop too many. The while loop continues until !$row, with one row per execution, so you don't want the foreach loop you had.
Related
So I've been doing this school project and need to make it so I can edit whatever is in the table. Whenever i click on "Edit" it redirects me correctly but in the form it says there is an undefined variable eventhough that variable is used pretty much everywhere.
Here is some code of the table:
<table style='margin-left:auto ; margin-right:auto;'>
<tr>
<th>#</th>
<th>Name</th>
<th>Zeit</th>
<th>Datum</th>
<th>Titel</th>
<th>Inhalt</th>
<th>Ort</th>
</tr>
<?php
if($stmt=$db->prepare("SELECT * FROM terminkalender")) {
$stmt->execute();
$stmt->store_result();
$zeilen = $stmt->num_rows();
$stmt->close();
}else {
$zeilen = 0;
}
if($zeilen > 0) {
//nur wenn Einträge, dann ausgeben
if($stmt = $db->prepare("SELECT * FROM terminkalender ORDER BY zeit,datum DESC")) {
$stmt->execute();
$stmt->bind_result($id,$name,$zeit,$datum,$ort,$titel,$inhalt);
$stmt->store_result();
//Ausgabe starten
while($stmt->fetch()){
echo "<tr>";
?>
<td><?php echo $id ;?></td>
<td><?php echo htmlspecialchars($name) ;?></td>
<td><?php echo htmlspecialchars($datum) ;?></td>
<td><?php echo htmlspecialchars($zeit) ;?></td>
<td><?php echo htmlspecialchars($ort) ;?></td>
<td><?php echo htmlspecialchars($titel) ;?></td>
<td><?php echo htmlspecialchars($inhalt); ?></td>
<td><a href='edit.php?id=<?php echo $id;?>'>Edit</a></td>
<td><a href='delete.php?id=<?php echo $id;?>'>Delete</a></td>
<?php
echo "</tr>" ;
}
}
}
?>
</table>
and here for the edit.php file:
<?php
include("./config/connect.inc.php");
$id = $_GET['id']; // get id through get string
$result=mysqli_query($db,"SELECT * FROM terminkalender WHERE id=$id");
if(isset($_POST['update'])) {
$name=$_POST['name'];
$datum=$_POST['datum'];
$zeit=$_POST['zeit'];
$ort=$_POST['ort'];
$titel=$_POST['titel'];
$inhalt=$_POST['inhalt'];
$result = "UPDATE terminkalender
SET name='$name',
datum='$datum',
zeit='$zeit',
ort='$ort',
titel='$titel',
inhalt='$inhalt'
WHERE id=$id";
header("location: ausgabe.php");
}
?>
<form name="form" method="POST" action="edit.php">
<input type="text" name="name" value="<?php echo $name; ?>" Required>
<input type="date" name="datum" value="<?php echo $datum; ?>" Required>
<input type="time" name="zeit" value="<?php echo $zeit; ?>" Required>
<input type="text" name="ort" value="<?php echo $ort; ?>" Required>
<input type="text" name="titel" value="<?php echo $titel; ?>" Required>
<input type="text" name="inhalt" value="<?php echo $inhalt; ?>" Required>
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>">
<input type="submit" name="update" value="Update">
</form>
Ẁould be really awesome if anyone could help. Thanks is advance!
Maybe you can try using isset function of php to check if that variable contains a value or not for more details check:-https://www.stechies.com/notice-undefined-variable-in-php/
I have an edit page where a user can edit an invoice form. I need to echo the array into a table but separate each array into a new line on the table.
When i echo the value, its bunched up into one line. When i echo the value into an input field, i only get one record from the array.
Here's what it looks like in my table:
<?php
$id = $_GET['id'];
$result = mysqli_query($mysqli, "SELECT * FROM invoices WHERE id=$id");
while($res = mysqli_fetch_array($result))
{
$partnumber = $res['partnumber'];
$partdescription = $res['partdescription'];
$partprice = $res['partprice'];
$partquantity = $res['partquantity'];
}
>
And then the table:
<tr>
<td><input type="text" class="input-small" name="partnumber[]" value=<?php echo $partnumber;?>></td>
<td><input type="text" class="input-small" name="partdescription[]" value=<?php echo $partdescription;?>></td>
<td><input type="text" class="input-small" name="partprice[]" size="4" value=<?php echo $partprice;?>></td>
<td><input type="text" class="input-small" name="partquantity[]" size="4" value=<?php echo $partquantity;?>></td>
</tr>
I get this:
<tr>
<td><?php echo $partnumber;?></td>
<td><?php echo $partdescription;?></td>
<td><?php echo $partprice;?></td>
<td><?php echo $partquantity;?></td>
</tr>
I get this:
I tried the following but they both result in a blank echo:
<tr>
<td><? echo $res['partnumber']; ?></td>
</tr><tr>
<td><? echo $res['partdescription']; ?></td>
</tr><tr>
<td><? echo $res['partprice']; ?></td>
</tr><tr>
<td><? echo $res['partquantity']; ?></td>
</tr>
And
<? foreach ($res as $row) { ?>
<tr>
<td><? echo $row['partnumber']; ?></td>
<td><? echo $row['partdescription']; ?></td>
<td><? echo $row['partprice']; ?></td>
<td><? echo $row['partquantity']; ?></td>
</tr>
<?
}
?>
This is the GOAL:
Please help
EDIT***************
I will fix the security issue once i have the table working.
I tried this and the output is still on ONE line.
<?php
$id = $_GET['id'];
$result = mysqli_query($mysqli, "SELECT * FROM invoices WHERE id='".mysql_real_escape_string($id)."' ");
while($res = mysqli_fetch_array($result))
{
?>
<tr>
<td><input type="text" class="input-small" name="partnumber[]" value=<?php echo $res['partnumber'];?>></td>
<td><input type="text" class="input-small" name="partdescription[]" value=<?php echo $res['partdescription'];?>></td>
<td><input type="text" class="input-small" name="partprice[]" size="4" value=<?php echo $res['partprice'];?>></td>
<td><input type="text" class="input-small" name="partquantity[]" size="4" value=<?php echo $res['partquantity'];?>></td>
<?php
}
?>
Does this help?
Please note, its only displaying 1 record because i have multiple values in one record and them i'm separating them with a comma. Please check screenshots above. Here's the code:
$partnumber = $_POST['partnumber'];
$partnumberarray = implode( ", ", $partnumber);
$partdescription = $_POST['partdescription'];
$partdescriptionarray = implode( ", ", $partdescription);
$partprice = $_POST['partprice'];
$partpricearray = implode( ", ", $partprice);
$partquantity = $_POST['partquantity'];
$partquantityarray = implode( ", ", $partquantity);
$result = mysqli_query($mysqli, "INSERT INTO invoicespartnumber, partdescription, partprice, partquantity, login_id) VALUES('$partnumberarray', '$partdescriptionarray', '$partpricearray', '$partquantityarray', '$loginId')");
Is there anyway i can echo the values from the 1 record which is separated by a comma into multiple lines on the table?
You have to put your html inside while loop that you do over result array.
It will look like this.
<?php
$id = $_GET['id'];
$result = mysqli_query($mysqli, "SELECT * FROM invoices WHERE ID='".mysql_real_escape_string($id)."'");
while($res = mysqli_fetch_array($result)){ //close php here to easier write html. ?>
<tr>
<td><input type="text" class="input-small" name="partnumber[]" value="<?php echo $res['partnumber'];?>"></td>
<td><input type="text" class="input-small" name="partdescription[]" value="<?php echo $res['partdescription'];?>"></td>
<td><input type="text" class="input-small" name="partprice[]" size="4" value="<?php echo $res['partprice'];?>"></td>
<td><input type="text" class="input-small" name="partquantity[]" size="4" value="<?php echo $res['partquantity'];?>"></td>
</tr>
<?php //open php tag again to add end scope to while loop
}
?>
<table width = "100%">
<thead>
<tr>
// here will be all columns names in th
</tr>
</thead>
<tbody>
<?php
$id = $_GET['id'];
$result = mysqli_query($mysqli, "SELECT * FROM invoices WHERE id=$id");
while($res = mysqli_fetch_array($result))
{ ?>
<tr>
<td><?php echo $res['partnumber']; ?></td> <!-- Also use input box here -->
<td><?php echo $res['partdescription'];?><td>
<td><?php echo $res['partprice']; ?></td>
<td><?php echo $res['partquantity'];?></td>
</tr>
<?php
}
?>
I am trying to update quantity as i fill the quantity field and click on update button. It works but it shows the form fields of each item in the loop and works for the last entry update only. Something like this.
/EcommerceClient/index.php?page=cart&action=update&id=3&name=%09%0D%0ACool+T-shirt&color=blue&size=XL&quantity=4&id=4&name=HBD+T-Shirt&color=yellow&size=XL&quantity=900
In the above link it should only get the information associated with id=3 only because i tried to update the quantity of id=3.
Here is my code. Any suggestions or help will be highly appreciated.
Code
if(isset($_GET['action']) && $_GET['action']=="update"){
$id= intval($_GET['id']);
$size = $_GET['size'];
$color = $_GET['color'];
$qty = $_GET['quantity'];
$index = $id." ".$color. " ".$size;
if( isset($_SESSION['cart'][$index]) && isset($_SESSION['cart'][$index]['color']) && $_SESSION['cart'][$index]['color'] == $color && isset($_SESSION['cart'][$index]['size']) && $_SESSION['cart'][$index]['size'] == $size){
$_SESSION['cart'][$index]['quantity']=$qty;
print_r($_SESSION['cart'][$index]);//It just shows me the last item array.
}
}
?>
<form class="product" method="get" action="index.php">
<table>
<input type="hidden" name="page" value="cart">
<input type="hidden" name="action" value="update">
<?php
if(isset($_SESSION['cart'])){
foreach($_SESSION['cart'] as $id => $value){
?>
<tr>
<input type="hidden" name="id" value="<?php echo $value['id'] ?>">
<input type="hidden" name="name" value="<?php echo $value['name'] ?>">
<input type="hidden" name="color" value="<?php echo $value['color'] ?>">
<input type="hidden" name="size" value="<?php echo $value['size'] ?>">
<td><?php echo $value['id'] ?></td>
<td><?php echo $value['name']?></td>
<td><?php echo $value['price']?> </td>
<td><?php echo $value['quantity']?><input type="text" name="quantity" value="<?php echo $value['quantity'] ?>"></td>
<td><?php echo $value['color'];?> </td>
<td><?php echo $value['size']; ?></td>
<td><?php echo "$" .$value['price']*$value['quantity']. ".00"; ?>
</tr>
<?php
}
}
?>
<tr><td><button type="submit">Update</button></td></tr>
</table>
</form>
You can update the quantity by not posting or getting all the cart product. Either you can use simple JS to update the field value or use SESSION variable array to update data as #Marc B mentioned.
Im actually trying to add only a specific row from the table product to the table product_add but the last row is being inserted in the table product_add. What i want is that when I click on the button ADD that specific row is being inserted in the table product_add. I think that the code is not considering the if (isset($_REQUEST['submit'])) part.
<?php
include'connect.php';
$image = isset($_GET['image']) ? $_GET['image'] : "";
$id = isset($_GET['id']) ? $_GET['id'] : "";
$name = isset($_GET['name']) ? $_GET['name'] : "";
$price= isset($_GET['price']) ? $_GET['price'] : "";
$sql="SELECT * FROM product";
$result = mysql_query($sql);
if($result>0){
?>
<form method="POST" id="form" name="form">
<table border='1'>
<tr>
<th>Id</th>
<th>Image</th>
<th>Name</th>
<th>Price MUR</th>
</tr>
<?php
while ($row = mysql_fetch_array($result)){
extract($row);
?>
<tr>
<td><?php echo ($row['id']); ?></td>
<td><img src=<?php echo $row['image'] ?> width='120' height='100'/></td>
<td><?php echo htmlspecialchars($row['name']); ?></td>
<td><?php echo htmlspecialchars($row['price']); ?></td>
<td>
<input id="submit" type="submit" name="submit" value='ADD'/>
</td>
</tr>
<?php
}
?>
</table>
</form>
<?php
}
if (isset($_REQUEST['submit']))
{
$insert = "INSERT INTO product_add(id, name, price) VALUES ('$id', '$name','$price')";
$insertQuery=mysql_query($insert);
}
?>
basically it a general coding issue
type submit and also name submit is conflicting
first add an attribute in form tag enctype="multipart/form-data" for image or video upload
then
creat a hidden field named 'something'
like
<input type="hidden" name="something" />
and after that use this
isset($_REQUEST['something']
add from in each while
like this updated code
<?php
include'connect.php';
$image = isset($_REQUEST['image']) ? $_REQUEST['image'] : "";
$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : "";
$name = isset($_REQUEST['name']) ? $_REQUEST['name'] : "";
$price= isset($_REQUEST['price']) ? $_REQUEST['price'] : "";
$sql="SELECT * FROM product";
$result = mysql_query($sql);
if($result>0){
?>
<table border='1'>
<tr>
<th>Id</th>
<th>Image</th>
<th>Name</th>
<th>Price MUR</th>
</tr>
<?php
while ($row = mysql_fetch_array($result)){
?>
<tr>
<td><?php echo ($row['id']); ?></td>
<td><img src=<?php echo $row['image'] ?> width='120' height='100'/></td>
<td><?php echo htmlspecialchars($row['name']); ?></td>
<td><?php echo htmlspecialchars($row['price']); ?></td>
<td>
<form method="POST" action="" >
<input type="hidden" name="id" value="<?php echo $row['id']; ?>" />
<input type="hidden" name="name" value="<?php echo $row['name']; ?>" />
<input type="hidden" name="image" value="<?php echo $row['image']; ?>" />
<input type="hidden" name="price" value="<?php echo $row['price']; ?>" />
<input id="submit" type="submit" name="submit" value='ADD'/>
</form>
</td>
</tr>
<?php
}
?>
</table>
<?php
}
if (isset($_REQUEST['submit']))
{
$insert = "INSERT INTO product_add(id, name, price) VALUES ('$id', '$name','$price')";
$insertQuery=mysql_query($insert);
}
?>
NOTE: mysql_* is deprecated use mysqli_* or PDO
It's been a while since I looked at php and I've got a brain block. I'm trying to get the values from a table using foreach so that I can store the values in a session and also display the number of items that have been ordered.
eg 2 of item number 4 etc
here's the table / form
<form id="products" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
//every item from the products table is queried because all fields need to be displayed
$sSQL = "SELECT * FROM products";
$rsResult = mysql_query($sSQL);
?>
<table id="products_table">
<tr style="font-weight:bold">
<td style="text-align:center">ID</td>
<td>Ref No.</td>
<td>Product Name</td>
<td>Description</td>
<td style="text-align:right">Price</td>
<td colspan='2' style='text-align:center'>Add To Order</td>
</tr>
<!--for each record in the table that matches the query a row is created in the table and the data in the relevant field is displayed-->
<?php while ($row = mysql_fetch_array($rsResult)){ ?>
<tr>
<input type="hidden" name="productID[<? echo $row['productID']; ?>]" value="<? echo $row['productID']; ?>" />
<td style="text-align:center"><? echo $row['productID']; ?></td>
<td><? echo $row['productReference']; ?></td>
<td><? echo $row['productName']; ?></td>
<td><? echo $row['productDescription']; ?></td>
<td style="text-align:right"><? echo '£'. $row['productPrice']; ?></td>
<td style="text-align:center"><span>Qty</span><input type='text' name="qty[<? echo ($_POST['qty']); ?>]" value="" ></td>
<? } ?>
</table>
Heres the php
<?php
foreach($_POST as $key => $value){
echo $key . ' ' .$value.'<br />';
print_r($key);
}
?>
I know that the php is no where complete for filling sessions etc, I just can't work out how to get the values out of the form. This php was my attempt to try and at least get some kind of value out of it
Any help is greatly appreciated
in your code , there is no need of
<input type="hidden" name="productID[<? echo $row['productID']; ?>
field .
also change the line
<td style="text-align:center"><span>Qty</span><input type='text' name="qty[<? echo ($_POST['qty']); ?>]" value="" ></td>
to
<td style="text-align:center"><span>Qty</span><input type='text' name="qty[<? echo ($row['productID']); ?>]" value="" ></td>
so that after the submission of the form , you can access the posted values with :
$sSQL = "SELECT * FROM products";
$rsResult = mysql_query($sSQL);
while ($row = mysql_fetch_array($rsResult))
{
echo $_POST['qty'.$row['productID']];
}
why don't you just display data in inputs with proper names?