I'm trying to delete some information from the database, but when I ask the user to confirm action, something goes wrong. Can you help me?
The script prints the categories to choose if nothing is settled. Then, It asks the user to delete or not delete the categories chosen. In the end, it deletes what was chosen.
But there's some error in the last part(first in the script) and I cannot understad where it's going wrong:
<?php
//If the user confirm to delete...
if(isset($_POST['eliminazione_conferma']) and $_POST['eliminazione_conferma']=='conferma'){
//if conferma_eliminazione=1, don't delete
if(isset($_POST['conferma_eliminazione']) and $_POST['conferma_eliminazione']=='1'){
echo 'Eliminazione annullata';
}else{//Delete if conferma_eliminazione=0
while($row=$categoria){
$delete= "DELETE FROM categorie WHERE $row = category_id";
$query=mysql_query($delete);
echo "$row eliminato\n";
}
}
}else{//ELSE, print the form to confirm action
if(isset($_POST['eliminazione']) and $_POST['eliminazione']=='delete'){
//Array with the chosen "categories"
$categoria=isset($_POST['categoria']) ? $_POST['categoria'] : array();
//Print the chosen categories to ask confirmation
echo'Sicuro di voler eliminare le seguenti categoire?<br />';
foreach($categoria as $row){
echo "$row<br />\n";
}
//Yes = 0, No = 1
echo '<form method="post" action="',$_SERVER['REQUEST_URI'],'">
<input type="radio" name="conferma_elimiazione" value="0" />Si<br />
<input type="radio" name="conferma_eliminzione" value="1" />No<br />
<input type="hidden" name="eliminazione_conferma" value="conferma" />';
foreach($categoria as $row){
echo'<input type="hidden" name="categoria[]" value="',$row,'" />',"\n",'' ;
}
echo'
<input type="submit" value="Conferma" />
</form>';
}else{//In the end, if nothing is settled, print the form to check the category to delete
//Select the categories from the database
echo'<form method="post" action="',$_SERVER['REQUEST_URI'],'" />',"\n",'';
$select = "SELECT nome,category_id FROM categorie ORDER BY category_id" ;
$select_result=mysql_query($select) or die("Mysql Error: ".mysql_error());
while($row = mysql_fetch_assoc($select_result)){
echo '<input type="checkbox" name="categoria[]" value="',$row['category_id'],'">',$row['nome'],'<br />';
}
echo'<input type="hidden" name="eliminazione" value="delete" />
<input type="submit" name="submit" value="Elimina" />
</form>';
}
}
The problem with the query
$delete= "DELETE FROM categorie WHERE $row = category_id";
is
here
WHERE $row = category_id";
What is category_id?
Do you have a value for that?
Probably you want WHERE category_id=/*something here like $row['column_name']*/
Thanks everyone for the help, but I've solved my problem in this way(the script wasn't transformig the value of "conferma_eliminazione" in the integer type):
<h2>Modifica o Elimina Categoria </h2>
<?php
if(isset($_POST['eliminazione_conferma']) and $_POST['eliminazione_conferma']=='conferma'){
$categoria=isset($_POST['categoria']) ? $_POST['categoria'] : array();
$a=(int)$_POST['eliminazione'];
if($a=='1'){
echo 'Eliminazione annullata';
echo "\n";
}elseif($a=='0'){
foreach($categoria as $row){
$delete= "DELETE FROM categorie WHERE $row = category_id";
$query=mysql_query($delete);
echo "$row eliminato\n";
}
}
}else{
if(isset($_POST['eliminazione']) and $_POST['eliminazione']=='delete'){
$categoria=isset($_POST['categoria']) ? $_POST['categoria'] : array();
echo'Sicuro di voler eliminare le seguenti categoire?<br />';
foreach($categoria as $row){
echo '',$row,'<br />',"\n";
}
echo '<form method="post" name="form_eliminazione_categoria" action="',$_SERVER['REQUEST_URI'],'">',"\n",'
<input type="radio" name="eliminazione" value="0" />Si<br />' ,"\n",'
<input type="radio" name="eliminazione" value="1" />No<br />',"\n",'
<input type="hidden" name="eliminazione_conferma" value="conferma" />',"\n",'';
foreach($categoria as $row){
echo '<input type="hidden" name="categoria[]" value="',$row,'" /><br />',"\n",'';
}
echo'
<input type="submit" value="Conferma" />
</form>';
}else{
echo'<form method="post" action="',$_SERVER['REQUEST_URI'],'" />',"\n",'';
$select = "SELECT nome,category_id FROM categorie ORDER BY category_id" ;
$select_result=mysql_query($select) or die("Mysql Error: ".mysql_error());
while($row = mysql_fetch_assoc($select_result)){
echo '<input type="checkbox" name="categoria[]" value="',$row['category_id'],'">',$row['nome'],'<br />';
echo "\n";
}
echo'<input type="hidden" name="eliminazione" value="delete" />',"\n",'
<input type="submit" name="submit" value="Elimina" />',"\n",'
</form>';
}
}
Related
I start by showing a list of businesses I have stored in a db. There is a form to search by industry or state. When showing a list of search results, I also provide the option to move the businesses to different tables. After submitting the form to move a business to a different table, the list refreshes to default result list, and we have to enter search term again.
I've tried assigning $_POST values to dynamic urls in the action url of my forms, I've tried assigning $_POST values to the value="" parameter of my forms.
<?php
if (isset($_POST['update']) && ($_POST['update'] == 'true')){
$sql = 'INSERT INTO table1 (columns,columns,columns)
SELECT columns,columns,columns
FROM table2 WHERE id = 1';
if (mysqli_query($db, $sql)) {}
}
?>
<div>
<form action="./?action=list" method="POST">
<input type="hidden" name="search" value="true" />
<input placeholder=" INDUSTRY" type="text" size="15" name="kw" />
<select name="state" onchange='this.form.submit()'>
<option value=''>BY STATE</option>
<?php require('includes/stateselect.php'); ?>
</select>
<input type="submit" value="SEARCH">
</form>
</div>
<table>
<?php
$sql = 'SELECT * FROM db.table ';
if ((isset($_POST['kw'])) && (!empty($_POST['kw']))){
$sql .=' WHERE `kw` LIKE \'%'.$_REQUEST['kw'].'%\' OR `biz` LIKE \'%'.$_POST['kw'].'%\' ';
}
if ((isset($_POST['state'])) && (!empty($_POST['state']))){
$sql .=' WHERE `state` = \''.$_POST['state'].'\' ';
}
if ($result = mysqli_query($db, $sql)){
while ($row = mysqli_fetch_array($result)) {
echo '<tr><form method="POST" action="./?action=list">
<input type="hidden" name="id" value="'.$row['id'].'" />
<input type="hidden" name="update" value="true" />
<td>'.$row['kw'] .'</td><td>'. $row['state'].'</td>';
echo '<td>
<select name="move">
<option>--Fresh--</option>
<option value="dnc">DNC</option>
</select>';
echo '<input type="submit" value="submit">';
echo '</td></form></tr>';
}
}
?>
</table>
We want to be able to search for lawyers, then disposition them from the list as we call them, but retain our search results.
You can store the search term inside a session so that you can use that term multiple times in your form. Store the search term in the session like this,
if ((isset($_POST['kw'])) && (!empty($_POST['kw']))){\
$_SESSION['search_term] = $_POST['kw'];
$sql .=' WHERE `kw` LIKE \'%'.$_REQUEST['kw'].'%\' OR `biz` LIKE
\'%'.$_POST['kw'].'%\' ';
}
You could try to re-set the kw as a hidden input within the second form to retain the search terms, like this :
<?php
if (isset($_POST['update']) && ($_POST['update'] == 'true')){
$sql = 'INSERT INTO table1 (columns,columns,columns)
SELECT columns,columns,columns
FROM table2 WHERE id = 1';
if (mysqli_query($db, $sql)) {}
}
?>
<div>
<form action="./?action=list" method="POST">
<input type="hidden" name="search" value="true" />
<input placeholder=" INDUSTRY" type="text" size="15" name="kw" />
<select name="state" onchange='this.form.submit()'>
<option value=''>BY STATE</option>
<?php require('includes/stateselect.php'); ?>
</select>
<input type="submit" value="SEARCH">
</form>
</div>
<table>
<?php
$sql = 'SELECT * FROM db.table ';
if ((isset($_POST['kw'])) && (!empty($_POST['kw']))){
$sql .=' WHERE `kw` LIKE \'%'.$_REQUEST['kw'].'%\' OR `biz` LIKE \'%'.$_POST['kw'].'%\' ';
}
if ((isset($_POST['state'])) && (!empty($_POST['state']))){
$sql .=' WHERE `state` = \''.$_POST['state'].'\' ';
}
if ($result = mysqli_query($db, $sql)){
while ($row = mysqli_fetch_array($result)) {
echo '<tr><form method="POST" action="./?action=list">
<input type="hidden" name="id" value="'.$row['id'].'" />
<input type="hidden" name="update" value="true" />
<td>'.$row['kw'] .'</td><td>'. $row['state'].'</td>';
echo '<td>
<select name="move">
<option>--Fresh--</option>
<option value="dnc">DNC</option>
</select>';
// added below blocks
if (!empty($_POST['kw'])){
echo '<input type="hidden" name="kw" value="'.$_POST['kw'].'" />';
// echo '<input type="hidden" name="search" value="true" />';
}
if (!empty($_POST['state'])){
echo '<input type="hidden" name="state" value="'.$_POST['state'].'" />';
}
echo '<input type="submit" value="submit">';
echo '</td></form></tr>';
}
}
?>
</table>
I'm trying to build a page where my users can paste in multiple item #s for that product and it will give them the parent model # for that particular item, where items are given individual identifiers.
However, my users paste there information into the textboxs, but it doesn't pull anything up. When I had one value to search it was able to find the items. My table structure is very simple.Fcsku varchar(45), fnsku varchar(45), updated time(45 are not important to this function).
Here is my query Updated:
<form action="" method="get">
Paste your ZZZ's here: <br><input type="text" name="item" id="textbox"/><br>
<input type="text" name="item2" id="textbox2"/>
<script>document.getElementById('textbox').focus()</script><br />
<input type="submit" value="Submit"/>
</form>
<?php
if (!empty($_REQUEST['item'])) {
$item = mysql_real_escape_string($_REQUEST['item']);
$item2 = mysql_real_escape_string($_REQUEST['item2']);
$sql = "select * from oak3_zzz_to_boo WHERE fcsku like '%".$item."%' or fcsku like '%".$item2."%'";
$r_query = mysql_query($sql);
while ($row = mysql_fetch_array($r_query)) {
echo "<font color=red size=7>";
echo '<center><br /> Parent ASIN: '.$row['fnsku'];
echo "</center></font>";
echo "<br><br><br><br><br>";
}
}
?>
This worked at my server:
<form action="" method="post">
Paste your ZZZ's here:<br />
<input type="text" name="item" id="textbox" /><br />
<input type="text" name="item2" id="textbox2"/><br />
<input type="submit" value="Submit" name="submit"/>
<script>document.getElementById('textbox').focus()</script>
</form>
<?php
if (isset($_POST['submit'])) {
$item = mysql_real_escape_string('%'.$_POST['item'].'%');
$item2 = mysql_real_escape_string('%'.$_POST['item2'].'%');
$sql = "SELECT * FROM oak3_zzz_to_boo WHERE fcsku LIKE '" . $item . "' OR fcsku LIKE '" . $item2 . "'";
$r_query = mysql_query($sql);
while ($row = mysql_fetch_assoc($r_query)) {
echo "<font color=red size=7>";
echo '<center><br />Parent ASIN: ' . $row['fnsku'];
echo "</center></font>";
echo "<br /><br /><br /><br /><br />";
}
}
?>
I am trying to display mysql records through hidden field values, but nothing displays.
A little help!
Here's the code;
Html:
<form name="form11" method="post" action="hpdata.php" enctype="multipart/form-data">
<input name="pro" id="pro" type="hidden" value= "CMS" />
<input name="piror" id="piror" type="hidden" value= "P1" />
<input name="stat" id="stat" type="hidden" value= "In Progress" />
<input type="submit" name="submit" id="submit" class="groovybutton" value="...">
</form>
PHP:
<?php
$project = $_POST["pro"];
$pirority = $_POST["piror"];
$status = $_POST["stat"];
mysql_connect ("one", "two", "three");
mysql_select_db ("wsms");
$rest = mysql_query("SELECT * FROM sheet WHERE project='$project' AND
pirority='$pirority' AND status='$status'");
while($row = mysql_fetch_array($rest))
{
echo $row['id'] . " " . $row['date']; echo "<br>";
}
?>
Put isset into your php code
Example
<?php
if(isset($_POST['submit'])){
echo $project = $_POST["pro"]."<br>";
echo $pirority = $_POST["piror"]."<br>";
echo $status = $_POST["stat"];
/* mysql_connect ("one", "two", "three");
mysql_select_db ("wsms");
$rest = mysql_query("SELECT * FROM sheet WHERE project='$project' AND
pirority='$pirority' AND status='$status'");
while($row = mysql_fetch_array($rest))
{
echo $row['id'] . " " . $row['date']; echo "<br>";
}*/
}
?>
<form name="form11" method="post" action="" enctype="multipart/form-data">
<input name="pro" id="pro" type="hidden" value= "CMS" />
<input name="piror" id="piror" type="hidden" value= "P1" />
<input name="stat" id="stat" type="hidden" value= "In Progress" />
<input type="submit" name="submit" id="submit" class="groovybutton" value="...">
</form>
Output
CMS
P1
In Progress
First of all check that if the data is coming in the post or not:
<?php
echo "<pre>";
print_r($_POST);
exit;
?>
If yes than remove the print code i provided , and use extract($_POST); at the top of your PHP code. You query will become like this:
$rest = mysql_query("SELECT * FROM sheet WHERE project='$pro' AND
pirority='$piror' AND status='$stat'");
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
}
}
?>
I am trying to echo out an array using for each but it's not displaying any values. The only values i can echo out is within a second for each.
Can anyone tell me what i'm doing wrong?
What i am trying to echo is the price, item, description etc but i am getting nothing.
If you need to see the output of the array it's here http://operationbraveheart.org.uk/jcart/testshop.php
while ($row = $result->fetch()) {
$superitem[$row['itemid']][] = $row;
}
foreach($superitem AS $subitem) {
list($prodid,$item,$size,$description,$price) = $subitem[0];
if ($count % NUMCOLS == 0) echo "<tr>"; # new row
echo '<td>';
var_dump($subitem);
//Your normal code up until the select box...
echo '<form method="post" action="" class="jcart">
<fieldset>
<input type="hidden" name="jcartToken" value="'.$_SESSION['jcartToken'].'" />
<input type="hidden" name="my-item-id" value="'.$subitem['prodid'].'" />
<input type="hidden" name="my-item-price" value="'.$subitem['price'].'" />
<input type="hidden" name="my-item-url" value="http://yahoo.com" />';
if(count($subitem) > 1) {
echo '<li><select name="my-item-name" id="foo">';
foreach($subitem AS $subsubitem) {
echo "<option value='".$subsubitem['size']."'>".$subsubitem['size']."</option>";
}
echo "</select></li>";
}
else {
echo '<input type="hidden" name="my-item-name" value="'.$subitem['item'].'" />';
}
echo'<li>Price: $<span class="price">'.$subitem['price'].'</span></li>
<li>
<label>Qty: <input type="text" name="my-item-qty" value="1" size="3" /></label>
</li>
</ul>
<input type="submit" name="my-add-button" value="add to cart" class="button" />
</fieldset>
</form>';
echo '</td>';
$count++;
$counter++;
if ($count % NUMCOLS == 0) echo "</tr>\n"; # end row
}
Current it looks like $subitem contains an array of length 1 where the first index is a row. Change...
$superitem[$row['itemid']][] = $row;
should be...
$superitem[$row['itemid']] = $row;
and...
list($prodid,$item,$size,$description,$price) = $subitem[0];
should be...
list($prodid,$item,$size,$description,$price) = $subitem;