Help needed for passing values
Posted: Wed Apr 29, 2009 10:58 am
Hello,
I am working on a form to Activate/Deactivate post items... I need the form page to display all post items (1-n...) Each post item needs to have 2 check boxes... on submit, the processing php script needs to update all of the entries to toggle the 2 check boxes.
The question is, how do i get my data from the form to the processing script in a way that it can update multiple entries?! I know how to create/edit single entries no problem, but how to I pass the values and process all of them.
Below is the code that I have:
Form:
Processing Script:
Thank you so much for any help that you can give!
I am working on a form to Activate/Deactivate post items... I need the form page to display all post items (1-n...) Each post item needs to have 2 check boxes... on submit, the processing php script needs to update all of the entries to toggle the 2 check boxes.
The question is, how do i get my data from the form to the processing script in a way that it can update multiple entries?! I know how to create/edit single entries no problem, but how to I pass the values and process all of them.
Below is the code that I have:
Form:
Code: Select all
<?php
include 'db.inc';
$dbh = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
$sql="SELECT id, type, title, long_title, active, in_archive FROM spotlight.posts";
$result = mysql_query($sql) or die(mysql_error());
?>
<form method="post" action="process_active.php" enctype="multipart/form-data">
<table cellspacing="5px" border="1px">
<tr>
<th>Visible on Homepage:</th>
<th>Visible In Archive:</th>
<th>Spotlight Title:</th>
<th>RSS Spotlight Title:</th>
</tr>
<?php
while ($row = mysql_fetch_assoc($result)) {
//Translates post type
if($row[type]==2){
$post_type='Did You Know: ';
}elseif($row[type]==3){
$post_type='Best in Show: ';
}else{
$post_type="";
}
//active value
if ($row[active}==1){
$active_check="checked";
}else{
$active_check="";
}
//in_archive value
if ($row[in_archive}==1){
$in_archive_check="checked";
}else{
$in_archive_check="";
}
?>
<tr>
<td><input type="hidden" name="id" value="<?=$row[id]; ?>" ><input type="checkbox" checked="<?=$active_check; ?>" name="active" value="1" /></td>
<td><input type="checkbox" checked="<?=$in_archive_check; ?>" name="in_archive" value="1" /></td>
<td><?=$post_type.$row[title]; ?></td>
<td><?=$row[long_title]; ?></td>
</tr>
<? } ?></table></form>Code: Select all
<?php
include 'db.inc';
$dbh = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
while(???MORE POSTS TO UPDATE???){
$post_id=$_POST['id'];
$post_active=$_POST['active'];
$post_in_archive=$_POST['in_archive'];
//Handle possibilities for ACTIVE/INACTIVE
if ($post_active==1 && $post_in_archive==1){
$post_active=1;
$post_in_archive=1;
} else if ($post_active==null && $post_in_archive==null){
$post_active=0;
$post_in_archive=0;
} else if ($post_active==null && $post_in_archive==1){
$post_active=0;
$post_in_archive=1;
} else if ($post_active==1 && $post_in_archive==null){
$post_active=1;
$post_in_archive=1;
}
$sql="INSERT INTO spotlight.posts (active, in_archive)
VALUES ($post_active, $post_in_archive)
WHERE id == $post_id";
$result = mysql_query($sql) or die(mysql_error());
}
?>