Update multiple rows without changing id value - php

We have Table like below :
Once we click on "Submit" button , I am displaying random numbers below column "tracking id" using below query , its working fine:
$sql = $con->query("update orders set tracking_id = '$r' WHERE id ='1'");
$sql = $con->query("update orders set tracking_id = '$r' WHERE id ='2'");
But when i use below query, its not updating....
$sql = $con->query("update orders set tracking_id = '$r' WHERE id ='$id'");
Code :
<?php
$result = mysqli_query($con,"SELECT * FROM orders");
echo "<table border='1'>
<tr>
<th>order</th>
<th>payment</th>
<th>generate</th>
<th>tracking id</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
$id = $row['id'];
echo "<tr>";
echo "<td>" . $row['order_id'] . "</td>";
echo "<td>" . $row['payment_type'] . "</td>";
echo "<td><form method='post' action='new1.php'>
<input type = submit>
</form> </td>";
echo "<td>" . $row['tracking_id'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
new1.php
<?php
$id = $row['id'];
$r = mt_rand(1000,9999);
$sql = $con->query("update orders set tracking_id = '$r' WHERE id ='$id'");
?>

I think that you're not including the details in the form
<?php
$result = mysqli_query($con, "SELECT * FROM orders");
echo "<table border='1'>
<tr>
<th>order</th>
<th>payment</th>
<th>generate</th>
<th>tracking id</th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
$id = $row['id'];
echo "<tr>";
echo "<td>" . $row['order_id'] . "</td>";
echo "<td>" . $row['payment_type'] . "</td>";
echo "<td>";
if (empty($row['tracking_id'])) {
echo "<form method='post' action='new1.php'>";
echo "<input type ='hidden' name='id' value='$id'>
<input type='submit'>
</form>";
}
echo "</td>";
echo "<td>" . $row['tracking_id'] . " </td > ";
echo "</tr>";
}
echo "</table >";
?>
And you also need to modify new1.php
<?php
$id = $_POST['id'];
$r = mt_rand(1000,9999);
$sql = $con->query("update orders set tracking_id = '$r' WHERE id ='$id'");
?>

Related

copy the tracking id from Table 2's column to Table 1's column

In below image , Once we click on "submit" button in third column , I want to copy the tracking id from Table 2's column to Table 1's column....
Table orders
displayed tracking id" in 4th column
I saved order information in table orders [ Table 1 ]
I saved tracking ids in table awbno [ Table 2 ] & in column tracking_two
track.php
<?php
$con = mysqli_connect("localhost","root","","do_management4");
$result = mysqli_query($con,"SELECT * FROM orders");
echo "<table border='1'>
<tr>
<th>order</th>
<th>payment</th>
<th>generate</th>
<th>tracking id</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
$id = $row['id'];
echo "<tr>";
echo "<td>" . $row['order_id'] . "</td>";
echo "<td>" . $row['payment_type'] . "</td>";
echo "<td>";
if (empty($row['tracking_one'])) {
echo "<form method='post' action='call.php'>";
echo "<input type ='hidden' name='id' value='$id'>
<input type='submit'>
</form>";
}
echo "</td>";
echo "<td>" . $row['tracking_one'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
call.php
<?php
$con = mysqli_connect("localhost","root","","do_management4");
$result = mysqli_query($con,"SELECT * FROM orders");
$id = $_POST['id'];
$r = "";
$sql = $con->query("update orders set tracking_one = '$r' WHERE id ='$id'");
mysqli_close($con);
?>
I did't found any particular query in google, What query will help me ?
Your query should be something like this
$sql = $con->query("update orders set tracking_one = (select tracking_two from awbno WHERE id =$id)");

get data depend on particular id using php and mysql

i just create the simple cart app.in this i add products to the cart,from that i want to buy the one particular product.So i want to get and display product based on product id. In this, i had receiving the error.Can anyone help to solve this problem?
Inserting product to database:
<?php
Include('connect.php');
$productId=$_POST['productId'];
$productName=$_POST['productName'];
$productPrice=$_POST['productPrice'];
$quantity=$_POST['quantity'];
$sql=mysql_query("INSERT INTO products (productId,productName,productPrice,quantity)
VALUES ('$productId','$productName','$productPrice','$quantity')") or die(mysql_error());
if($sql){
header('Location:buy.php');
}
else
{
echo"Failed to add cart !";
}
?>
Adding products to cart:
<?php
include('connect.php');
$sql="SELECT * FROM buy";
$result = mysql_query($sql);
$myArray = array();
echo "<table style='width:50%;'>
<tr>
<th>Product Name</th>
<th>Product Price</th>
<th>Quantity</th>
</tr>";
$index = 0;
while($row = mysql_fetch_assoc($result))
{
$myArray[$index] = $row;
$index++;
echo "<tr>";
echo "<td>" . $row['productName'] . "</td>";
echo "<td>" . $row['productPrice'] . "</td>";
echo "<td>" . $row['quantity'] . "</td>";
echo "<td><a href='quantity.php'><button class='btn btn-default col-md- offset-2' value='submit'>Buy Now</button></a></td>" ;
echo "<td><button class='btn btn-danger' value='submit'>Buy Later</button> </td>" ;
echo "</tr>";
}
?>
choose product and display product based on id:
<?php
include('connect.php');
$productId=$_GET['productId'];
$sql="SELECT productName, productPrice, quantity FROM products WHERE productId=" . $productId . "";
$result = mysql_query($sql);
$myArray = array();
echo "<table style='width:50%;'>
<tr>
<th>Product Name</th>
<th>Product Price</th>
<th>Quantity</th>
</tr>";
$index = 0;
while($row = mysql_fetch_assoc($result))
{
$myArray[$index] = $row;
$index++;
echo "<tr>";
echo "<td>" . $row['productName'] . "</td>";
echo "<td>" . $row['productPrice'] . "</td>";
echo "<td>" . $row['quantity'] . "</td>";
echo "</tr>";
}
?>

Delete button on PHP echo table not working

Below is my code for a PHP echo delete that contains a delete button. When pressed I want that entry to be deleted from the database
$result = mysql_query("SELECT * ,CONCAT (HomeScore,'-',AwayScore) AS Score, CONCAT(Against) AS Game FROM Fixture WHERE TeamID='9' ORDER BY Date DESC");
echo "<table id='customers' border='1'>;
<tr>
<th>FixtureID</th>
<th>Competition</th>
<th>Match</th>
<th>Date</th>
<th>Time</th>
<th>Score</th>
<th>test</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FixtureID'] . "</td>";
echo "<td>" . $row['Competition'] . "</td>";
echo "<td>" . $row["Against"] . "</td>";
echo "<td>" . $row['Date'] . "</td>";
echo "<td>" . $row['Time'] . "</td>";
echo "<td>" . $row['Score'] . "</td>";
echo "<td><form method=post>
<input name=id type=hidden value='".$row['FixtureID']."';>
<input type=submit name=submit value=Delete>
</form></td>";
echo "</tr>";
echo "</tr>";
}
}
echo "</table>";
// delete record
if($_SERVER['REQUEST_METHOD'] == "POST")
{
if(isset($_POST['FixtureID']))
{
$id = FixtureID;
$sql = mysql_query("DELETE FROM Fixture WHERE FixtureID =$id");
if(!$sql)
{
echo ("Could not delete rows" .mysql_error());
}
}
}
How do I get this to work? Also FixtureID is stored as an integer in the database.
Try with these changes :
// delete record
if(isset($_POST['submit']) && isset($_POST['id']) && !empty($_POST['id'])) {
$id = $_POST['id'];
$sql = mysql_query("DELETE FROM Fixture WHERE FixtureID =$id");
if(!$sql) {
echo ("Could not delete rows" .mysql_error());
}
}
Edit :
// Delete record if ID submitted
if(isset($_POST['submit']) && isset($_POST['id']) && !empty($_POST['id'])) {
$id = $_POST['id'];
$sql = mysql_query("DELETE FROM Fixture WHERE FixtureID =$id");
if(!$sql) {
echo ("Could not delete rows" .mysql_error());
}
}
// Get datas from BDD
$result = mysql_query("SELECT * ,CONCAT (HomeScore,'-',AwayScore) AS Score, CONCAT(Against) AS Game FROM Fixture WHERE TeamID='9' ORDER BY Date DESC");
// Display data
echo "
<table id='customers' border='1'>
<tr>
<th>FixtureID</th>
<th>Competition</th>
<th>Match</th>
<th>Date</th>
<th>Time</th>
<th>Score</th>
<th>test</th>
</tr>";
// For each result
while($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['FixtureID'] . "</td>";
echo "<td>" . $row['Competition'] . "</td>";
echo "<td>" . $row["Against"] . "</td>";
echo "<td>" . $row['Date'] . "</td>";
echo "<td>" . $row['Time'] . "</td>";
echo "<td>" . $row['Score'] . "</td>";
echo "<td><form method=post>
<input name=id type=hidden value='".$row['FixtureID']."';>
<input type=submit name=submit value=Delete>
</form></td>";
echo "</tr>";
echo "</tr>";
}
echo "</table>";
just try this
if(isset($_POST['submit']))
{
$id = $_POST['id']; // here you should use form post value
$sql = mysql_query("DELETE FROM Fixture WHERE FixtureID =$id");
if(!$sql)
{
echo ("Could not delete rows" .mysql_error());
}
}
}
$id value is not properly set
$id = $_POST['FixtureID'];

unable to sum the values that are fetched from the database and display

I am making a project on proposal management system and I am stuck on the pricing module. Here is my HTML DEMO. I have shown only required field in the output. INSERT queries below are correct.
I am trying to AUTOMATICALLY display the sum of the 'amount' in the 'total' each time the user presses 'save' button . The value of 'total' should increment when user presses 'save' either from 'add services' table or from 'add products' table. I am unsuccessful so far. HOW CAN I DO IT?
I have tried this: (Any other solution will be appreciated).
<td> <input type="text" name="amount" maxlength="15" id="amount" readonly="true" value=" <?php
if(isset($_POST['save'])|| isset($_POST['psave']))
{
$con= mysqli_connect("localhost","root","","my_db")or die("cannot connect");
$sql = "SELECT proposal_services.service_amount AMOUNT,proposal_products.product_amount AMOUNT1, FROM proposal_services INNER JOIN proposal_products ON proposal_products.proposal_id=proposal_services.proposal_id";
$result= mysqli_query($con,$sql);
$row = mysqli_fetch_array($result);
foreach ( $row as $records )
{
$sum = str_replace(",", "", $records['AMOUNT']) + str_replace(",", "", $records['AMOUNT1']);
}
echo number_format( $sum, 2 );
}
?> "/></td>
my php code where I insert and display results is:
insert_services.php
<?php
session_start();
$prop_id = $_SESSION['propl_id'];
$user_id = $_SESSION['sign_user_id'];
$con = mysqli_connect("localhost","root","","my_db");
$name = $_POST['ser_name'];
$desc = $_POST['txt'];
$price = $_POST['price'];
$per = $_POST['per'];
$quantity = $_POST['quantity'];
$amount = $_POST['amount'];
if(mysqli_query($con,"INSERT INTO proposal_services(proposal_id,user_id,service_name,service_description,service_price,per,service_quantity,service_amount) VALUES('$prop_id','$user_id','$name','$desc','$price','$per','$quantity','$amount')"))
$sql= "select * FROM proposal_services WHERE proposal_id='$prop_id' ";
$result= mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th align='center'>Service Name</th>
<th align='center'>Description</th>
<th align='center'>Price</th>
<th align='center'>per</th>
<th align='center'>Quantity</th>
<th align='center'>Amount</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<div id='change'>";
echo "<tr>";
echo "<td align='center'>" . $row['service_name'] . "</td>";
echo "<td align='center'>" . $row['service_description']. "</td>";
echo "<td align='center'>" . $row['service_price']. "</td>";
echo "<td align='center'>" . $row['per']. "</td>";
echo "<td align='center'>" . $row['service_quantity']. "</td>";
echo "<td align='center'>" . $row['service_amount']. "</td>";
echo "</tr>";
echo "</div>";
}
echo "</table>";
?>
insert_products.php
<?php
session_start();
$prop_id = $_SESSION['propl_id'];
$user_id = $_SESSION['sign_user_id'];
$con = mysqli_connect("localhost","root","","my_db");
$name = $_POST['pro_name'];
$desc = $_POST['txt'];
$price = $_POST['price'];
$per = $_POST['per'];
$quantity = $_POST['quantity'];
$amount = $_POST['amount'];
if(mysqli_query($con,"INSERT INTO proposal_products(proposal_id,user_id,product_name,product_description,product_price,per,product_quantity,product_amount) VALUES('$prop_id','$user_id','$name','$desc','$price','$per','$quantity','$amount')"))
$sql= "select * FROM proposal_products WHERE proposal_id='$prop_id'";
$result= mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>Product Name</th>
<th>Description</th>
<th>Price</th>
<th>per</th>
<th>Quantity</th>
<th>Amount</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['product_name'] . "</td>";
echo "<td>" . $row['product_description'] . "</td>";
echo "<td>" . $row['product_price'] . "</td>";
echo "<td>" . $row['per'] . "</td>";
echo "<td>" . $row['product_quantity'] . "</td>";
echo "<td>" . $row['product_amount'] . "</td>";
echo "<td align='center'>" . "<input type='button' value= 'edit'>" . "</td>";
echo "</tr>";
}
echo "</table>";
?>

PHP beginner. Trying to display UserId

I am accessing my database on PhpMyAdmin, all my names etc are correct, but for some reason UserId is not working. Can someone point me in the right direction?
I have tried printing it but nothing displays.
<?php session_start();
$username = $_GET['username'];
$password = $_GET['password'];
// Create connection
$con=mysqli_connect("localhost","root","","test");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM user2 where username='$username' and password='$password'");
$row_cnt = mysqli_num_rows($result);
if($row_cnt >0){
while($row = mysqli_fetch_array($result)){
$UserId = $row['UserId'];
}
$sqlQuery2 = "SELECT ProductID, Name, Price, Description FROM product";
echo "Hello ".$username."<br>" .$UserId. "<br> This is a list of products";
$result2 = mysqli_query($con,$sqlQuery2);
echo "<table border='1'>
<tr>
<th>ProductID</th>
<th>Name</th>
<th>Price</th>
<th>Description</th>
<th>View</th>
</tr>";
while($row = mysqli_fetch_array($result2))
{
echo "<tr>";
echo "<td>" . $row['ProductID'] . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Price'] . "</td>";
echo "<td>" . $row['Description'] . "</td>";
echo "<td><a href=\"detailview.php?ProductID=".$row['ProductID']."\"'>Detailed View</a></td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Update My Details
<?php } else{
echo "invalid login "; }
?>
use var_dump($var) to see what is in the variable
1st check what is returned in $result and after check $UserId before using it
after the while check if $UserId is set (if the condition is false, ou var is not set..) you should check $row_count too
you should indent your code
here is your code reindented :
<?php session_start();
$username = $_GET['username'];
$password = $_GET['password'];
// Create connection
$con=mysqli_connect("localhost","root","","test");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM user2 where username='$username' and password='$password'");
$row_cnt = mysqli_num_rows($result);
if($row_cnt >0){
while($row = mysqli_fetch_array($result)){
$UserId = $row['UserId'];
}
$sqlQuery2 = "SELECT ProductID, Name, Price, Description FROM product";
echo "Hello ".$username."<br>" .$UserId. "<br> This is a list of products";
$result2 = mysqli_query($con,$sqlQuery2);
echo "<table border='1'>
<tr>
<th>ProductID</th>
<th>Name</th>
<th>Price</th>
<th>Description</th>
<th>View</th>
</tr>";
while($row = mysqli_fetch_array($result2))
{
echo "<tr>";
echo "<td>" . $row['ProductID'] . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Price'] . "</td>";
echo "<td>" . $row['Description'] . "</td>";
echo "<td><a href=\"detailview.php?ProductID=".$row['ProductID']."\"'>Detailed View</a></td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Update My Details
<?php
}
else
{
echo "invalid login ";
}
?>

Categories