<form method="post">
<div class="form-group">
<label >Product</label>
<select class="form-control" name="product" id="supplier-select" >
<option value="">Select a Product </option>
<?php
$link = mysqli_connect();
if (!$link) {
die('Could not connect: ' . mysqli_connect_error());
}
$query="select id,name from product";
$result = mysqli_query($link,$query);
while($row = mysqli_fetch_array($result)){
echo "<option value=".$row['id']."name='product' >".$row['name']."</option>";
}
?></select>
</div>
<div class="form-group">
<label >Date</label>
<input type="date" name="date" class="form-control" id="" >
</div>
<div class="form-group">
<label >Sale Quantity</label>
<input type="text" name="quantity" class="form-control" id="">
</div>
<div class="form-group">
<label >Sale Price</label>
<input type="text" name="price" class="form-control" id="">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$id = $_POST['id'];
$product = $row['product'];
$date =$_POST['date'];
$quan = $_POST['quantity'];
$price = $_POST['price'];
$link= mysqli_connect();
if(!$link){
die ('connection unsuccessful'. mysqli_connect_error($link));
}
$sql = "INSERT INTO items_sale (sale_id, prod_id, date, sale_quantity, sale_price) VALUES ('$id','$product','$date','$quan','$price')";
if (mysqli_query($link, $sql)) {
exit();
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($link);
}
how do I pass in value from selected option from to insert query in this code here. i have using row[] but the value is not passing on in the from to my insert query when I selected. in past i have tried POST['id'] but it select the same value as the id before.
Try changing line
echo "<option value=".$row['id']."name='product' >".$row['name']."</option>";
to
echo "<option value=".$row['id'].">".$row['name']."</option>"
$row['product'] doesn't exist - try changing the beginning of your php code to
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$id = $_POST['id'];
$product = $_POST['product'];
...
Related
So I want to fetch my data from Database and be able to display and update it. Im on the part where I have to display a value from the db on a Dropdown but only the first option of the dropdown is being displayed.
Below is complete code.
<?php
mysqli_connect('localhost', 'root', '');
mysqli_select_db('storm');
$_GET['id'];
$ssh = $_GET['ssh'];
$_GET['provi'];
$_GET['impact'];
$_GET['advice'];
$_GET['date'];
$_GET['typhoon'];
$_GET['warning'];
?>
<html>
<body>
<div class="container">
<form action="edit.php" method="post">
<div class="form-group">
<label for="prov">Provinces</label><br>
<select id="prov" class="form-control" type="text" name="provi1">
<option value="Isabela"><?php echo $_GET['provi'];?></option>
<option value="La Union"><?php echo $_GET['provi'];?></option>
<option value="Pangasinan"><?php echo $_GET['provi'];?></option>
<option value="Ilocos Sur"><?php echo $_GET['provi'];?></option>
<option value="Ilocos Norte"><?php echo $_GET['provi'];?></option>
</select>
</div>
<div class="form-group">
<label>Date</label><br>
<input class="w3-input w3-border form-control" type="date" name="date" value="">
</div>
<div class="col-md-6">
<div class="form-group">
<label>Typhoon Name</label><br>
<input type="text" name="typhoon" value="<?php echo $_GET['typhoon']; ?>" class="form-control">
<input type="hidden" name="id" value="">
</div>
<div class="form-group">
<label>Warning #</label><br>
<input type="text" name="warning" value="<?php echo $_GET['warning']; ?>" class="form-control">
</div>
</div>
<div class="col">
<div class="form-group">
<input class="btnSubmit" type="submit" value="Update" name="submit" style="background-color: #408cff;">
<input class="btnSubmit" type="reset" value="Cancel" style="background-color: #de5959;">
</div>
</div>
</form>
<?php
if(isset($_GET['submit'])){
$id = $_GET['id'];
$ssh = $_GET['ssh'];
$muni = $_GET['muni'];
$impact = $_GET['impact'];
$advice = $_GET['advice'];
$date = $_GET['date'];
$typhoon = $_GET['typhoon'];
$warning = $_GET['warning'];
$query = "UPDATE twothree SET ssh='$ssh', muni='$muni', impact='$impact', advice='$advice', date='$date', typhoon='$typhoon', warning='$warning' WHERE id='$id'";
$data = mysqli_query($conn, $query);
if($data){
echo "Record Updated Successfully!";
}else{
echo "Record Not Updated.";
}
}
?>
I'm pretty sure that I am doing something wrong. Hope you guys figure it out for me. I'm new to this and I hope that I can learn from you guys. Thanks.
You can use something like this at basic level. this will retrieve all data from table and add a dropdown option to it.
<select class="" name="" required>
<option value="" selected disabled>Select a option</option>
<?php
$select_1 = $db->query("SELECT * FROM table");
while ($row_1 = $select_1->fetch_assoc()) {
?>
<option value="<?php echo $row_1['value']; ?>">
<?php echo $row_1['name']; ?>
</option>
<?php } ?>
</select>
where $db is your database connection and it must be mysqli connection.
You should do like this, to display the list of provinces.
<?php
$host = "localhost";
$user = "root";
$pwd = "";
$db = "storm";
$db_connection = new mysqli($host, $user, $pwd, $db);
if ($db_connection->connect_errno) {
printf("Connect failed: %s\n", $db_connection->connect_error);
exit();
}
//select provinces here
$provinces = $db_connection->query("Select * from provinces"); //change provinces according to your table name that you want to query.
?>
<div class="form-group">
<label for="prov">Provinces</label><br>
<select id="prov" class="form-control" type="text" name="provi1">
<?php
while ($row = $provinces->fetch_object()){
echo '<option value="'.$row->province_name.'">'.$row->province_name.'</option>'; //change province_name according to your fieldname.
}
?>
</select>
</div>
<?php
$db_connection->close();
?>
bro you need to loop data form your database to get continuous data out of your database you could use this.
<?php
$host = "localhost";
$user = "root";
$pwd = "";
$db = "storm";
$db_connection = new mysqli($host, $user, $pwd, $db);
if ($db_connection->connect_errno) {
printf("Connect failed: %s\n", $db_connection->connect_error);
exit();
}
//select provinces here
$query = mysqli_query($db_connection,"Select * from provinces");
?>
<div class="form-group">
<label for="prov">Provinces</label><br>
<select id="prov" class="form-control" type="text" name="provi1">
<?php
while ($row = mysqli_fetch_assoc($query)){
echo '<option value="'.$row["province_name"].'">'.$row["province_name"].'</option>'; //change province_name according to your fieldname.
}
?>
</select>
</div>
<?php
$db_connection->close();
?>
you code should look like this
I am wanting to submit a form using $_SERVER["PHP_SELF"]however its not picking up the id of the item when the form is being submitted, is there a way to add an id like the following and not have to link to another file...
<form action="process.php<?php echo"?id=$productID" ?> " method="post">
Surely there is a simple way of doing this.. I have spent alot of time googling and searching forums and nothing is giving me an explanation.
Thanks in advance.
EDIT
include "../model/functions_updateproducts.php";
function select_productspreparedGETIDt(){
global $conn;
$productID = $_GET['id'];
$nameError = "";
$sql = "SELECT * FROM product WHERE productID = :productID";
$statement = $conn->prepare($sql);
$statement->bindValue(':productID', $productID);
$statement->execute();
$result = $statement->fetchAll();
$statement->closeCursor();
foreach($result as $row):
?>
<form action="<?php echo $_SERVER["PHP_SELF"];?>" method="post">
<div class="form-group">
<input type="hidden" id="productID" name="productID" value="<?php echo $row['productID'] ?>" />
<label>Product Name</label>
<input type="text" class="form-control" id="productName" name="productName" placeholder="Enter Product Name" value="<?php echo $row['productName'] ?>" /><span class="error"> <?php echo $nameError;?></span>
</div>
<div class="form-group">
<label>Quantity</label>
<input type="Number" class="form-control" id="QTY" name="QTY" placeholder="Enter Quantity" value="<?php echo $row['QTY'] ?>" min='1' max='100' >
</div>
<div class="form-group">
<label>Price</label>
<input type="text" class="form-control" id="productPrice" name="productPrice" placeholder="Enter Price" value="<?php echo $row['productPrice'] ?>" required/>
</div>
<div class="form-group">
<label>Variable</label>
<input type="text" class="form-control" id="Variable" name="Variable" placeholder="Enter Variable" value="<?php echo $row['Variable'] ?>" required>
</div>
<div class="form-group">
<label>Description</label>
<textarea class="form-control" id="productDescription" name="productDescription" /><?php echo $row['productDescription'] ?></textarea>
</div>
<button type="submit" class="btn btn-primary">Update Item</button>
</form>
<?php
endforeach;
}
$nameError ="";
if(isset($_POST['submit'])){
if (empty($_POST["productName"])) {
$nameError = "Name is required";
} else {
$name = test_input($_POST["productName"]);
// check name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameError = "Only letters and white space allowed";
}
}
$productID = $_GET['id'];
$productName = $_POST['productName'];
$productDescription = $_POST['productDescription'];
$productPrice = $_POST['productPrice'];
$QTY = $_POST['QTY'];
$Variable = $_POST['Variable'];
global $conn;
$sql = "UPDATE product SET productName = :productName, productDescription = :productDescription, productPrice = :productPrice, QTY = :QTY, Variable = :Variable WHERE productID = :productID";
$statement = $conn->prepare($sql);
$statement->bindValue(':productName', $productName);
$statement->bindValue(':productDescription', $productDescription);
$statement->bindValue(':productPrice', $productPrice);
$statement->bindValue(':productID', $productID);
$statement->bindValue(':QTY', $QTY);
$statement->bindValue(':Variable', $Variable);
$result = $statement->execute();
$statement->closeCursor();
header("location: ../view/success.php");
return $result;
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<?php
}
?>
Inside your form put this code:
<input type="hidden" value="<?php echo $productID ?>" name="id" />
This will submit the hidden field. PHP wrote your $productID to it and it get submitted as part of the $_POST[]
<form action="process.php<?php echo"?id=$productID" ?> " method="post">
is totally fine.. access the productID with
$_GET['id']
I'm fairly certain it couldn't be the angular routing since the button has both ng-click and name="btn-save", one for directing back to the main page which works and the other is for the php section which doesnt work. I have spent quite a few days solving it.
I thought about passing form data to an ng-model then to the js file for angular to post it via a php file as the mysql call, then there are some values in my dropdown that couldn't be passed over so I shelved the idea.
Can anyone figure out what is wrong with it? Sorry of the code is messy.
addSales.php
<?php
require_once 'database.php';
//if submit button is clicked submit
if(isset($_POST['btn-save']))
{
$data = json_decode(file_get_contents("php://input"));
//sotred variables from form
$customer_name= $_POST['customer_name'];
$item_id= $_POST['item_name'];
//retrieve
$item_name = mysqli_query($connect,"SELECT ItemName FROM Items WHERE ItemID = '$item_id");
$country= $_POST['country_name'];
$quantity= $_POST['item_quantity'];
$price= $_POST['item_price'];
$sales_date= $_POST['sales_date'];
//insert sales record
mysqli_query($connect,"INSERT INTO Sales (CustomerName, ItemID, ItemName, Country, Quantity, Price, SalesDate) VALUES('Awful Days', 17, 'Xanax', 'Singapore', 1, 48.3, 2012-01-02)");
//mysqli_query($connect,"INSERT INTO Sales (CustomerName, ItemID, ItemName, Country, Quantity, Price, SalesDate) VALUES('$customer_name', $item_id, '$item_name', '$country_name', $quantity, $price, $sales_date)");
//update stock count
mysqli_query($connect,"UPDATE Items SET StockLeft = StockLeft - '$quantity' WHERE ItemID = '$item_id'");
//check stock after each record add
$stockchecker=mysqli_query($connect,"SELECT ItemName FROM Items WHERE StockLeft <= 5");
if($stockchecker != NULL)
{
$message = $stockchecker + "is running out of stock!";
echo "<script type='text/javascript'>alert('$message');</script>";
}
//return to index main page
echo "
<!DOCTYPE html>
<script>
function redir()
{
alert('Record successfully added.');
window.location.assign('index.php');
}
</script>
<body onload='redir();'></body>";
}
?>
<form method="post">
<div class="form-group">
<label for="custName">Customer Name:</label>
<input type="text" name="customer_name" class="form-control" required/>
</div>
<div class="form-group">
<label for="itemName">Item Purchased:</label>
<?php include "database.php";
$result = mysql_query("SELECT ItemID, ItemName FROM Items");
echo "<select name='item_name' class='form-control'>";
while ($row = mysql_fetch_array($result))
{
echo "<option value='" . $row['ItemID'] . "'>". $row['ItemID'] . " - " . $row['ItemName'] ."</option>";
}
echo "</select>";
?>
</div>
<div class="form-group">
<label for="Country">Country:</label>
<select name="country_name" class="form-control">
<option value="Malaysia">Malaysia</option>
<option value="Thailand">Thailand</option>
<option value="Singaore">Singapore</option>
<option value="Phillipines">Phillipines</option>
<option value="Vietnam">Vietnam</option>
<option value="Other">Other</option>
</select>
</div>
<div class="form-group">
<label for="Quantity">Quantity:</label>
<input type="text" name="item_quantity" class="form-control" required/>
</div>
<div class="form-group">
<label for="itemPrice">Total Price (MYR):</label>
<input type="text" name="item_price" class="form-control" required/>
</div>
<div class="form-group">
<label for="dateSold">Date:</label>
<input type='text' name="sales_date" class="form-control" placeholder="dd/mm/yyyy"/>
</div>
<div class="form-group">
<button ng-click="save()" type="submit" name="btn-save" class="btn btn-primary">Save</button>
<button ng-click="cancel()" class="btn btn-primary">Cancel</button>
</div>
My form
ITEM FORM
<form method="POST" action="index.php">
<div class="col-xs-4">ITEM ID<input type="text" name="itemid" class="form-control"/></div>
<div class="col-xs-4">ITEM NAME<input type="text" name="itemname" class="form-control"/></div>
<div class="col-xs-4">ITEM DETAIL<input type="text" name="itemdetail" class="form-control"/></div>
ITEM DESCRIPTION<input type="text" name="itemdescription" class="form- control"/>
<div class="col-xs-4">MANUFACTURER ID <input type="text" name="manufacturerid" class="form-control"/></div>
<div class="col-xs-4">TYPE ID <input type="text" name="typeid" value="4001" class="form-control"/></div>
<div class="col-xs-4">CATEGORY ID <input type="text" value="1003" name="categoryid" class="form-control"/></div>
<div class="col-xs-4">MODULE ID <input type="text" name="moduleid" class="form-control"/></div>
<input type="submit" name="itemSubmit" class="btn btn-default"/>
</form>
<?php echo $_GET['$lastid'] ?>
<table class="table table-hover">
<tbody>
<?php while($row = mysqli_fetch_array($allresult)) { ?>
<tr>
<td><?php echo $row['itemid']?></td>
<td><?php echo $row['itemname']?></td>
<td><?php echo $row['itemdetail']?></td>
<td><?php echo $row['manufacturerid']?></td>
<td><?php echo $row['moduleid']?></td>
</tr>
<?php } ?>
</tbody>
</table>
PRICE FORM
<form method="POST" action="index.php">
<div class="col-xs-4">ITEM ID <input type="text" name="itemid" class="form-control"/></div>
<div class="col-xs-4">SHOP ID <input type="text" name="shopid" class="form-control"/></div>
<div class="col-xs-4">PRICE <input type="text" name="price" class="form-control"/></div>
ITEM URL <input type="text" name="itemurl" class="form-control"/>
ITEM IMAGE <input type="text" name="itemimage" class="form-control"/>
<input type="submit" name="priceSubmit" class="btn btn-default"/>
</form>
My php
<?php
$servername = "localhost";
$username = "abc";
$password = "abc";
$database = "cd";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->mysqli_connect_error) {
die("Connection failed: " . $conn->mysqli_connect_error);
}
echo "Connected successfully";
$query1 = "
INSERT INTO items
( itemid
, itemname
, itemdetail
, itemdescription
, manufacturerid
, typeid
, categoryid
, moduleid
) VALUES
('".$_POST['itemid']."'
,'".$_POST['itemname']."'
,'".$_POST['itemdetail']."'
,'".$_POST['itemdescription']."'
,'".$_POST['manufacturerid']."'
,'".$_POST['typeid']."'
,'".$_POST['categoryid']."'
,'".$_POST['moduleid']."'
)";
$query2 = "SELECT COUNT(itemid) FROM products";
$query3 = "INSERT INTO prices (itemid, shopid, price, itemurl, itemimage VALUES ('".$_POST['itemid']."','".$_POST['shopid']."','".$_POST['price']."','".$_POST['itemurl']."','".$_POST['itemimage']."')";
$query4 = "SELECT * FROM items ORDER BY itemid DESC LIMIT 1";
if(isset($_POST['itemSubmit']))
{
mysqli_query($conn, $query1);
}
else if(isset($_POST['priceSubmit']))
{
mysqli_query($conn, $query3);
}
$lastid = mysqli_query($conn, $query2);
$allresult = mysqli_query($conn, $query4);
?>
Please check am I missing anything? It doesn't show any error and it doesn't insert data into table! Can I not implement two forms in one php file?
Also I tried to add logic that if I press itemSubmit or priceSubmit button then it should run respective query. Another thing is I have created a table and it doesn't display either.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "stack11";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->mysqli_connect_error) {
die("Connection failed: " . $conn->mysqli_connect_error);
}
echo "Connected successfully";
$query1 = "INSERT INTO items (itemid, itemname, itemdetail, itemdescription, manufacturerid, typeid, categoryid, moduleid) VALUES ('".$_POST['itemid']."','".$_POST['itemname']."','".$_POST['itemdetail']."','".$_POST['itemdescription']."','".$_POST['manufacturerid']."','".$_POST['typeid']."','".$_POST['categoryid']."','".$_POST['moduleid']."')";
$query2 = "SELECT * FROM items";
$query3 = "INSERT INTO prices (itemid, shopid, price, itemurl, itemimage) VALUES ('".$_POST['itemid']."','".$_POST['shopid']."','".$_POST['price']."','".$_POST['itemurl']."','".$_POST['itemimage']."')";
$query4 = "SELECT * FROM items ORDER BY itemid DESC LIMIT 1";
if(isset($_POST['itemSubmit']))
{
mysqli_query($conn, $query1);
}
else if(isset($_POST['priceSubmit']))
{
mysqli_query($conn, $query3);
}
$lastid = mysqli_query($conn, $query2);
$allresult = mysqli_query($conn, $query4);
?>
ITEM FORM
<form method="POST" action="#">
<div class="col-xs-4">ITEM ID<input type="text" name="itemid" class="form-control"/></div>
<div class="col-xs-4">ITEM NAME<input type="text" name="itemname" class="form-control"/></div>
<div class="col-xs-4">ITEM DETAIL<input type="text" name="itemdetail" class="form-control"/></div>
ITEM DESCRIPTION<input type="text" name="itemdescription" class="form- control"/>
<div class="col-xs-4">MANUFACTURER ID <input type="text" name="manufacturerid" class="form-control"/></div>
<div class="col-xs-4">TYPE ID <input type="text" name="typeid" value="4001" class="form-control"/></div>
<div class="col-xs-4">CATEGORY ID <input type="text" value="1003" name="categoryid" class="form-control"/></div>
<div class="col-xs-4">MODULE ID <input type="text" name="moduleid" class="form-control"/></div>
<input type="submit" name="itemSubmit" class="btn btn-default"/>
</form>
<?php echo mysqli_num_rows($lastid); ?>
<table class="table table-hover">
<tbody>
<?php while($row = mysqli_fetch_array($allresult)) { ?>
<tr>
<td><?php echo $row['itemid']?></td>
<td><?php echo $row['itemname']?></td>
<td><?php echo $row['itemdetail']?></td>
<td><?php echo $row['manufacturerid']?></td>
<td><?php echo $row['moduleid']?></td>
</tr>
<?php } ?>
</tbody>
</table>
PRICE FORM
<form method="POST" action="#">
<div class="col-xs-4">
ITEM ID
<input type="text" name="itemid" class="form-control"/>
</div>
<div class="col-xs-4">
SHOP ID
<input type="text" name="shopid" class="form-control"/>
</div>
<div class="col-xs-4">
PRICE
<input type="text" name="price" class="form-control"/>
</div>
ITEM URL
<input type="text" name="itemurl" class="form-control"/>
ITEM IMAGE
<input type="text" name="itemimage" class="form-control"/>
<input type="submit" name="priceSubmit" class="btn btn-default"/>
Change $query2 to $query2 = "SELECT * FROM items"; and replace <?php echo $_GET['$lastid'] ?> to <?php echo mysqli_num_rows($lastid); ?>
I am using Unique Key for the field 'name', but when I update the record using edit file through admin, it gives me an error for duplicate entry. I can't remove unique key, it's required..
<!--update process-->
<?php
if(isset($_POST['submit']) and !empty($_POST['token'])){
print_r($_POST);
$name = $_POST['name'];
$size = $_POST['size'];
$linkt = $_POST['link'];
$seeds = $_POST['seeds'];
$leechs = $_POST['leechs'];
$active = $_POST['active'];
$query = "UPDATE tplus_torrentlist SET name = '$name', size = '$size', link = '$linkt', seeds = '$seeds', leechs = '$leechs', active = '$active'";
$result = mysqli_query($link, $query) or die(mysqli_error($link));
$error = mysqli_error($link);
if(empty($error)){
$_SESSION['flash'] = '<blockquote style="background: green; color: #fff">One record updated</blockquote>';
header('location:dashbord.php');
}
else{
$_SESSION['flash'] = '<blockquote style="background: green; color: #fff">Sorry cant updated this record</blockquote>';
header('location:dashbord.php');
}
}
?>
<!--fetch values from database according to id-->
<?php
$sql = "SELECT * FROM tplus_torrentlist WHERE id = $id limit 1";
$result = mysqli_query($link ,$sql) or die(mysqli_error($link));
$row = $result->fetch_array();
?>
<div class="container">
<div class="row clearfix">
<div class="col-md-8 col-md-offset-2">
<?php if(!empty($response)){echo $response;} ?>
<h3>Edit this torrent</h3>
<hr/>
<form role="form" class="form" action="edit.php?id=<?php echo $_GET['id']?>" method="POST">
<label>Name</label>
<input type="text" name="name" value="<?php echo $row['name']?>" class="form-control input-sm">
<label>Size</label>
<input type="text" name="size" value="<?php echo $row['size']?>" class="form-control input-sm">
<label>Link</label>
<input type="text" name="link" value="<?php echo $row['link']?>" class="form-control input-sm">
<label>Seeds</label>
<input type="text" name="seeds" value="<?php echo $row['seeds']?>" class="form-control input-sm">
<label>Leechs</label>
<input type="text" name="leechs" value="<?php echo $row['leechs']?>" class="form-control input-sm">
<label>Active</label>
<select name="active" class="form-control input-sm">
<?php if($row['active'] ==0){?>
<option value="0">Active</option>
<option value="1">InActive</option>
<?php }else{ ?>
<option value="1">InActive</option>
<option value="0">Active</option>
<?php } ?>
</select>
<br/>
<input type="hidden" name="token" value="<?php echo rand(100, 100000)?>">
<input type="submit" name="submit" value="update torrent" class="btn btn-info">
</form>
</div>
</div>
</div>
Any solution without removing unique key from the field 'name' ?
Your update query need to correct as following -
$query = "UPDATE tplus_torrentlist SET name = '$name', size = '$size', link = '$linkt', seeds = '$seeds', leechs = '$leechs', active = '$active' WHERE id = $id limit 1";