Solved

batch update checkbox fields (PHP-MySQL)

Posted on 2004-08-28
12
705 Views
Last Modified: 2010-05-18
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
Comment
Question by:khna
  • 7
  • 4
12 Comments
 
LVL 1

Assisted Solution

by:Cart_man
Cart_man earned 100 total points
ID: 11924819
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
 

Author Comment

by:khna
ID: 11925537
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
 
LVL 4

Expert Comment

by:sgalzin
ID: 11925966
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
 

Author Comment

by:khna
ID: 11930529
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
 
LVL 4

Expert Comment

by:sgalzin
ID: 11930854
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
 

Author Comment

by:khna
ID: 11931317
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:khna
ID: 11931380
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
 
LVL 4

Expert Comment

by:sgalzin
ID: 11931497
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
 

Author Comment

by:khna
ID: 11931657
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
 
LVL 4

Accepted Solution

by:
sgalzin earned 400 total points
ID: 11931757
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
 

Author Comment

by:khna
ID: 11933389
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
 

Author Comment

by:khna
ID: 11933482
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to count occurrences of each item in an array.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now