Jump to content

Need help passing a value through a checkbox


Go to solution Solved by Barand,

Recommended Posts

Hey,

 

I have a list of store names displayed in the script below. I am trying to delete the whole store row, by selecting the store and running a delete query. I am having trouble passing the store id to the $store_id variable through a checkbox. I am want to select a store via checkbox, and pass the ID to the $store_id using the POST super global. Does anyone have any suggestions. Below is my script.

<?php require_once("include/database_connection.php");
global $connection;


?>


<?php 
if(isset($_POST['submit'])) {

$store_id = ""; // How do I pass the store_id  to the $store_id variable by selecting the store below
//$delete_query = "DELETE FROM stores WHERE store_id = {$store_id} ";
echo $store_id;	
}

?>

<form action="delete_stores.php" method="post">
<?php
$count=1;
$get_all_stores = "SELECT * FROM stores ";
$query = mysql_query($get_all_stores, $connection);
while($display_store = mysql_fetch_array($query)) {
echo "<input type=\"checkbox\" value=\"{$display_store['store_id']}\">" . $count . " " .  $display_store['store] . "</input>" ."<br>";
$count++;}
?>
<input type="submit" name="delete" value="delete">
</form>

Couple things:

 

1. There is no $_POST['submit'] because you named your submit button "delete".

2. You're missing a quote here: $display_store['store]

3. Your checkbox input doesn't have a name.  If you name it "store" then you would access $_POST['store'].  However, you have the possibility of multiple stores, so you would need a stores array like name="stores[]".  You would then need to loop through each store in the array and delete them, or better put them all in one query probably using the WHERE IN ().

Edited by AbraCadaver

Couple things:

 

1. There is no $_POST['submit'] because you named your submit button "delete".

2. You're missing a quote here: $display_store['store]

3. Your checkbox input doesn't have a name.  If you name it "store" then you would access $_POST['store'].  However, you have the possibility of multiple stores, so you would need a stores array like name="stores[]".  You would then need to loop through each store in the array and delete them, or better put them all in one query probably using the WHERE IN ().

Okay got you! I have made the changes. I first need to figure out how to i pass in multiple store id's in the array. This is what I cam came up with now, but not getting any luck :/

<?php require_once("database_connection.php");
global $connection;


?>


<?php 
if(isset($_POST['submit'])) {
$store_array = array();
$store_array[] = $_POST['store_id']; 
echo $store_array;	
}

?>

<form action="delete_stores.php" method="post">
<?php
$count=1;
$get_all_stores = "SELECT * FROM stores ";
$query = mysql_query($get_all_stores, $connection);
while($display_store = mysql_fetch_array($query)) {
echo "<input type=\"checkbox\" value=\"{$display_store['store_id']}\" name=\"store_id\">" . $count . " " .  $display_store['store'] . "</input>" ."<br>";
$count++;}
?>
<input type="submit" name="submit" value="submit">
</form>


 

 

However, you have the possibility of multiple stores, so you would need a stores array like name="stores[]".  You would then need to loop through each store in the array and delete them, or better put them all in one query probably using the WHERE IN ().

 

However, you have the possibility of multiple stores, so you would need a stores array like name="stores[]".  You would then need to loop through each store in the array and delete them, or better put them all in one query probably using the WHERE IN ().

 

Barand,

 

 I did a foeach loop on the array but its only giving me back one store_id when I select multiple stores and click on submit. Below is the code I am working with

if(isset($_POST['submit'])) {
$store_array = array();
$store_array[] = $_POST['store_id']; 
foreach ($store_array as $store_id) {
echo $store_id;	

The name of the INPUT field should use the [] array construct: <INPUT name="store_id[]" ...

 

The reference in the code should not: $store_array = $_POST['store_id']

 

What should the reference be? Can you provide me with a full working example?

The name of the INPUT field should use the [] array construct: <INPUT name="store_id[]" ...

 

The reference in the code should not: $store_array = $_POST['store_id']

Oops! typo; should have said:

 

"The reference in the code should be: $store_array = $_POST['store_id']"

The above are both working examples just as they are.

if(isset($_POST['submit'])) {
   $store_array = $_POST['store_id'];
}

And

echo "<input type=\"checkbox\" value=\"{$display_store['store_id']}\" name=\"store_id[]\">{$display_store['store']}</input><br>";

 

The above are both working examples just as they are.

if(isset($_POST['submit'])) {
   $store_array = $_POST['store_id'];
}

And

echo "<input type=\"checkbox\" value=\"{$display_store['store_id']}\" name=\"store_id[]\">{$display_store['store']}</input><br>";

Abracadaver. Thanks for that tip. It now worked! Just to confirm.

 

Why did we put the [] after store_id is it to let the $_POST super global to except an array?

 

The above are both working examples just as they are.

if(isset($_POST['submit'])) {
   $store_array = $_POST['store_id'];
}

And

echo "<input type=\"checkbox\" value=\"{$display_store['store_id']}\" name=\"store_id[]\">{$display_store['store']}</input><br>";

Hey I have one more question.

 

I am trying to do a fetch array on the ID that it brings back.

However when I try to do a fetch array it keeps coming back with Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/eldan88/public_html/delete_stores.php on line 22

Below is the code. Any suggesstions?

<?php require_once("include/database_connection.php");
global $connection;

function get_all_stores() {
	$query = "SELECT * FROM stores ";
	return $query;
	
}

function get_store_by_id($store_id) {
	$query = "SELECT * FROM stores WHERE id = {$store_id} ";
	return $query;
}
?>


<?php 
if(isset($_POST['submit'])) {
$store = $_POST['store_id']; 
foreach ($store as $store_id) {
	$store = get_store_by_id($store_id);
mysql_fetch_array($store);


}



}
  • Solution

You are not running the query, just defining a string

function get_store_by_id($store_id) {
    $query = "SELECT * FROM stores WHERE id = {$store_id} ";
    $result = mysql_query($query);
    return $result;
}

In general, you want to avoid running queries in a loop. It would be better to issue a single select for all of the selected IDs and then loop over the results:

 

if(isset($_POST['submit'])) {
  // Since they are checkboxes, it is possible that the store_id element does not exist
  if (isset($_POST['store_id'])) {
    $store = array_map('intval', $_POST['store_id']); // Protect against invalid input
    $sql = 'SELECT * FROM stores WHERE id IN (' . implode(',', $store) . ')';
    $res = mysql_query($sql);
    if ($res) {
      while ($row = mysql_fetch_assoc($res)){
        // Process the data
      }
    }
  }
}
Assuming, of course that the store id is an integer.

 

You are not running the query, just defining a string

function get_store_by_id($store_id) {
    $query = "SELECT * FROM stores WHERE id = {$store_id} ";
    $result = mysql_query($query);
    return $result;
}

Yes you right! I can't believe that has slipped!! Thank you for pointing that out!

In general, you want to avoid running queries in a loop. It would be better to issue a single select for all of the selected IDs and then loop over the results:

 

if(isset($_POST['submit'])) {
  // Since they are checkboxes, it is possible that the store_id element does not exist
  if (isset($_POST['store_id'])) {
    $store = array_map('intval', $_POST['store_id']); // Protect against invalid input
    $sql = 'SELECT * FROM stores WHERE id IN (' . implode(',', $store) . ')';
    $res = mysql_query($sql);
    if ($res) {
      while ($row = mysql_fetch_assoc($res)){
        // Process the data
      }
    }
  }
}
Assuming, of course that the store id is an integer.

 

DavidAm,

 

 Thanks for the useful tip. I am new to this. But I am going to look up what array map is, and how it works with the query you wrote below it.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.