Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 747
  • Last Modified:

batch update checkbox fields (PHP-MySQL)

Hi all,

I am looking for a way to batch update checkbox fields values in Mysql
database. I have a repeat region with checkboxes and am trying to update checkbox fields in MySQL database.

I have tried:
<?php
$ParamID = "1";
if (isset($HTTP_GET_VARS['checkbox'])) {
$ParamID = (get_magic_quotes_gpc()) ? $HTTP_GET_VARS['checkbox'] :
addslashes($HTTP_GET_VARS['checkbox']);
}
$result=mysql_query("UPDATE tbl_model_name SET fld_select = 1 WHERE ID =
$ParamPicID")
or die ("Invalid Query");
//header ("Location: test_selected.php");
?>

First i am not sure if the code is 100% correct, what do you think?
2nd which i am pretty sure it seems like the checkbox values are not being passed from the previous page.

The checkbox field looks like:

<input name="checkbox" type="checkbox"  id="checkbox" value="<?php echo $row_rsLbox['ID']; ?>">

How do i pass or force to pass the checkbox values in the repeated region to the next page to make this code work?

I thank you in advance.

Best Regards
Khna  
0
khna
Asked:
khna
  • 7
  • 4
2 Solutions
 
Cart_manCommented:
First make sure you include those checkboxes within a <form></form> tag and pass the values by submitting the form.

Next, try using a different name for the checkboxes. "checkbox" might be reserved.

If you do all those and it still doesn't work then do this:

      foreach ($_POST["checkbox"] as $value)
      {
            $result=mysql_query("UPDATE tbl_model_name SET fld_select = 1 WHERE ID =
$value")
or die ("Invalid Query");
      }

Hope this helps.
0
 
khnaAuthor Commented:
Thanks for the Reply,
I have changed the name from checkbox to PicID.

I have also tried to use the code snippet you provided but the database column is not being updated and i don't get any errors.

I have tried to check if the variables are being passed by displaying:
<?php echo $_POST['PicID']; ?> on the intermediate page.
Again i can't see any values, which shows the variables "PicID" are not being passed to intermediate page.
Is there something special that i have to do to pass them to next page?
0
 
sgalzinCommented:
Hi,

I can't test your file (I don't have a server "at hand" at the moment) but do check these simple possibilities :

1. Use $ParamID in you SQL query (in your code you make a reference to an undefined variable $ParamPicID)

2. Make sure the form method (<form method="GET"> or "<form method="POST">) corresponds to the way you're getting your variables in PHP, i.e. if you use HTTP_GET_VARS in your PHP code, use method="GET" in your HTML form

3. I remember having problems with checkboxes before. I believe they can either be null, 0, 1, true, false, or have the value you set them to in your HTML code (using the VALUE attribute). So, once you've checked the two aspects above, the value should be passed to your script. Then try echoing the value of the parameter to see what it's worth.

If none of these steps solve your problem, please post your HTML code as well : I'll give it a try tomorrow on my server.

Hope this helps,

Stephane.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
khnaAuthor Commented:
I can now (with the help of one of friends Gerry) pass the variables but it is still not updating the database but the database is still not updated.
The database field "fld_select " is of Type 'tinyint(1)' with NULL = No and
Default = 0

Do i have to change something in database?

The code looks like this now (checkboxes are name selected[] and has the unique value 'ID' assigned):

<?php
if ($_SERVER['REQUEST_METHOD']=="POST"){
 if (is_array($_POST['selected'])){
  $str=join(",",$_POST['selected']);
  $str="UPDATE tbl_model_name SET fld_select = 1 WHERE ID IN ($str)";
  echo $str;
 } else {
  echo "None selected";
 }
}
?>
0
 
sgalzinCommented:
Hi,

Does the echo $str write anything good ? It should look like "UPDATE tbl_model_name SET fld_select = 1 WHERE ID IN (15, 62, 178)". If it doesn't (for example if the string of IDs is not constructed properly in the default array.toString()) then you need to do it manually :

$where = "";
foreach ( $_POST['selected'] as id)
{
 if ( $where == "") then $where = id;
 else $where .= ", " . id;
}
$str="UPDATE tbl_model_name SET fld_select = 1 WHERE ID IN ($where)";

//And then, you must execute the query with :
$dbConnection = mysql_connect ( DBHOSTNAME, DBUSERNAME, DBPASSWORD );
mysql_query ( $str, $dbConnection );

Cheers,

Stephane.
0
 
khnaAuthor Commented:
Yes it wirtes everything as it is supposed to. Depending on the checkboxes selected it always write something like:

UPDATE tbl_model_name SET fld_select = 1 WHERE ID IN (30,29) or
UPDATE tbl_model_name SET fld_select = 1 WHERE ID IN (30,29,28,27)
so on...

So variables are being passed and caught. The only problem is that it is not updating the database.

0
 
khnaAuthor Commented:
Isn't the update carried out by the code?
Why do i have to add:
$dbConnection = mysql_connect ( DBHOSTNAME, DBUSERNAME, DBPASSWORD );
mysql_query ( $str, $dbConnection );

to make it happen?
I am using Dreamweaver and the databse connection is taken care by the String:
<?php require_once('Connections/Contest.php'); ?>
In the include file 'Contest.php' all my connection details are stored.
0
 
sgalzinCommented:
khna,

We're getting there. All you need is to get the string $str to be executed on your database. Writing it to the screen doesn't actually do anything to the database.

The method I suggested was doing :
$dbConnection = mysql_connect ( DBHOSTNAME, DBUSERNAME, DBPASSWORD );
mysql_query ( $str, $dbConnection );

But you can use the method of your choice.

So to sum it up, have the database execute the query that's in $str in some way (if you can't find out how to do it in DreamWeaver do it using what I suggested, and replace the constants DBHOSTNAME, DBUSERNAME, DBPASSWORD with strings containing the relevant information).

keep us posted ;-)

Stephane.
0
 
khnaAuthor Commented:
Thanks Stephane,
I have tried the way you suggested.
I added at the end of the above code:
<?php
$dbConnection = mysql_connect ( 00.100.100.00, test, test);
mysql_query ( $str, $dbConnection );
?>

It still does not update the database. I might be making some stupid mistake somewhere but then you can see the whole code and point out what am i doing wrong.
0
 
sgalzinCommented:
khna,

Try something along the lines of :

$dbConnection = mysql_connect ( "00.100.100.00", "test", "test");

(The double quotes (") are important).

Also, I have to say I'm a little surprised at your Database Host Name : are you sure it is correct ?

Finally, I would have expected a few error messages ... Maybe turning on error messages might help us solve your problem, add these two lines to the very top of your PHP code :

error_reporting ( E_ALL );
ini_set ( "display_errors", "on" );

Tell me what you get ...

Stephane.
0
 
khnaAuthor Commented:
Database Host Name has just dummy values. I did not post the real IP adress, pass and username. If you want i can mail you the real data.
The code looks like this now:

<?php
error_reporting ( E_ALL );
ini_set ( "display_errors", "on" );
if ($_SERVER['REQUEST_METHOD']=="POST"){
 if (is_array($_POST['selected'])){
  $str=join(",",$_POST['selected']);
  $str="UPDATE tbl_model_name SET fld_select = 1 WHERE ID IN ($str)";
}
}
?>
<?php
$dbConnection = mysql_connect ( "00.100.100.00", "test", "test");
mysql_query ( $str, $dbConnection );
?>
no errors and no update taking place.
0
 
khnaAuthor Commented:
I sorted it out with another friends help.
Thank you for pointing in the right direction.
Here is the code if someone else needs it:

<?php
require_once('Connections/connection.php');
mysql_select_db($database_ConModelz, $ConModelz);
if ($_SERVER['REQUEST_METHOD']=="POST"){
 if (is_array($_POST['selected'])){
  $str=join(",",$_POST['selected']);
  $str="UPDATE tbl_test SET fld_select = 1 WHERE ID IN ($str)";
  mysql_query($str) or die("Query failed:<br>".mysql_error());
  echo "Table updated";
 } else {
  echo "None selected";
 }
}
?>
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now