how to add queries from 2 diferrent form using php - php

I want to make a cofirm command page, that user after select the produse he cofirm the comand using the CONFIRM FORM, then to redirection him to the cofirm page, where he must write their dates to cofirm the command(the user dates)
So after i have a array in page cart.php
$cofirmaComanda .='<form action="cofirma_comanda.php" method="post">
<input name="prettotal" type="hidden" value="'. $pricetotal .'">
<input name="produseID" type="hidden" value="'. $item_id .'">
<input name="produseNume" type="hidden" value="'. $product_name .'">
<input name="size" type="hidden" value="'. $my_ArraySize .'">
<input name="cantitate" type="hidden" value="' . $each_item['quantity'] .'">
<input name="produse" type="hidden" value="'. $item_id .'">
<input type="submit" name="CofirmaComanda" value="cofirma_comanda"></form>';
then the cofirm page
<?php
session_start(); // Start session first thing in script
// Script Error Reporting
error_reporting(E_ALL);
ini_set('display_errors', '1');
// Connect to the MySQL database
include "storescripts/connect_to_mysql.php";
?>
<?php
$pretTotal = $_POST["prettotal"];
$PRODUSE = $_POST["produseID"];
$produseNume = $_POST["produseNume"];
$size = $_POST["size"];
$cantitate = $_POST["cantitate"];
echo "ID: ".$PRODUSE."<br>";
echo "nume produs selectat: ".$produseNume." <br>";
echo $pretTotal." preata total in LEI<br>";
echo $cantitate." cantitate<br>";
echo $size." dimensiune<br>";
// Filter Function -------------------------------------------------------------------
function filterFunction ($var) {
$var = nl2br(htmlspecialchars($var));
$var = str_replace("/", "\\\\", $var);
$var = preg_replace("~/~", "\\\\", $var);
return $var;
}
$pretTotal = filterFunction($pretTotal);
$PRODUSE = filterFunction($PRODUSE);
$produseNume = filterFunction($produseNume);
$size = filterFunction($size);
$cantitate = filterFunction($cantitate);
if(isset($_SESSION["sumbitDateClienti"])){
$nume = $_POST["nume_client"];
$comanda = 'IDprodus: '.$PRODUSE.' / produseNume: '.$produseNume.' / cantitate: '.$cantitate.' / dimensiune: '.$size.' ';
$stmt = $con->prepare("INSERT comanda (pret_comanda, comanda) VALUES (?, ?)");
// TODO check that $stmt creation succeeded
// "s" means the database expects a string
$stmt->bind_param("ss", $pretTotal, $comanda);
$stmt->execute();
if($stmt->execute()) {
echo "<strong>succes</strong> pagina a fost creata\n titlu: n<a href='admin_index_istorie.php'><strong>Inapoi la pagina de modificare istorie</strong></a>";
}
else {
echo "eroare";
}
$stmt->close();
}
else{
echo "completeaza forum pentru a finaliza comanda";
}
?>
the form on the cofirm page
<form action="" method="post">
nume: <input name="nume_client" type="text">
<input type="hidden" name="prettotal" value="<?php echo $_POST['prettotal'] ?>">
<input type="hidden" name="produseID" value="<?php echo $_POST['produseID']; ?>">
<input type="hidden" name="produseNume" value="<?php echo $_POST['produseNume']; ?>">
<input type="hidden" name="cantitate" value="<?php echo $_POST['cantitate']; ?>">
<input type="hidden" name="size" value="<?php echo $_POST['size']; ?>">
<input type="submit" name="sumbitDateClienti" value="ok">
</form>
I am stucking in this problem because if i put a name on the field is noting doing, doesnt insert the query in the database, and also doesnt give me the else return with the error

Related

How to remove variable from session array according to id PHP

im trying to remove a product from my shopping cart after adding them.
can you please help
this is my code
ADD TO CART
html:
<input type="hidden" name="pid" value="<?php echo $row['product_id']?>
<input type="double" name="weight" placeholder="Weight?" required>
PHP:
session_start();
$pid = $_POST['pid'];
$weight = $_POST['weight'];
$p = "pid";
$p .= (string) $pid;
$w = "weight";
$w .= (string) $pid;
$_SESSION[$p]=$pid;
$_SESSION[$w] = $weight;
if(isset($_SESSION)){
header('location:../shopping-cart.php');
}
after execution the product is added.
Display in Cart
foreach($_SESSION as $key => $values){
if(strpos($key, "pid") !== false){
$sql="select * from products where product_id = $_SESSION[$key]";
$result=$connect->query($sql);
$r = mysqli_fetch_assoc($result);
$w = "weight";
$w .= (string) $r['product_id'];
$weight = $_SESSION[$w];
HTML PHP CODE
<?php echo $r["product_name"];?>
<?php echo 'LBP '.number_format($r['product_price'],0)?>
<input type="hidden" id="unitprice" onchange="f();" value="<?php echo $r['product_price']?>">
<input type="text" onchange="f();" id="quantity" value="<?php echo $weight ?>">
<input type="submit" value="Delete">
<input type="hidden" name="pid" value="<?php echo $r['product_id'] ?>">
<input type="hidden" name="pweight" value="<?php echo $weight ?>">
remove from cart is not working
Any Help?
session_start();
$pidremove = $_POST['pid'];
$weightremove = $_POST['pweight'];
foreach($_SESSION as $key => $values){
if($_SESSION[$w]==$pidremove)
{
unset($_SESSION[$w]);
unset($_SESSION[$p]);
echo 'done';
}
}
HTML:
<input type="hidden" name="pid" value="<?php echo $row['product_id']; ?>" />
<input type="submit" name="remove" value="Remove">
PHP:
unset($_SESSION['pid' . $_POST['pid']]
unset($_SESSION['weight' . $_POST['pid']]

How to create a selection dropdown in php form

I am working on a project and would like to give the user per-determined values when updating a record.
Here is my code so far.
<?php
// if there are any errors, display them
if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>
<form action="" method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
<div>
<p><strong>ID:</strong> <?php echo $id; ?></p>
<strong>School Name:</strong> <input type="text" name="Name" value="<?php echo $Name; ?>"/><br/><br>
<strong>Status:</strong> <input type="text" name="Status" value="<?php echo $Status; ?>"/><br/><br>
<strong>Comments:</strong> <input type="text" name="Comments" value="<?php echo $Comments; ?>"/><br/><br>
<strong>Type:</strong> <input type="text" name="Type" value="<?php echo $Type; ?>"/><br/><br>
<input type="submit" name="submit" value="Submit">
</div>
</form>
</body>
</html>
<?php
}
// connect to the database
include('connect-db.php');
// check if the form has been submitted. If it has, process the form and save it to the database
if (isset($_POST['submit']))
{
// confirm that the 'id' value is a valid integer before getting the form data
if (is_numeric($_POST['id']))
{
// get form data, making sure it is valid
$id = $_POST['id'];
$Name = mysql_real_escape_string(htmlspecialchars($_POST['Name']));
$Status = mysql_real_escape_string(htmlspecialchars($_POST['Status']));
$Comments = mysql_real_escape_string(htmlspecialchars($_POST['Comments']));
$Type = mysql_real_escape_string(htmlspecialchars($_POST['Type']));
// check that firstname/lastname fields are both filled in
if ($Name == '' || $Type == '')
{
// generate error message
$error = 'ERROR: Please fill in all required fields!';
//error, display form
renderForm($id, $Name, $Status, $Comments, $Type, $error);
}
else
{
// save the data to the database
mysql_query("UPDATE Schools SET Name='$Name', Status='$Status', Comments='$Comments', Type='$Type' WHERE id='$id'")
or die(mysql_error());
// once saved, redirect back to the view page
header("Location: view.php");
}
}
else
{
// if the 'id' isn't valid, display an error
echo 'Error!';
}
}
else
// if the form hasn't been submitted, get the data from the db and display the form
{
// get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0)
if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)
{
// query db
$id = $_GET['id'];
$result = mysql_query("SELECT * FROM Schools WHERE id=$id")
or die(mysql_error());
$row = mysql_fetch_array($result);
// check that the 'id' matches up with a row in the databse
if($row)
{
// get data from db
$Name = $row['Name'];
$Status = $row['Status'];
$Comments = $row['Comments'];
$Type = $row['Type'];
// show form
renderForm($id, $Name, $Status, $Comments, $Type, '');
}
else
// if no match, display result
{
echo "No results!";
}
}
else
// if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error
{
echo 'Error!';
}
}
?>
I am wanting to replace the status text filed with a drop down list of options.
Replace your <input by <select :
<form action="" method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
<div>
<p><strong>ID:</strong> <?php echo $id; ?></p>
<strong>School Name:</strong> <input type="text" name="Name" value="<?php echo $Name; ?>"/><br/><br>
<!-- <strong>Status:</strong> <input type="text" name="Status" value="<?php echo $Status; ?>"/><br/><br>-->
<strong>Status:</strong> <select name="Status">
<option value="1">Status 1</option>
<option value="2">Status 2</option>
</select>
<strong>Comments:</strong> <input type="text" name="Comments" value="<?php echo $Comments; ?>"/><br/><br>
<strong>Type:</strong> <input type="text" name="Type" value="<?php echo $Type; ?>"/><br/><br>
<input type="submit" name="submit" value="Submit">
</div>
</form>
If your statuses are in a table, fill the <select> with a query :
<form action="" method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
<div>
<p><strong>ID:</strong> <?php echo $id; ?></p>
<strong>School Name:</strong> <input type="text" name="Name" value="<?php echo $Name; ?>"/><br/><br>
<!-- <strong>Status:</strong> <input type="text" name="Status" value="<?php echo $Status; ?>"/><br/><br>-->
<strong>Status:</strong> <select name="Status">
<?php
$result = mysql_query("SELECT * FROM tbl_status",$cnx);
while ( $row = mysql_fetch_array($result) )
echo "<option value='" . $row["id"] . "'>" . $row["text"] . "</option>";
?>
</select>
<strong>Comments:</strong> <input type="text" name="Comments" value="<?php echo $Comments; ?>"/><br/><br>
<strong>Type:</strong> <input type="text" name="Type" value="<?php echo $Type; ?>"/><br/><br>
<input type="submit" name="submit" value="Submit">
</div>
</form>
You could use the html <datalist> or the <select> tag.
I hope I could help.
First of all you need to switch from mysql_* to mysqli_* as it going to get removed in php 7.0 I'm using this function i created and it might help you
here is the php code
function GetOptions($request)
{
global $con;
$sql = "SELECT * FROM data GROUP BY $request ORDER BY $request";
$sql_result = mysqli_query($con, $sql) or die('request "Could not execute SQL query" ' . $sql);
while ($row = mysqli_fetch_assoc($sql_result)) {
echo "<option value='" . $row["$request"] . "'" . ($row["$request"] == $_REQUEST["$request"] ? " selected" : "") . ">" . $row["$request"] . "$x</option>";
}
}
and the html code goes like
<label>genre</label>
<select name="genre">
<option value="all">all</option>
<?php
GetOptions("genre");
?>
</select>

losing variable value as script executes PHP/MySQL

My GET value is fine on first if statement but when it hits update_submit I get an undefined variable error. This is still test script so there's no validation
<?php
session_start();
include "connect.php";
error_reporting(E_ALL);
ini_set('display_errors','1');
if(isset($_GET['pid']))
{
$get_item = $_GET['pid'];
echo "pid" . $get_item;//<--has value here
$get_products = $db->prepare("select * from `item` where
`item_id` = '$get_item' LIMIT 1");
$get_products->execute();
while ($row = $get_products->fetch())
{
$user_id = $row['user_id'];
$item_name = $row['item_name'];
$item_description = $row['item_description'];
$image = $row['photopath'];
}
}
echo "pid" .$get_item;//<---has value here
if(isset($_POST['cancel_edit']))
{
header("Location: manage_items.php");
exit();
}
if(isset($_POST['update_submit']))//<<---lose it here
{
$get_products = $db->prepare("select `photopath` from `item` where
`item_id` = '$get_item' LIMIT 1");
$get_products->execute();
$path= $get_products->fetchColumn(6);
FORM
<form action="item_edit.php" method="post" enctype="multipart/form-data">
<input type = "text" name="item_name" value="<?php echo $item_name ?> "/>
<textarea name="item_description"><?php echo $item_description ?></textarea>
<p> <img src="<?php echo $image; ?>" width="75" height="75" /></p>
<input type="file" name="image_edit" value="<?php echo $image ?>"/>
<input name="img_edit" type="hidden" value="<?php echo $image ?>"/>
<input name="edit_form_id" type="hidden" value="<?php echo $get_item ?>">
<p><input type="submit" name="update_submit" value="Update"/></p>
<p><input type="submit" name="cancel_edit" value="Cancel"/></p>
</form>
If coming from the same form, are you mixing $_GET and $_POST settings for your form? You have the value when using ($_GET['pid']), but not when using ($_POST['update_submit']).

How to echo mysql results to a text field

this script displays data from a specific email address which the user enters.
the snippet of code below displays the data in a textfield at the top of the page however I want to display the data in a textfield in the body of text.
echo '<input name="login" type="text" value="' . $result['name'] . '>';
What do I change in the above code to enable me to do this.
<?php
$host=""; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name="orders"; // Table name
$email = $_POST['textfield'];
$db = new PDO('mysql:host='.$host.
';dbname='.$db_name.
';charset=UTF-8',
$username, $password);
$stmt = $db->prepare('SELECT * FROM `orders` WHERE `email`=:email LIMIT 1');
$stmt->bindValue(':email', $email, PDO::PARAM_STR);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
if($stmt->rowCount()>0)
{
echo '<input name="login" type="text" value="' . $result['name'] . '>';
}
else
{
echo "Email not found in the database!";
}
?>
form:
<form id="form_53" name="login" action="test.php">
<input type="submit" value="Track">
<input type="text" username="textfield" value="">
<input type="text" name="name" value="<?php echo $result['name']?>"> //I want to display results here
</form>
if both the code exists in the same file.. this will work
<input type="text" name="name" value="<?php echo $result['name']?>">
and if you want to check if there are rows returned you can use
<input type="text" name="name" value="<?php echo ($stmt->rowCount()>0) ? $result['name'] : "" ?>">
edited the code so that you know what exactly you want to do
First: Replace this code
if($stmt->rowCount()>0)
{
echo '<input name="login" type="text" value="' . $result['name'] . '>';
}
else
{
echo "Email not found in the database!";
}
just keep $result = $stmt->fetch(PDO::FETCH_ASSOC); and
if($stmt->rowCount()<=0) echo "Email not found in the database!";
Second: now in HTML section
<input type="text" name="name" value="<?php echo ($stmt->rowCount()>0) ? $result['name'] : "" ?>">
As long as you don't clobber $result you can do:
<input type="text" name="name" value="<?php echo $result['name']; ?>"> //I want to display results here
echo '<input name="login" type="text" value="' . $result['name'] . '">';
Just add a double quote to the echo'd string.
It's pretty easy to accomplish. Of course make sure that the $results array is included in the session for the desired HTML page that you are working on.
<form id="form_53" name="login" action="test.php">
<input type="submit" value="Track">
<input type="text" username="textfield" value="">
<input type="text" name="name" value="<?php echo $results['name']?>"> //I want to display results here
</form>

PHP check if array elements are 'undefined' and if so, set them to zero (0)?

I am creating an array like this:
function imageSize($name, $nr, $category){
$path = 'ad_images/'.$category.'/'.$name.'.jpg';
$path_thumb = 'ad_images/'.$category.'/thumbs/'.$name.'.jpg';
list($width, $height) = getimagesize($path);
list($thumb_width, $thumb_height) = getimagesize($path_thumb);
$myarr = array();
$myarr['thumb_image_' . $nr . '_width'] = $thumb_width;
$myarr['thumb_image_' . $nr . '_height'] = $thumb_height;
$myarr['image_' . $nr . '_width'] = $width;
$myarr['image_' . $nr . '_height'] = $height;
return $myarr;
}
I have hidden inputs on my page which values are taken from the array like this:
(FIRST)
<input type="hidden" id="img1_width" value="<?php echo $img_array['image_1_width'];?>" />
<input type="hidden" id="img1_height" value="<?php echo $img_array['image_1_height'];?>" />
<input type="hidden" id="img1_th_width" value="<?php echo $img_array['thumb_image_1_width'];?>" />
<input type="hidden" id="img1_th_height" value="<?php echo $img_array['thumb_image_1_height'];?>" />
(SECOND)
<input type="hidden" id="img2_width" value="<?php echo $img_array['image_2_width'];?>" />
<input type="hidden" id="img2_height" value="<?php echo $img_array['image_2_height'];?>" />
<input type="hidden" id="img2_th_width" value="<?php echo $img_array['thumb_image_2_width'];?>" />
<input type="hidden" id="img2_th_height" value="<?php echo $img_array['thumb_image_2_height'];?>" />
Now, if there is only 1 image, then the array will only be called ONCE, and the inputs which calls the second image (all inputs with 'id' = 'img2 etc') will have a value of undefined variable
My Q is this:
Is there any way I can check the array length and set the remaining values all to '0' (zero) if they are not set?
Thanks
What if you loop through them...
<?php for ($i=0;$i<count($array);$i++): ?>
<input type="hidden" id="img{$i}_width" value="<?php echo $img_array["image_{$i}_width"];?>" />
<input type="hidden" id="img{$i}_height" value="<?php echo $img_array["image_{$i}_height"];?>" />
<input type="hidden" id="img{$i}_th_width" value="<?php echo $img_array["thumb_image_{$i}_width"];?>" />
<input type="hidden" id="img{$i}_th_height" value="<?php echo $img_array["thumb_image_{$i}_height"];?>" />
<?php endfor; ?>
You could parse the value to an integer so it would become 0 if it's undefined ...

Categories