take multiple input from user on dynamic post name - php

Please check the codes bellow. I need to take each products quantity value input form user. But the products/id are dynamic and coming form database. how can i receive and identify which products quantity value will be what submitted by user? i mean since product is multiple/dynamic coming form database then so i cant get them by $_POST['id']. whats the solution for this case then?
<form method="post">
<table class="table table-hover">
<thead>
<tr>
<th>Stock Available</th>
<th>Product Name</th>
<th>Enter Order Quantity</th>
</tr>
</thead>
<tbody>
<?php
$q= mysqli_query($conn,"SELECT * FROM products");
while ($row=mysqli_fetch_array($q)) {
$id = $row['id'];
$product_name = $row['product_name'];
$available_stock = $row['available_stock'];
echo '
<tr>
<td>'.$available_stock.'</td>
<td>'.$product_name.'</td>
<td><input type="number" min="1" class="form-control" name="qty[]"></td>
<input type="hidden" name="id[]" value="'.$id.'">
<tr>
';
}
?>
</tbody>
</table><br><br>
<button type="submit" class="btn btn-success" name="submit_next_order2">Confirm This Order</button>
</form>

<?php
if($_POST['submit_next_order2'])
{
print_R($_POST);die;
}
?>
Please update your html.
<?php
$q= mysqli_query($conn,"SELECT * FROM products");
while ($row=mysqli_fetch_array($q)) {
$id = $row['id'];
$product_name = $row['product_name'];
$available_stock = $row['available_stock'];
echo '
<tr>
<td>'.$available_stock.'</td>
<td>'.$product_name.'</td>
<td><input type="number" min="1" class="form-control" name="qty['.$id .'][]"></td>
<input type="hidden" name="id['.$id .'][]" value="'.$id.'">
<tr>
';
}
?>

Related

How to populate the data from database using popup

i am working on project.in which i want to fetch data by clientid and populate it to the rest fields.(just for user convenience).i have done with it.but its not working on a popup.and body suggest me the good way or guide me where i am doing the mistake.
html code:
<table class="table table-bordered">
<tr>
<th>
Client Id
</th>
<th>
Client Name
</th>
<th>
Client Number
</th>
<th>
Address
</th>
</tr>
<tr>
<td>
<input class="form-control" type='text' id="countryname_1" />
</td>
<td>
<input class="form-control" type='text' id="country_no_1"/>
</td>
<td>
<input class="form-control" type='text' id="phone_code_1" />
</td>
<td>
<input class="form-control" type='text' id="country_code_1" />
</td>
</tr>
</table>
ajax code:
<?php
require_once 'config.php';
if($_POST['type'] == 'country_table'){
$row_num = $_POST['row_num'];
$name = $_POST['name_startsWith'];
$query = "SELECT clientId, clientName, contactNo, address FROM clients";
$result = mysqli_query($con, $query);
$data = array();
while ($row = mysqli_fetch_assoc($result)) {
$name = $row['clientId'].'|'.$row['clientName'].'|'.$row['contactNo'].'|'.$row['address'].'|'.$row_num;
array_push($data, $name);
}
echo json_encode($data);
}?>
this code is working individually.not in the popup.any suggestion

How to get the input value from multiple value when button pressed php

i want to make a cart and my product is pizza.
so i'm listing all the menu to the table, and add a button to submit menu to cart.
here's my index.php
<?php
require("connect.php");
$result = mysqli_query($con,'select * from menu');
?>
<form action="cart.php" method="get">
<table border="1" style="width:100%">
<tr>
<th>Pizza Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Buy</th>
</tr>
<?php while($product = mysqli_fetch_object($result)){ ?>
<tr>
<td><?php echo $product->nama_menu; ?></td>
<td><?php echo $product->harga_menu; ?></td>
<td><input type="number" name="quantity" min="1" max="20"></td>
<td><button type="submit" name="id" value="<?php echo $product->id_menu; ?>">Add To Cart</button></td>
</tr>
<?php } ?>
</table>
</form>
But when i pressed the button "Add To Cart", it sends all the quantity from the menu listing and can't be read in my cart.php
can anyone help me how to get the right quantity value when i pressed the button add to cart.
Make separate form for each of the item. Try below code.
<?php
require("connect.php");
$result = mysqli_query($con,'select * from menu');
?>
<table border="1" style="width:100%">
<tr>
<th>Pizza Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Buy</th>
</tr>
<?php while($product = mysqli_fetch_object($result)){ ?>
<form action="cart.php" method="get">
<tr>
<td><?php echo $product->nama_menu; ?></td>
<td><?php echo $product->harga_menu; ?></td>
<td><input type="number" name="quantity" min="1" max="20"></td>
<td><button type="submit" name="id" value="<?php echo $product->id_menu; ?>">Add To Cart</button></td>
</tr>
</form>
<?php } ?>
</table>
Try to use array in name then submit it will give you seperate quantity.
<td><input type="number" name="quantity[<?php echo $product->nama_menu; ?>]" min="1" max="20">
Output:
quantity['pizza1'] = 10
quantity['pizza2'] = 20
....
Another Option is use dynamic name for number.
<td><input type="number" name="quantity_<?php echo $product->nama_menu; ?>" min="1" max="20">
Output:
quantity_pizza1 = 10
quantity_pizza2 = 20
....

How to view images stored in BLOB format using PHP and MYSQL

I am doing a registration form where i am also saving image to database, well that image is being stored in LONGBLOB format. I am able to add and view all the data accept the image part.
Here is my product_register.php code:
<form role="form" method="post" action="product_register.php">
<fieldset>
<div class="form-group">
<input class="form-control" placeholder="Product Name" name="product_name" type="text" autofocus>
</div>
<div class="form-group">
<input class="form-control" placeholder="Product Code" name="product_code" type="text" autofocus>
</div>
<div class="form-group">
<input class="form-control" placeholder="Product Image" name="product_image" type="file" value="">
</div>
<div class="form-group">
<input class="form-control" placeholder="Purchase Date" name="date" type="date" autofocus>
</div>
<div class="form-group">
<input class="form-control" placeholder="Time Guarantee" name="time_guarantee" type="text" autofocus>
</div>
<input class="btn btn-lg btn-success btn-block" type="submit" value="submit" name="submit" >
</fieldset>
</form>
<?php
include("db_conection.php");//make connection here
//mysql_close();
if(isset($_POST['submit']))
{
$usern = $_SESSION['sess_username'];
$product_name = $_POST['product_name'];
$product_code = $_POST['product_code'];
$product_image = $_POS['product_image'];
$date = $_POST['date'];
$time_guarantee = $_POST['time_guarantee'];
$insert_user = "insert into product (product_name,product_code,product_image,date,time_guarantee,usern) VALUE ('$product_name','$product_code','$product_image','$date','$time_guarantee','$usern')";
if(mysqli_query($dbcon,$insert_user))
// $result = mysql_query($insert_user);
{
echo "<script type='text/javascript'>alert('Product added successfully !!!')</script>";
// echo"<script>window.open('product_register.php','_self')</script>";
// mysql_close();
}
}
?>
Here is the code to view it view_product.php code:
<table class="table table-bordered table-hover table-striped" style="table-layout: fixed">
<thead>
<tr>
<!-- <th>User Id</th> -->
<th>Product Name</th>
<th>Product Code</th>
<th>Product Image</th>
<th>Date of Purchase</th>
<th>Time Guarantee</th>
<!-- <th>Delete User</th> -->
</tr>
</thead>
<?php
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("gmgmt") or die(mysql_error());
// $view_users_query="select * from users WHERE role='user'"; //select query for viewing users.
// $view_users_query="select * from users WHERE role='admin'"; //select query for viewing users.
$view_users_query="select * from 'product' WHERE 'usern' = 'demo'"; //select query for viewing users.
// $run=mysql_query($view_users_query);//here run the sql query. // '".$_SESSION['userid']."'
$check = mysql_query("SELECT * FROM product WHERE usern = '".$_SESSION['sess_username']."' ") or die(mysql_error());
while($row=mysql_fetch_array($check)) //while look to fetch the result and store in a array $row.
{
$id=$row[0];
$product_name=$row[1];
$product_code=$row[2];
$product_image=$row[3];
$date=$row[4];
$time_guarantee=$row[5];
// $usern=$row[6];
?>
<tr>
<!--here showing results in the table -->
<!-- <td><?php echo $id; ?></td> -->
<td><?php echo $product_name; ?></td>
<td><?php echo $product_code; ?></td>
<td><?php echo "<img src='php/imgView.php?imgId=".$product_image."' />"; ?></td>
<td><?php echo $date; ?></td>
<td><?php echo $time_guarantee; ?></td>
<!-- <td><button class="btn btn-danger">Delete</button></td> -->
</tr>
<?php } ?>
</table>
I searched a lot but I didn't find a code or method that suits my needs.
echo '<img src="data:image/jpeg;base64,' . base64_encode( $row['imageContent'] ) . '" />';
Use the above code to display you image store in mysql database. Hope this will help you

Update failed after edit the data [no error]

I have made a edit form...but when i press the edit button..it works...when after complete the edit if i press the update button...it goes to main page but no field updated.
edit.page
<body>
<table align="center">
<tr>
<td align="center">Edit data</td>
</tr>
<tr>
<td>
<table border="2">
<th>SL</th>
<th>name</th>
<th>address</th>
<th>action</th>
<?php
include"dbc.php";//database conncetion
$order = "select * from tbl_record";
$result = mysqli_query($con,$order);
while ($row=mysqli_fetch_array($result)){
echo ("<tr><td>$row[employees_number]</td>");
echo ("<td>$row[name]</td>");
echo ("<td>$row[address]</td>");
echo ("<td>Edit</td></tr>");
}
mysqli_close($con);
?>
</table>
</td>
</tr>
</table>
</body>
</html>
edit form
<body>
<table border=2>
<tr>
<td align=center>Form Edit Employees Data</td>
</tr>
<tr>
<td>
<table border="1">
<?php
include "dbc.php";//database connection
$id = $_GET["id"];
$order = "SELECT * FROM tbl_record where employees_number='$id'";
$result = mysqli_query($con,$order);
$row = mysqli_fetch_array($result);
?>
<form method="post" action="edit_data.php">
<input type="hidden" name="id" value="<?php echo "$row[employees_number]"?>">
<tr>
<td>Name</td>
<td>
<input type="text" name="name"
size="20" value="<?php echo "$row[name]"?>">
</td>
</tr>
<tr>
<td>Address</td>
<td>
<input type="text" name="address" size="40"
value="<?php echo "$row[address]"?>">
</td>
</tr>
<tr>
<td align="right">
<input type="submit"
name="submit value" value="Edit">
</td>
</tr>
</form>
</table>
</td>
</tr>
</table>
</body>
update page
<?php
//edit_data.php
include "dbc.php";
if (isset($_POST['submit']))
{
$id = $_GET['id'];
$name = $_POST["name"];
$address = $_POST["address"];
mysqli_query("UPDATE tbl_record SET name='$name', address='$address' WHERE employees_number='$id'")
or die(mysqli_error());
}
header("location:edit.php");
?>
As per your comment "i am getting this error now mysqli_query() expects at least 2 parameters, 1 given ...for update page"
Add your connection parameter to the query:
mysqli_query("UPDATE
to that it reads as
mysqli_query($con, "UPDATE ...
Plus, your submit button should read as:
<input type="submit" name="submit" value="Edit">
The name="submit value" in it now, doesn't match the conditional statement for it
if (isset($_POST['submit']))

Store form values in a table on multiple requests

Here I have an HTML form that stores stores submitted data in a table.
<label> ID: </label><input type="text" name="id"/>
<label>Name :</label><textarea name='Name'></textarea>
<label>Value :</label><br /><input type="text" name="Value"/>
<input type="submit" name="submit" value=" submit "/>
Next time I submit the form, the table refreshes and stores the new values. Instead I need it to add new rows to table and store all previous data I submit during the session. How do I add the new rows without using a data base?
<?php
session_start();
echo "<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Value</th>
</tr>";
if (isset($_POST['submit'])) {
echo "
<tr>
<td>".$_POST['id']."</td>
<td>".$_POST['Name']."</td>
<td>".$_POST['Value']."</td>
</tr>";
}
Try this:
<?php
session_start();
echo "<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Value</th>
</tr>";
if (isset($_POST['submit'])) {
$_SESSION['posts'][] = $_POST;
foreach ($_SESSION['posts'] as $post)
echo "<tr>
<td>{$post['id']}</td>
<td>{$post['Name']}</td>
<td>{$post['Value']}</td>
</tr>";
}
or if you want it all in one page:
<?php
session_start();
echo "<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Value</th>
</tr>";
if (isset($_POST['submit'])) {
$_SESSION['posts'][] = $_POST;
foreach ($_SESSION['posts'] as $post)
echo "<tr>
<td>{$post['id']}</td>
<td>{$post['Name']}</td>
<td>{$post['Value']}</td>
</tr>";
}
?>
<form action="" method="post">
<label> ID: </label><input type="text" name="id"/><br>
<label>Name :</label><textarea name='Name'></textarea><br>
<label>Value :</label><br /><input type="text" name="Value"/><br>
<input type="submit" name="submit" value=" submit "/><br>

Categories