Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add button to Mysql Results ROW!!

Status
Not open for further replies.

patrickstrijdonck

Programmer
Jan 18, 2009
97
NL
Hello all,

Im have a simple table wichs picks alot of records from a table

What I want can be solved with 2 things,

For each row: a checkbox, if the checkbox is checked (when button pressed) that record wich is checked must update (also the other records must also update)

For each row: a button, if button is presses, that record must update something,


What i have managed:

Ive managed to get a button for every row, but when its clicked ALL records will update :S

If someone can please explaine how this is done :D


Thanks in advance

Patrick
 
Hi

Patrick said:
Ive managed to get a button for every row, but when its clicked ALL records will update :S
I suppose all [tt]input[/tt]s have the same [tt]value[/tt], for example "Update". But their [tt]name[/tt]s are different, right ?

Feherke.
 
exactly example:

Mysql gets all records from family members:


Name last name status Action

Patrick Jurly Fail Button
Linda Jurly Fail Button
Mario Bros OK Button


Now when the button for Mario is pressed, a piece of code, wich is related to all buttons.

Example:

IF button is pressed THEN
SQL query UPDATE status = Fail WHERE Name = Mario (to lazy to write the exact php code but i know that,)

now when button is pressed (and no matter what) ALL records will go to Fail.

What im trying to do is, when Mario button is pressed ONLY mario record must UPDATE

If i press the button of Patrick, only that record must update with FAIL

Hope this clears it up :D
 
i dont know what u mean,

if you mean the names of the buttons, then NO

this is the button that will show for each record

Code:
<input type="submit" name="UPDATE" id="UPDATE" value="UPDATE" />

 
Hi

Then you will have no chance to guess on server side which of them was pressed. When you generate them, put the record identifier into it. ( Make sure it is valid according to SGML basic types. )
Code:
<input type="submit" name="[red]<?php echo $name; ?>[/red]" value="UPDATE" />
By the way, [tt]id[/tt] means identifier. Identifiers must be unique.

Feherke.
 
would it be easier to add a hidden field into the form for the mario record? i'm assuming that you have one form for each row.

or use a GET request with the ID appended as a query string?
 
Hi

jpadie said:
i'm assuming that you have one form for each row.
Patrick said:
For each row: a checkbox
If using checkboxes could be an alternative solution then I suppose there is a single [tt]form[/tt].

One more possibility to mention is adding JavaScript dependency :
Code:
<input type="submit" name="UPDATE" value="UPDATE" [red]onclick="this.value='Mario'"[/red]/>

[gray]<!-- or -->[/gray]

[red]<input type="hidden" name="which"/>[/red]

<input type="submit" name="UPDATE" value="UPDATE" [red]onclick="this.form.which.value='Mario'"[/red]/>
Or something similar.

Feherke.
 
Code:
<input type="checkbox" name="tids[]" value="<?=$row['Personeelsnummer']?>" onClick="highLight(this.value,this.checked);">

this is what i have for each row now.....

only now i need 1 button wich will look every row if checkbox is checked, if so, Update the record

any idea?
 
this is a Cell in the table wich contains the checkbox.

Code:
td valign="middle">
<div align="center">
            <? $recht = $_SESSION['recht'];
									  $query  = "SELECT * FROM personeel WHERE recht = '$recht'";
$result = mysql_query($query);

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{  ?>          <input type="checkbox" name="tids[]" value="<?=$row['Personeelsnummer']?>" onClick="highLight(this.value,this.checked);">
 <? } ?>
      </div>
   
</td>

This is the button wich is placed outside the table but still in the form

Code:
<label>
  <input type="submit" name="UPDATE" id="UPDATE" value="UPDATE" />
</label>

When that button is pressed, this IF will run:

Code:
if ($_POST['UPDATE'] == 'UPDATE')
{ "The piece of code wichs checks if a checkbox is checked, if so, UPDATE that record. this must be done for each record"
}

The only thing i still need is the little piece of code for the UPDATE itself,

at first i got a String in each row "$ID", wich says wich ID it was

then i used this SQL:

Code:
$query1="UPDATE personeel SET status = 'Fail' WHERE Personeelsnummer = '$ID'";

but this wont check if the checkbox is checked.
 
Hi

The [tt]tids[][/tt] notation determines PHP to put the values in an array. So if you check the checkbox which look like this :
HTML:
<input type="checkbox" name="tids[]" value="66">
The value of the $_POST array will be like this :
Code:
Array
(
    [tids] => Array
        (
            [0] => 66
        )

    [UPDATE] => UPDATE
)
So basically your code should look like this :
PHP:
if ($_POST['UPDATE'] == 'UPDATE' [red]&& count($_POST['tids'])[/red]) {
  [red]$ID=[url=http://php.net/mysql_real_escape_string/]mysql_real_escape_string[/url]($_POST[tids][0]);[/red]
  $query1="UPDATE personeel SET status = 'Fail' WHERE Personeelsnummer = '$ID'";
  [gray]// ...[/gray]
}
Of course, if you want to handle multiple checked checkboxes there will be abit more to do.

Feherke.
 
Ah, thats what i was looking for, only indeed, i want to handle multiple checked checkboxes,

 
Hi

Ok, then just compose a list of values and use it in you SQL statement :
Code:
if ($_POST['UPDATE'] == 'UPDATE' && count($_POST['tids'])) {
  [red]$idlist=array();
  foreach ($_POST['tids'] as $one) $idlist[]="'".mysql_real_escape_string($one)."'";
  $ID=join(',',$idlist);[/red]
  $query1="UPDATE personeel SET status = 'Fail' WHERE Personeelsnummer [red]in ([/red]$ID[red])[/red]";
  [gray]// ...[/gray]
}

Feherke.
 
Your my savior :D

It works !!!!

i was trying for weeks now,...

still 1 more question, IF no checkbox is selected,
then the whole code may not execute and a alert must come saying: You have not selected any checkboxes


I was thinking about an IF rule wichs looks at [tids] if that is 0 or maybe if the array is 0, dont know if thats right???
 
Hi

I already added that to the [tt]if[/tt]'s condition. If you want to treat it separately, then move it in a separate [tt]if[/tt] and add it an [tt]else[/tt] too :
Code:
if ($_POST['UPDATE'] == 'UPDATE'[red]) {
  if ([/red]count($_POST['tids'])) {
    $idlist=array();
    foreach ($_POST['tids'] as $one) $idlist[]="'".mysql_real_escape_string($one)."'";
    $ID=join(',',$idlist);
    $query1="UPDATE personeel SET status = 'Fail' WHERE Personeelsnummer in ($ID)";
    [gray]// ...[/gray]
  [red]} else echo 'You have not selected any checkboxes';[/red]
}

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top