losing variable value as script executes PHP/MySQL - php

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']).

Related

Old values not appearing in text field when called

I'm trying to call the old values to be edited. What part am I wrong at?
<?php
if (isset($_GET['edit'])) {
$id = $_GET['edit'];
$update = true;
$record = mysqli_query($db, "SELECT * FROM bookinfo WHERE BookNo='$BookNo'");
if (mysqli_num_rows($record) == 1 ) {
$n = mysqli_fetch_array($record);
$BookNo = $n['BookNo'];
$ISBN = $n['ISBN'];
$title = $n['title'];
$author = $n['author'];
$publisher = $n['publisher'];
$status = $n['status'];
$cost = $n['cost'];
}
}
?>
<a href="viewBook.php?edit=<?php echo $row['BookNo']; ?>" class="edit_btn" >Edit</a>
</td>
<?php
if (isset($_GET['edit'])) { ?>
<form method="post" action = "viewBook.php">
<input type="hidden" name="BookNo" value="<?php echo $BookNo; ?>">
<input type="text" name="ISBN" value="<?php echo $ISBN; ?>">
<input type="text" name="title" value="<?php echo $title; ?>">
<input type="text" name="author" value="<?php echo $author; ?>">
<input type="text" name="publisher" value="<?php echo $publisher; ?>">
<input type="text" name="status" value="<?php echo $status; ?>">
<input type="text" name="cost" value="<?php echo $cost; ?>">
<?php if ($update == true): ?>
<button class="btn" type="submit" name="update" style="background: #556B2F;" >update</button>
<?php else: ?>
<button class="btn" type="submit" name="save" >Save</button>
<?php endif ?>
<?php } ?>
</form>
So far, what it does is, when the user clicks the edit button, it just shows 6 text fields. I thought by doing what I did, it was supposed to show the details already filled in the textbox.
When you do
$record = mysqli_query($db, "SELECT * FROM bookinfo WHERE BookNo='$BookNo'");
$BookNo is not defined.
maybe you wanted to do something like this:
$id = $_GET['edit'];
$update = true;
$record = mysqli_query($db, "SELECT * FROM bookinfo WHERE BookNo='$id'");
<form method="post" action = "viewBook.php">
your form method is "post" but you are checking $_GET You must check $_POST
if (isset($_GET['edit']))
you are passing value in $id And using $BookNo which not define.
only 6 input field will be show because first one is using hidden property.
<input type="hidden" name="BookNo" value="<?php echo $BookNo; ?>">
when you click on submit button data will be receive by $_POST

How to add caption field for every image preview and post all caption through one submit button

This is my code I want to add one input field for every image preview and save it to db.. the field is coming but I'm not getting any data.. can anyone suggest how can I post them???
$fetch_imgid=$con->prepare("SELECT * FROM attempt010 where link='$rand'");
$fetch_imgid->setFetchMode(PDO:: FETCH_ASSOC);
$fetch_imgid->execute();
?>
<ul class="reorder_ul reorder-photos-list" id="previewImg">
<?php
while($row = $fetch_imgid->fetch()):
$delid = $row['id'];
//echo $row['id'].' '.$row['name'].'<br/>';?>
<li id="image_li_<?php echo $row['id']; ?>" class="ui-sortable-handle" data-image-id="<?php echo $delid; ?>">
<img src="uploads/<?php echo $row['name']; ?>" alt="">
<input type="submit" class="del_btn" value="Delete Image" />
<input type="text" id="cap" name="cap[]" placeholder="Enter Caption Here" />
<input type="hidden" id="cap_id" value="<?php echo $row['id']; ?>" />
<?php
endWhile;
?>
</ul>
<input type="submit" value="Add Caption" name="addcap" /> <?php include('addcap.php'); ?>
and this is addcap.php
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
if(isset($_POST['addcap'])){
foreach($_POST['cap'])
{
$imgcap = $_POST['cap'];
if($imgcap!=empty())
{
try
{
$con=new PDO("mysql:host=localhost;dbname=newimg","root","");
$sql=$con->prepare("UPDATE attempt010 SET caption='$imgcap' WHERE id='$cap_id'");
$sql->execute();
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
}
}
}
?>
<input type="hidden" id="cap_id" value="<?php echo $row['id']; ?>" />
Must have unique id. You can't send multiple fields with same id. You will get only last one.
For example:
$fetch_imgid=$con->prepare("SELECT * FROM attempt010 where link='$rand'");
$fetch_imgid->setFetchMode(PDO:: FETCH_ASSOC);
$fetch_imgid->execute();
?>
<form action="addcap.php" method="post">
<ul class="reorder_ul reorder-photos-list" id="previewImg">
<?php
$id_array="";
while($row = $fetch_imgid->fetch()):
$id_array = $id_array.$row['id'].",";
$delid = $row['id'];
//echo $row['id'].' '.$row['name'].'<br/>';?>
<li id="image_li_<?php echo $row['id']; ?>" class="ui-sortable-handle" data-image-id="<?php echo $delid; ?>">
<img src="uploads/<?php echo $row['name']; ?>" alt="">
<input type="text" id="cap_<?php echo $row['id']; ?>" placeholder="Enter Caption Here" />
<?php
endWhile;
$id_array = substr($id_array, 0, -1);
?>
<input type="hidden" id="cap_ids" value="<?php echo $id_array ; ?>" />
</ul>
<input type="submit" value="Add Caption" name="addcap" />
</form>
<!--addcap.php-->
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
if(isset($_POST['addcap'])){
if(isset($_POST['cap_ids'])){
$ids_array = explode(",", $_POST['cap_ids']);
foreach($ids_array as $ids)
{
$idcap = 'cap_'.$ids;
$imgcap = $_POST[$idcap];
if($imgcap!=empty())
{
try
{
$con=new
PDO("mysql:host=localhost;dbname=newimg","root","");
$query = "UPDATE attempt010 SET
caption='$imgcap' WHERE id='$ids'";
echo $query;
$sql=$con->prepare($query);
$sql->execute();
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
}
}
}
}
?>
This code looks like it can't work. Because you have submit and form handling code in same page. The idea behind form is to post data to different page(set in form action) and this page will do something with this data and display results to the user. For your example to work make form in your first file like:
<form action="addcap.php">
<inputs here>
</form>
Nowadays it is common that database operations are done asynchronic on server side, when user can continue using the page/app.
So learn how to use jQuery and AJAX. Maybe nodeJS or other new stuff.

how to add queries from 2 diferrent form using 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

trying to edit SQL via php but keep getting an error

as far as i can see my code is sound however, I keep getting an error
this is the error
Notice: Undefined variable: person in
\sql\modify.php on line 12
here is my code..
<?php
include 'includes/connection.php';
if (!isset($_POST['submit'])){
$q = "SELECT * FROM people WHERE ID = $_GET[id]";
$result = mysql_query($q);
$person = mysql_fetch_array($result);
}
?>
<h1>You are modifying A User</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Name<input type="text" name="inputName" value="<?php echo $person['Name']; ?>" /><br />
Description<input type="text" name="inputDesc" value="<?php echo $person['Description']; ?>" />
<br />
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
<input type="submit" name="submit" value="Modify"/>
</form>
<?php
if(isset($_POST['sumbmit'])) {
$u = "UPDATE people SET `Name` = '$_POST[inputName]', `Description` = '$_POST[inputDesc]' WHERE ID = $_POST[id]";
mysql_query($u) or die(mysql_error());
echo "User has been modify";
header("Location: index.php");
}
?>
any Thoughts or am im I just blind???
<?php
include 'includes/connection.php';
// set $person veriable
if (!isset($_POST['submit'])){
$q = "SELECT * FROM people WHERE ID = $_GET[id]";
$result = mysql_query($q);
$person = mysql_fetch_array($result);
}
// if form submit you use update and redirect
else {
$u = "UPDATE people SET `Name` = '$_POST[inputName]', `Description` = '$_POST[inputDesc]' WHERE ID = $_POST[id]";
mysql_query($u) or die(mysql_error());
//echo "User has been modify"; // this not need, bcz execute header('location') redirect you current page
header("Location: index.php");
exit(); //use it after header location
}
?>
<h1>You are modifying A User</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Name<input type="text" name="inputName" value="<?php echo $person['Name']; ?>" /><br />
Description<input type="text" name="inputDesc" value="<?php echo $person['Description']; ?>" />
<br />
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
<input type="submit" name="submit" value="Modify"/>
</form>
You just need to check if you actually got output.
Just a plain example:
if ($person):
?>
<h1>You are modifying A User</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Name<input type="text" name="inputName" value="<?php echo $person['Name']; ?>" /><br />
Description<input type="text" name="inputDesc" value="<?php echo $person['Description']; ?>" />
<br />
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
<input type="submit" name="submit" value="Modify"/>
</form>
<?php
endif;
Remember to always validate your input and output, but also if queries you try to run do produce a result set.
Here's the issue:
<?php
include 'includes/connection.php';
if (!isset($_POST['submit'])){
$q = "SELECT * FROM people WHERE ID = $_GET[id]";
$result = mysql_query($q);
$person = mysql_fetch_array($result);
}
?>
At this point $person is set only if the form hasn't been submitted; but later on:
Name<input type="text" name="inputName" value="<?php echo $person['Name']; ?>" /><br />
Description<input type="text" name="inputDesc" value="<?php echo $person['Description']; ?>" />
You're using it anyway. If the form has been submitted, then you're going to get the warning you're seeing. What you need to do is something like:
if (isset($_POST['submit'])){
$name = $_POST['name'];
$description = $_POST['description']
} else {
$q = "SELECT * FROM people WHERE ID = $_GET[id]";
$result = mysql_query($q);
$person = mysql_fetch_array($result);
$name = $person['name'];
$description= $person['description'];
}
And then:
Name<input type="text" name="inputName" value="<?php echo $name ?>" /><br />
Description<input type="text" name="inputDesc" value="<?php echo $description; ?>" />
The variables are now set either way.
A couple of other things - you're not doing any error checking to see if your query has worked; if the query fails, your code will carry on regardless.
Secondly, the mysql_ functions are deprecated and will stop working at some point; you should look at moving to using mysqli_* or PDO instead.

PHP Form update will not update

I have written an Edit part of my Form but somehow it will not Update the edited Fields. The Code does not give any Errors but it will not update?!
If possible could somebody take a look please?
<?php
include "connect.php";//database connection
if (isset($_GET["id"])) {
$id = intval($_GET["id"]);
if (isset($_POST["edited"]))
{
$update = "UPDATE traumprojekt SET";
$update .= sprintf("quantityProduct='%s', " , mysql_real_escape_string($_POST["quantityProduct"]));
$update .= sprintf("titleProduct='%s', " , mysql_real_escape_string($_POST["titleProduct"]));
$update .= sprintf("informationProduct='%s'", mysql_real_escape_string($_POST["informationProduct"]));
$update .= "WHERE id = '$id'";
mysql_query($update);
}
$sql = "SELECT * FROM `traumprojekt` WHERE id=$id";
$res = mysql_query($sql) or die (mysql_error());
if (mysql_num_rows($res) == 1)
{
$row = mysql_fetch_assoc($res);
?>
<form method="post" action="edit_form.php?id=<?php echo $row["id"] ?>">
ID: <?php echo $row["id"] ?><br />
Quantity: <input type="text" name="quantityProduct" value="<?php echo $row["quantityProduct"] ?>"><br />
Product Title: <input type="text" name="titleProduct" value="<?php echo $row["titleProduct"] ?>"><br />
Product Information: <input type="text" name="informationProduct" value="<?php echo $row["informationProduct"] ?>"><br />
<input type="submit" name="submit" value="Update"><br />
<input type="hidden" name="edited" value="1">
</form>
<?php
}
}
?>

Categories