Page 2 of 3

Re: delete and update of user records

Posted: Sat Jan 22, 2011 12:30 am
by social_experiment
I think you should place this code on a seperate page and add form tags to your existing form. The error is probably still stemming from the foreach() statement. My thinking is that because $_POST['checkbox'] is not an array when the page loads (before submitting) it gives an error.

Code: Select all

<?php
foreach ($_POST['checkbox'] as $value) {
    $query = "DELETE * FROM recommendations WHERE entryid = '". mysql_real_escape_string($value) ."' ";
    $deleteSql = mysql_query($query);
 }
?>
Hth

Re: delete and update of user records

Posted: Sat Jan 22, 2011 4:26 am
by timoteo
But can I send the form to 2 separate pages - one to delete and one to update? I understand that I am not getting the variable. How can I select one entry from my displayed rows to delete or update?

Re: delete and update of user records

Posted: Sat Jan 22, 2011 8:36 am
by social_experiment
timoteo wrote:But can I send the form to 2 separate pages - one to delete and one to update? I understand that I am not getting the variable. How can I select one entry from my displayed rows to delete or update?
You probably could but then you would have to check which action is to take place. With delete (using the form) you check if the post button has been set. Your update option uses an anchor tag so you can simply check if the query string value is set.

Code: Select all

<?php
 // this is your single page (if you want to use a single page)
 if (isset($_POST['yourPostButton'])) {
 // delete records
 }
 if (isset($_GET['yourUpdateValue'])) {
 // update record
 }
?>

Re: delete and update of user records

Posted: Wed Jan 26, 2011 4:22 am
by timoteo
OK, still here with this little problem. I have been scouring forums and tutorials and this is the best I have come up with. The update works fine - redirecting to update page with checkbox value. No probs. The delete however does the same - updates. I need it to delete checked values and refresh page when delete is checked.
I can't figure it out :(
this could be the problem:

Code: Select all

<form action="recommendationsupdate.php" method="get" enctype="application/x-www-form-urlencoded" name="myrecommendations" target="_self" id="myrecommendations">
or something here:

Code: Select all

     <input name="update" type="submit" id="update" value="update">  
   <input name="delete" type="submit" id="delete" value="delete" onClick = "return confirm('Are you sure you want to delete this recommendation?')">
 
 </div></td>
 </tr>


</table>
<?php 
if

($_POST['delete']=='delete'){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE*FROM $recommendations WHERE numid='$del_id'";
$result = mysql_query($sql);
}
}
if($result)
{

echo "<meta http-equiv=\"refresh\" content=\";myrecommendations.php\">";


mysql_close();


}
else if ($_GET['update']=='update')
{
?>
<td><a href="recommendationsupdate.php" action = update=$_GET['checkbox']>update</a><td/>
<?php

?>
<?php
}

?>	 
	 

Re: delete and update of user records

Posted: Wed Jan 26, 2011 9:37 am
by social_experiment
Do both actions take place on this page recommendationsupdate.php ?

Re: delete and update of user records

Posted: Wed Jan 26, 2011 10:03 am
by timoteo
delete yes, update no

Re: delete and update of user records

Posted: Wed Jan 26, 2011 11:00 am
by social_experiment

Code: Select all

<?php
if ($_POST['delete']=='delete'){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE*FROM $recommendations WHERE numid='$del_id'";
$result = mysql_query($sql);
}
?>
Where do you define the value of $count? It's easier to user a foreach loop,

Code: Select all

<?php
foreach ($_POST['checkbox'] as $value) {
 $id = $value;
 $sql = "DELETE * FROM table WHERE id = '". $id ."' ";
 $result = mysql_query($sql);
}  
?>

Re: delete and update of user records

Posted: Thu Jan 27, 2011 5:12 am
by timoteo
I don't know if it is possible what I wish to do - It is for reasons of presentation - I want the update to be on a separate page from the delete - one form 2 actions. This is what I have come up with taking your ideas on board... but nothing happens. If the update button is pressed I want just a simple redirect to another page (with checkbox as value to display relevant results). If the delete button is pressed I want delete action on relevant results.
Is this a job for javascript?

Code: Select all

<?php 
if

($delete) {
foreach ($_POST['checkbox'] as $value) {
$id = $value;
$sql = "DELETE * FROM table WHERE idnum = '". $id ."' ";
$result = mysql_query($sql);
}
?>
<form action="myrecommendations.php" method="post" enctype="application/x-www-form-urlencoded" name="myrecommendations" target="_self" id="myrecommendations">
<?php
}


else if ($update)
{
?>
<form action="recommendationsupdate.php" method="get" enctype="application/x-www-form-urlencoded" name="recommendationsupdate" target="_parent" id="recommendationsupdate">
<?php
}
?>	 
	 
	    <p>
        </p>
      </div>
    </form>

Re: delete and update of user records

Posted: Thu Jan 27, 2011 6:47 am
by social_experiment
timoteo wrote:I want the update to be on a separate page from the delete - one form 2 actions
I don't think you can have two action attributes for one form.
timoteo wrote:If the update button is pressed I want just a simple redirect to another page (with checkbox as value to display relevant results). If the delete button is pressed I want delete action on relevant results.
You will have to create another form, or have the update option accessed via a hyperlink. If you use the hyperlink, you can pass the value ($id) along in a query string.

Re: delete and update of user records

Posted: Thu Jan 27, 2011 7:07 am
by timoteo
You will have to create another form, or have the update option accessed via a hyperlink. If you use the hyperlink, you can pass the value ($id) along in a query string.
That sounds like a great idea, however how can I attach and pass the value along. This is what I had tried but there is no value passed on.

Code: Select all

<a href="recommendationsupdate.php" title="recommendationsupdate" accesskey="u" value =  <?php echo $_GET['checkbox'] ?>>update

Re: delete and update of user records

Posted: Thu Jan 27, 2011 7:30 am
by timoteo
I tried this again for the delete:

Code: Select all

<?php
foreach ($_POST['checkbox'] as $value) {
 $id = $value;
 $sql = "DELETE * FROM table WHERE id = '". $id ."' ";
 $result = mysql_query($sql);
}  
?>
but got this:
Warning: Invalid argument supplied for foreach() in /home/... on line 283
The checkbox is this:

Code: Select all

<input name="checkbox" type="checkbox" value=<?php echo $row_rsrecommendations['idnum']?>>

Re: delete and update of user records

Posted: Thu Jan 27, 2011 10:49 am
by social_experiment

Code: Select all

<a href="recommendationsupdate.php" title="recommendationsupdate" accesskey="u" value =  <?php echo $_GET['checkbox'] ?>>update
There is no value attribute for the anchor element :) Your code has to look like this

Code: Select all

<a href="recommendationsupdate.php?id='. dbValueHere .'" title="recommendationsupdate" accesskey="u">Update</a> 
In the same above dbValueHere is a value, retrieved from the database. Only on the page that does the update can you use $_GET to access the value. Currently your syntax (html) is incorrect and the php engine is looking for $_GET['checkbox'] in the query string but doesn't find it, which leaves 'value' empty.

Code: Select all

<input name="checkbox" type="checkbox" value=<?php echo $row_rsrecommendations['idnum']?>>
If you want the checkboxes to be an array you have to change your html slightly

Code: Select all

<input name="checkbox[]" type="checkbox" value="<?php echo $row_rsrecommendations['idnum']; ?>" />
Now any the $_POST['checkbox'] contains an array (or is one) and this will stop your foreach argument from giving the error.

Hth

Re: delete and update of user records

Posted: Thu Jan 27, 2011 3:17 pm
by timoteo
THanks a lot for the href links - that is working great and passing along the id.
The delete however is not working . I try and try.

Code: Select all

<td><input name="checkbox[]" type="checkbox" value="<?php echo $row_rsrecommendations['idnum']; ?>" />
This yields no output - and when I echo out $_POST['checkbox'] it just echos "array" (no id). Obviously the delete is not working for the same :?

Re: delete and update of user records

Posted: Thu Jan 27, 2011 4:11 pm
by social_experiment
timoteo wrote:This yields no output - and when I echo out $_POST['checkbox'] it just echos "array" (no id).
Yes it will do that because the html was changed to tell php that $_POST['checkbox'] should be an array. Do you still get the foreach error?

Re: delete and update of user records

Posted: Thu Jan 27, 2011 4:28 pm
by timoteo
Yes I do. I had assumed that this was because there is no value coming out of checkbox.