Jump to content

using checkbox for updating or deleting...


aian04

Recommended Posts

ok now i change my idea... i just put checkbox and two submit button...

the button is either update or delete...

hirs my code

<?php
$host="localhost"; 
$username="root"; 
$password="tmc";
$db_name="tgp"; 
$tbl_name="reservation"; 
$id=$_COOKIE['ID'];

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name join packages on reservation.pac_id = packages.pac_id where Cid ='$id'";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

?>
<html>
<head><link href="interface design/css.css" rel="stylesheet" type="text/css">
</head>
<body align="center">
<form action="http://localhost:6080/aian/update.php" method="post" name="form">
<table class="ret"   border="0" cellpadding="10" cellspacing="3" background="interface design/logo/member.jpg">
<tr>
<td colspan="7" align="center"><strong>Reservatio History</strong> </td>
</tr>
<tr><td>&nbsp</td>
<td align="center">Reservation ID</td>
<td align="center"><strong>Check in Date</strong></td>
<td align="center"><strong>No. of nights</strong></td>
<td align="center"><strong>Room Number</strong></td>
<td align="center"><strong>Specials And Packages</strong></td>
<td align="center"><strong>Customer ID</strong></td>
</tr>
<?php


while($rows=mysql_fetch_assoc($result)){

?>
<tr><td><input type="checkbox" name="check" id="check" value=" <? echo $row['Res_id'];?>"></td>
<td><? echo $rows['Res_id'];?></td>
<td><? echo $rows['check_in'];?></td>
<td><? echo $rows['night_per_stay'];?></td>
<td><? echo $rows['R_no'];?></td>
<td><? echo $rows['pac_name'];?></td>
<td><? echo $rows['Cid']; ?></td>
</tr>
<?php
}?>

</table>
<pre>
		<input type="submit" class="groovybutton2" value="Update" name="update">  <input class="groovybutton2" type="submit" value="Delete" name="delete">
</pre>
</form>
</body>
</html>
<?

@mysql_close();?>

and this is my code to proces the above code

<html>
<head><link href="interface design/css.css" rel="stylesheet" type="text/css">
</head>
<body class="ret">
<?php
@mysql_connect ("localhost", "root", "tmc") or die (mysql_error());
@mysql_select_db("tgp")or die(mysql_error());

$edit=$_POST['update'];
$delete=$_POST['delete'];
$check=$_POST['check'];

if(isset($edit)){
	echo "$edit";
	echo "$check";

}

if(isset($delete)){

echo $check;
echo $delete;
}


?>
</body>
</html>

 

ok im just trying to echo the value of the checkbox for testing the code..

but the value of checked checkbox doesnt show anything...

is this a good idea? can u help me to solve this..

 

Link to comment
Share on other sites

Change the name of the "submit" button to be the same for each button, then you can check for the value:

<input type="submit" class="groovybutton2" value="Update" name="submit_button">  <input class="groovybutton2" type="submit" value="Delete" name="submit_button">

 

<html>
<head><link href="interface design/css.css" rel="stylesheet" type="text/css">
</head>
<body class="ret">
<?php
@mysql_connect ("localhost", "root", "tmc") or die (mysql_error());
@mysql_select_db("tgp")or die(mysql_error());

$check=$_POST['check'];

if($_POST['submit_button'] == 'Update'){
	echo "Update<br>";
	echo $check;

}

if($_POST['submit_button'] == 'Delete'){
echo "Delete<br>";
echo $check;
}


?>
</body>
</html>

 

Ken

Link to comment
Share on other sites

You have multiple checkboxes all with the same name, so PHP is going to return the value of the last on defined. That's probably not what you want. You need to change your code so that the name of the checkbox is an array:

<?php
while($rows=mysql_fetch_assoc($result)){

?>
<tr><td><input type="checkbox" name="check[<? echo $row['Res_id'];?>]" id="check" value="1"></td>
<td><? echo $rows['Res_id'];?></td>
<td><? echo $rows['check_in'];?></td>
<td><? echo $rows['night_per_stay'];?></td>
<td><? echo $rows['R_no'];?></td>
<td><? echo $rows['pac_name'];?></td>
<td><? echo $rows['Cid']; ?></td>
</tr>
<?php
}?>

Then in your processing script:

<?php
if($_POST['submit_button'] == 'Update'){
	echo "Update<br>";
	echo '<pre>' . print_r($_POST['check'],true) . '</pre>';

}

if($_POST['submit_button'] == 'Delete'){
       echo "Delete<br>";
	echo '<pre>' . print_r($_POST['check'],true) . '</pre>';
}


?>

 

Ken

Link to comment
Share on other sites

ok if i do dat... how can i query my table using the id??

for e.g updating... when i check a specific column then then i just gonna get the id from the checkbox to update my table... so how to get the id from my 1st code to second code.. and also to my 3rd code..

Link to comment
Share on other sites

make your inputs like so:

 

<input type="checkbox" name="check[]" id="check" value="1">

 

This will make them all an array (Each value needs to be unique).

 

then do this:

 

<?php
if(isset($_POST['btnDelete'])){
     foreach($_POST['check'] as $value){
          mysql_query("DELETE FROM tableName WHERE id = '$value'");
     }
}else{
     foreach($_POST['check'] as $value){
          mysql_query("UPDATE tableName SET value = 'Some Value' WHERE id = '$value'");
     }
}
?>

 

My Example: http://phpsnips.com/snippet.php?id=11

Link to comment
Share on other sites

When you use my method, each key of the returned array is set to the value of the record id to be deleted/updated. Just loop through the returned array:

<?php
if (isset($_POST['check']))
    foreach($_POST['check'] as $rec_id => $dmy) {
//
//  create the mysql_statement you need using $rec_id
//
}
?>

 

Ken

Link to comment
Share on other sites

<input type="checkbox" name="check[]" id="check" value="1">

bro how can u delete if u didnt get the res_id.. this code wont get the res_id coz it is not specified..

i think must put

value=" <? echo $row['Res_id'];?>"

so it will send the res_id to server and process it..??

Link to comment
Share on other sites

wats wrong wid this..
i try to get the id and delete it
[code<?php
@mysql_connect ("localhost", "root", "tmc") or die (mysql_error());
@mysql_select_db("tgp")or die(mysql_error());

$ch=$_POST['check'];


if($_POST['button'] == 'Delete'){
for($i=0;$i<$count;$i++){
$del_id = $ch[$i];
$sql = "DELETE FROM reservation WHERE Res_id='$del_id'";
$result = mysql_query($sql);
}

if($result){
echo "<script> window.location ='history.php'";
}
}

geeezz.. have u created deleting multiple rows using checkbox?

Link to comment
Share on other sites

Try something like this:

 

index.php

<?php
$sql="SELECT * FROM $tbl_name join packages on reservation.pac_id = packages.pac_id where Cid ='$id'";
$result=mysql_query($sql);
echo '<form action="process.php" method="post">';
while($row = mysql_fetch_array($result)){
echo '<tr>
<td><input type="checkbox" name="check[]" value="'.$row['Res_id'].'"></td>
<td>'.$rows['check_in'].'</td>
<td>'.$rows['night_per_stay'].'</td>
<td>'.$rows['R_no'].'</td>
<td>'.$rows['pac_name'].'</td>
<td><'.$rows['Cid'].'</td>
<tr>';
}
echo '<tr><td><input type="sbumit" name="delete" value="DELETE ROWS">
<input type="sbumit" name="update" value="UPDATE ROWS"></td></tr>';
echo '</form>';
?>

 

process.php

<?php
if(isset($_POST['delete'])){
     foreach($_POST['check'] as $value){
          mysql_query("DELETE FROM tableName WHERE id = '$value'");
     }
}else{
     foreach($_POST['check'] as $value){
          mysql_query("UPDATE tableName SET value = 'Some Value' WHERE id = '$value'");
     }
}
header("Location: index.php");
exit;
?>

Link to comment
Share on other sites

Once you have a check box for each item listed

 

<input type="checkbox" name="check[]" value="'.$row['Res_id'].'">

 

Then you can delete all the selected items with a single query instead of a loop to delete them one by one:

$items = join(',' , $_POST['check']);
mysql_query ("DELETE FROM tablename WHERE id IN ($items)");

Link to comment
Share on other sites

Once you have a check box for each item listed

 

<input type="checkbox" name="check[]" value="'.$row['Res_id'].'">

 

Then you can delete all the selected items with a single query instead of a loop to delete them one by one:

$items = join(',' , $_POST['check']);
mysql_query ("DELETE FROM tablename WHERE id IN ($items)");

 

Nice! I have always wanted to know how to do that, but nobody ever answers me that question.

Link to comment
Share on other sites

$items = join(',' , $_POST['check']);
$sql="DELETE FROM reservation WHERE Res_id IN ($items)";

if(isset($_POST['delete'])){
		if(!mysql_query($sql)){?>
		<div style=" width: 450px; background:black;filter:alpha(opacity=75); -moz-opacity:.75;opacity:.75; margin-left: 0; margin-right:0;"><?php echo "Deleting Information Failed!";
			echo $items;
		?>
		</div><?php

 		}
 		else {?><script> window.location="history.php"</script><?}
}


@mysql_close();

 

i try to use ur code but its stil not workin...

its still doesnt get the id on the checkbox

Link to comment
Share on other sites

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.