Error while editing and saving the data in PHP - php

When I delete the values it works well. When I Search it works well.
Only when I try to save the data after edit I get an error.
I also tried to use
if(!isset($_GET['edit']))
{
echo '<form action="" method="post">';
echo '<input type="text" name=criteria>';
echo '<input type="submit" value="search" name="submit">';
echo '</form>';
}
But it did not work.
The error I receive is ( ! ) SCREAM: Error suppression ignored for
( ! ) Notice: Undefined index: criteria in C:\wamp\www\test\search.php on line 13.
The entire code is
<html>
<head>
</head>
<body>
<?php
$page='search.php';
mysql_connect("localhost","root","") or die (mysql_error());
mysql_select_db("list") or die (mysql_error());
if(empty($_POST) === false)
{
$data=$_POST['criteria'];
if (empty($data) === true)
{
echo 'Please enter some text!!<br/>';
}
else
{
$get=mysql_query("SELECT SRNO, fname, lname, phone, email, address, comments from names where fname='" . mysql_real_escape_string($data) . "'");
if (mysql_num_rows($get)==0)
{
echo 'There are no search results!!';
}
else
{
echo '<table border=0 cellspacing=25 cellpadding=1>';
echo'<tr><th>Sr. No</th><th>First Name</th><th>Last Name</th><th>Phone No</th><th>E-mail</th><th>Address</th><th>Comments!!</th><th>Modify</th><th>Delete!</th></tr>';
while($get_row=mysql_fetch_assoc($get))
{
echo '<tr><td>'.$get_row['SRNO'].'</td><td>'.$get_row['fname'].'</td><td>'.$get_row['lname'].'</td><td>'.$get_row['phone'].'</td><td>'.$get_row['email'].'</td><td>'.$get_row['address'].'</td><td>'.$get_row['comments'].'</td><td>Edit</td><td>Delete</td></tr>';
}
echo '</table>';
}
}
/*
if(mysql_num_rows($getf) == 0)
{
$getel=mysql_query('SELECT SRNO, fname, lname, phone, email, address, comments from names where lname='.$_GET['$data']));
}*/
}
if(isset($_GET['delete']))
{
mysql_query('DELETE from names where SRNO='.mysql_real_escape_string((int)$_GET['delete']));
}
if(isset($_GET['edit']))
{
$getedit=mysql_query('SELECT SRNO, fname, lname, phone, email, address, comments from names where SRNO='.mysql_real_escape_string((int)$_GET['edit']));
echo '<table border=0>';
while ($get_row=mysql_fetch_assoc($getedit))
{
echo '<form method="POST" action="">';
echo '<tr><td>Sr.No:</td><td><input type="text" value='.$get_row['SRNO'].' name="SRNO" readonly="readonly"></td></tr>';
echo '<tr><td>First Name:</td><td><input type="text" value='.$get_row['fname'].' name="fname"></td></tr>';
echo '<tr><td>Last Name:</td><td><input type="text" value='.$get_row['lname'].' name="lname"></td></tr>';
echo '<tr><td>Phone No:</td><td><input type="text" value='.$get_row['phone'].' name="phone"></td></tr>';
echo '<tr><td>E-mail address:</td><td><input type="text" value='.$get_row['email'].' name="email"</td></tr>';
echo '<tr><td>Address:</td><td><textarea name="address" rows=4>'.$get_row['address'].'</textarea></td></tr>';
echo '<tr><td>Comments:</td><td><textarea name="comments" rows=4>'.$get_row['comments'].'</textarea></td></tr>';
echo '<tr><td><input type="submit" name="submit" value="save"></td><td>Cancel</td></tr>';
echo '</form>';
}
echo '</table>';
}
if(!isset($_GET['edit']))
{
echo '<form action="" method="post">';
echo '<input type="text" name=criteria>';
echo '<input type="submit" value="search" name="submit">';
echo '</form>';
}
?>
</body>
</html>
There are two forms and just one
if(empty($_POST) === false)
how do I add a condition in if statement to filter each one?
Please help

Try putting double quotes around the name on your input :
<input type="text" name="criteria">

Related

php: getting a variable (user selection from radio button) to another file

I have a list of items in one file (fruit.php):
<form action="delete_items.php" method="post">
<input type="radio" value="apple" name="fruit">Apple<br>
<input type="radio" value="pear" name="fruit">Pear<br>
<input type="radio" value="banana" name="fruit">Banana<br>
<input type="submit" value="delete" name="deleteButton"><br>
</form>
In the delete_items.php, I have a variable that stores the selection:
<?php
$selection = $_POST["fruit"];
echo "are you sure you want to delete it?";
echo '<form action="delete_confirmation.php" method="post"><br>';
echo '<input type="submit" name="deleteYes" value="Yes"><br>';
echo '<input type="submit" name="deleteNo" value="No"><br>';
echo '</form>';
?>
Than in the confirmation file (delete_confirmation.php):
<?php
include 'delete_items.php';
if($_POST["deleteYes"]){
$query = 'DELETE FROM databaseName WHERE fruitName="' . $selection . '"';
$result = mysqli_query($connection, $query);
if(!$result){
die("Delete after check query failed!");
} else {
echo "Delete after check query success!";
}
} elseif($_POST["deleteNo"]){
echo "The course was not deleted!";
}
?>
But the varibale $selection in the delete_confirmation.php file is always null even though i included the delete_items.php file (when i echo it, nothing is shown). Is there a way to pass the selection variable from delete_items.php to the delete_confirmation.php file?

$_SESSION variables "disappear" after form is submitted

I stored values from a form in SESSION variables but it seems that they are emptied after if(isset($_POST['submit']) even though I can echo them earlier in my code. The code runs and the insert works except for $toto and $tata which are empty.
In the code below _id, master_label, client_label are inserted correctly. And the target table fields match so the error does not come from there.
<?php
session_start();
[...]
echo "<form id='organization' action='?' method='POST' enctype='multipart/form-data' >" ;
echo "<select name='organization' onchange='this.form.submit()'>"; //
echo '<option value="">Select ORGANIZATION</option>';
while ($row=mysqli_fetch_array($result00)) {
echo '<option value="'.$row['company_id'].'">'.$row['company_id'].'</option>';
}
echo "</select>";
echo "</form>";
$cie_ticker = $_POST['organization'] ;
$_SESSION['company_id'] = $cie_ticker ;
$sql="(SELECT company_id, databasefile, company_name FROM `settings` WHERE company_id='$cie_ticker') LIMIT 1";
$result=mysqli_query($connect,$sql) or die('Could not execute the query ' . mysqli_error()) ;
$data_bis=$result->fetch_array() ;
$_SESSION['selected_cie_id'] = $data_bis['company_id'];
$_SESSION['selected_cie_name'] = $data_bis['company_name'];
<form id='demoForm' action="?" method="POST" enctype="multipart/form-data" target="_top">
<?php
$file = fopen('./company/'.$cie_ticker.'/'.$_SESSION['selected_cie_db'], "r") ; // $file = fopen('./company/'.$_SESSION["company_id"].'/'.$_SESSION['selected_cie_db'], "r") ;
$headers=fgetcsv($file, 1000, "|");
fclose($file);
?>
<table id="user_table">
<tr><th>HEADERS </th><th>MASTER DB HEADERS</th></tr>
<?php foreach ($headers as $entete) {
$query01="SELECT master_label FROM `mastertable`";
$result01=mysqli_query($connect,$query01); ?>
<tr id="<?php echo $entete;?>">
<td> <?php echo $entete ?></td>
<td id="<?php echo $row['_id'];?>">
<?php
echo "<br>";
echo "<select name=".$entete.">";
echo '<option value="MASTER DATA">Select MASTER DATA</option>';
while ($row=mysqli_fetch_array($result01)) {
echo '<option value="'.$row['master_label'].'">'.$row['master_label'].'</option>';
}
echo "</select>";
?>
</td>
</tr>
<?php } ?>
</table>
<input id="checkBtn" type="submit" name="submitbutton" value="SAVE" class="button"/>
</form>
if(isset($_POST['submitbutton'])) {
$toto = $data_bis['company_id'] ;
$tata = $data_bis['company_name'];
$selected=$_POST;
foreach($selected as $x => $x_value) {
$query= $connect->prepare("INSERT INTO `mytable` (_id, master_label, client_label, org_label, client_id) VALUES ('',?,?,?,?) ");
$query->bind_param('ssss',$x_value, $x, $toto, $tata );
$query->execute();
}
}
?>
I have found the answer:
In this case one must add hidden input for the $_SESSION variables otherwise they were lost after submitting the form.

$_POST not running for the form inside PHP script

I was making a form inside a PHP script and when i tried to access a field
of form in the same script it was giving me error Undefined index
This is my code:
<?php
$query="SELECT * FROM `vendor_list`";
$query_run=mysql_query($query);
if(mysql_num_rows($query_run)>0)
{
echo '<form method="POST">';
echo 'Book name:<input type="text" name="book_name" size="40" maxlength="40"><br><br>';
echo 'Number Of Copies:<input type="text" size="5" name="num_copies" maxlength="2"><br><br>';
echo '<select name="vendor_email">';
while($query_row=mysql_fetch_assoc($query_run))
{
$laser=$query_row['vendor_name'];
$email=$query_row['email'];
echo "<option value='".$email."'>".$laser."</option>";
}
echo '</select><br>';
echo '<input type="submit" value="submit">';
echo '</form>';
}
else
{
echo 'No vendor to display';
}
echo $_POST['book_name'];
USE THIS:-
<?php
$query="SELECT * FROM `vendor_list`";
$query_run=mysql_query($query);
if(mysql_num_rows($query_run)>0)
{
echo '<form method="POST">';
echo 'Book name:<input type="text" name="book_name" size="40" maxlength="40"><br><br>';
echo 'Number Of Copies:<input type="text" size="5" name="num_copies" maxlength="2"><br><br>';
echo '<select name="vendor_email">';
while($query_row=mysql_fetch_assoc($query_run))
{
$laser=$query_row['vendor_name'];
$email=$query_row['email'];
echo "<option value='".$email."'>".$laser."</option>";
}
echo '</select><br>';
echo '<input type="submit" value="submit">';
echo '</form>';
}
else
{
echo 'No vendor to display';
}
if($_POST){
echo $_POST['book_name'];
}
?>
Try this
<?php
$query="SELECT * FROM `vendor_list`";
$query_run=mysql_query($query);
if(mysql_num_rows($query_run)>0)
{
?>
<form method="POST" action="">
Book name:<input type="text" name="book_name" size="40" maxlength="40"><br><br>
Number Of Copies:<input type="text" size="5" name="num_copies" maxlength="2"><br><br>
<select name="vendor_email">
<?php
while($query_row=mysql_fetch_assoc($query_run))
{
$laser=$query_row['vendor_name'];
$email=$query_row['email'];
echo "<option value='".$email."'>".$laser."</option>";
}
?>
</select><br>
<input type="submit" name="submit" value="submit">
</form>
<?php
}
else
{
echo 'No vendor to display';
}
if(isset($_POST["submit"]))){
echo $_POST['book_name'];
}
?>
The Clean Way
All the breaks you have in that code are a hot mess...
Tip When you are echoing too much HTML just wrap it in an if..else.. like this...
<?php
$query = "SELECT * FROM `vendor_list`";
$query_run = mysql_query($query);
?>
<?php if(mysql_num_rows($query_run)>0): ?>
<form method="POST">
<label for="book_name">Book name:</label>
<input type="text" name="book_name" id="book_name" size="40" maxlength="40">
<label for="book_name"Number Of Copies:</label>
<input type="text" size="5" name="num_copies" maxlength="2">
<select name="vendor_email">
<?php while($query_row=mysql_fetch_assoc($query_run)): ?>
<option value="<?=$query_row['email']?>"><?=$query_row['vendor_name']?></option>
<?php endwhile; ?>
</select>
<input type="submit" value="submit">
</form>
<?php else: ?>
<p>No vendor to display</p>
<?php endif; ?>
<?php if (isset($_POST['book_name'])) echo $_POST['book_name']; ?>
Using the below code this will also check if you have a form submission or not, unless this code is a cut/copy paste.. It looks like it will process sections when it should not.. eg the echo post..
Personally i myself will set the action="" to PHP_SELF as many years ago had issues with it, soon this will change once i start using HTML5 when its cross browser enough / older users update their browsers.
Example of processing or showing a post but not both.
echo $_POST['book_name'];
echo "POST Dump: " . print_r($_POST, true);
} else {
// No Form Submission, So we generate the form instead
// QUERY - data
$query = "SELECT * FROM 'vendor_list'";
$query_run = mysql_query($query);
if(mysql_num_rows($query_run)>0) {
// FORM - Name/#Copies
echo '<form method="POST" action="' . $_SERVER['PHP_SELF'] . '">';
echo 'Book name:<input type="text" name="book_name" size="40" maxlength="40"><br><br>';
echo 'Number Of Copies:<input type="text" size="5" name="num_copies" maxlength="2"><br><br>';
// FORM - Select Vendor
echo '<select name="vendor_email">';
while($query_row=mysql_fetch_assoc($query_run))
{
$laser=$query_row['vendor_name'];
$email=$query_row['email'];
echo "<option value='".$email."'>".$laser."</option>";
}
echo '</select><br>';
// FORM - Submit / Close Form
echo '<input type="submit" value="submit">';
echo '</form>';
} else {
echo 'No vendor to display';
}
}
?>
Or using a code that shows the form and processing any $_POST
<?php
// QUERY - data
$query = "SELECT * FROM 'vendor_list'";
$query_run = mysql_query($query);
if(mysql_num_rows($query_run)>0) {
// FORM - Name/#Copies
echo '<form method="POST" action="' . $_SERVER['PHP_SELF'] . '">';
echo 'Book name:<input type="text" name="book_name" size="40" maxlength="40"><br><br>';
echo 'Number Of Copies:<input type="text" size="5" name="num_copies" maxlength="2"><br><br>';
// FORM - Select Vendor
echo '<select name="vendor_email">';
while($query_row=mysql_fetch_assoc($query_run))
{
$laser=$query_row['vendor_name'];
$email=$query_row['email'];
echo "<option value='".$email."'>".$laser."</option>";
}
echo '</select><br>';
// FORM - Submit / Close Form
echo '<input type="submit" value="submit">';
echo '</form>';
} else {
echo 'No vendor to display';
}
if (isset($_POST['book_name'])) {
echo "<b>Displaying post data</b> <br />";
echo "Example book: " . $_POST['book_name'] . "<br />";
echo "POST Dump: <br /><pre>" . print_r($_POST, true) . "</pre>";
} else {
echo "Page has not been subitted yet, please select an item";
}
?>

It only post the value of "array" instead of the expected value

I have this code bellow:
<?php
if(isset($_SESSION["products"])) {
$total = 0;
echo '<form action="cart-post-config.php" enctype="multipart/form-data" method="POST">>';
foreach ($_SESSION["products"] as $cart_itm) {
echo '<tr>';
echo '<td></td>';
echo '<td><input type="text" name="product_name[]" value="'.$cart_itm["name"].'"/></td>';
echo '<td><input type="text" name="product_price[]" value="'.$cart_itm["price"].'"/></td>';
echo '<td><input type="text" name="product_quantity[]" value="'.$cart_itm["qty"].'"/></td>';
$totalPerEach = ($cart_itm["price"]*$cart_itm["qty"]);
echo '<td><input type="text" name="product_eachTotal[]" value="'.$totalPerEach.'"/></td>';
echo '<td>View Save Delete</td>';
echo '</tr>';
}
echo '<input type="submit" name="submit" />';
echo '</form>';//close the form
}
else {
echo 'Your Cart is empty'; }
?>
The code above is used to collect the cart information of the member, where the information that has been collected are putted to the input of the form that will be posted to the database.
This is the config:
if($_SERVER["REQUEST_METHOD"] == "POST")
{
for($i=0; $i<count($_POST['product_name']);$i++)
{
$product_name=$_POST['product_name'][$i];
$product_price=$_POST['product_price'][$i];
$product_quantity=$_POST['product_quantity'][$i];
$product_eachTotal=$_POST['product_eachTotal'][$i];
mysql_query("insert into member_cart (cart_code, product_cart_name, product_cart_price, product_cart_quantity, total_cart_price)
values(0, '$product_name', '$product_price', '$product_quantity', '$product_eachTotal')");
}
}
The config above will be read the form that will be posted where will loop the row of eahc products that have been added to the chart.
My problem now is after posting the form, the row of product_cart_quantityand the row of total_cart_price' only got the text/data ofArray` in the database.
Please help I don't have any idea what I am doing wrong here.
Thanks
You have set $product_price to 3 different values which ends up being product_eachTotal. Following is the corrected code.
if($_SERVER["REQUEST_METHOD"] == "POST") {
for($i=0; $i<count($_POST['product_name']);$i++) {
$product_name=$_POST['product_name'][$i];
$product_price=$_POST['product_price'][$i];
$product_quantity=$_POST['product_quantity'][$i];
$product_eachTotal=$_POST['product_eachTotal'][$i];
mysql_query("insert into member_cart (cart_code, product_cart_name, product_cart_price, product_cart_quantity, total_cart_price)
values(0, '$product_name', '$product_price', '$product_quantity', '$product_eachTotal')");
}
}

checkbox related issue in php and html

I want to print the check box values(only the selected value) but the following error are shown
Access forbidden!
You don't have permission to access the requested object. It is either read-protected or not readable by the server.
If you think this is a server error, please contact the webmaster.
Error 403
localhost
Apache/2.4.7 (Win32) OpenSSL/1.0.1e PHP/5.5.9
here is the entire php file ... i am new in php ... so plz check my code..there may b some error ...
<?php // File: anyco.php
require('anyco_ui.inc.php');
// Create a database connection
$conn = oci_connect('system','123','localhost/orcl');
ui_print_header('FoodItemList');
//session_start();
//$cid=$_SESSION['cid'];
do_query($conn, 'SELECT Fooditem_ID,Food_item_name,price,day_available,time_available,discount_percentage,start_date,deadline FROM Food_Item');
ui_print_footer(date('Y-m-d H:i:s'));
// Execute query and display results
function do_query($conn, $query)
{
$stid = oci_parse($conn, $query);
$r = oci_execute($stid,OCI_DEFAULT);
print '<table border="1">';
print '<tr>';
print '<td>Food_ID<td>Food_Name<td>Price(tk)<td>Dvailable_day<td>Avaliable_time<td>Discount<td>Dis_start date<td>Dis_finish date<td>selected item<td>quanity';
while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS))
{
print '<tr>';
$num=1;
$val="";
foreach ($row as $item)
{
if($num==1)
{
$val = $item;
$num=2;
}
print '<td>'.($item!== null ? htmlentities($item) : ' ').'</td>';
}
echo '<td><input type="checkbox" name="ticked[]" value="$val"></td>';
echo '<td><input type="number" name ="name[]" ></td>';
print '</tr>';
}
print '</table>';
}
if(isset($_POST['submit'])&&)
{
echo'yeeeeeeeeeeeeeeee';
$ticked = $_POST['ticked'];
foreach ($ticked as $ticked=>$value)
{
echo "values selected are : ".$value."<br />";
}
}
?>
<html>
<style>
body
{
background:orange;
}
</style>
<body text="green">
<br><br>
<form action="<?php echo $current_file;?>" method="POST">
<input type ="submit" value="Submit"><br><br>
</form>
</body>
</html>
First thing to do is remove &&
if(isset($_POST['submit'])&&)
^ remove the &&
then change this
<input type ="submit" value="Submit"><br><br>
to
<input type="submit" name="submit" value="Submit"><br><br>
because isset($_POST['submit']) is looking for a name=""
<?php
if(isset($_POST['submit']))
{
echo'yeeeeeeeeeeeeeeee';
// more of your code...
}
?>
Edit
remove
action="<?php echo $current_file;?>"
from
<form action="<?php echo $current_file;?>" method="POST">
so it should be just
<form method="POST">

Categories