Help with code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Help with code

Post by nigma »

I want to make a site that prints out all the rows in the MySQL database, but printing each row come after a diff radio button. Then depending on which radio button you choose it will delete the row that you selected. Here is what I have so far:

<?

require("util.php");
$sql = new MySQL_class;
$sql->Create("felist");

echo("<h2>Enemy Posts:</h2><br>");
echo("</div>");
echo("<form action=\"delete.php\" method=\"post\">\n");
echo("<input type=\"hidden\" name=\"update\" value=\"1\">\n");
$sql->Query("Select ID, EName from eLIST order by ID");
for ($i = 0; $i < $sql->rows; $i++) {
$sql->Fetch($i);
$ID = $sql->data[0];
$ename = $sql->data[1];

echo("<input type=\"radio\" name=\"$ID\">Post ID:&nbsp$nbs;$ID&nbsp&nbsp&nbspEnemy name:&nbsp$ename</input><br>");
}

echo("<br><input type=\"submit\" value=\"Delete Enemy\"></form><br>\n");

?>

But I dont know how to read what radiobutton the user checked. So basically I need a script that can tell what radio button was checked using the data provided(or some altered data if you have any ideas) that will take the $ID and delete it from the MySQL dbase. Can you offer me some ideas or even give me some pseudocode?

Thanks for all help provided.
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

HTML basics: Radio buttons are grouped by name and only one value or none is used in the request..

So you want to change the radio field to use the same name on each row and the value of the id..

<input type=radio name=DeleteThisId value="<?php echo $id; ?>">

and just fetch that with

$qry = 'DELETE FROM tablename WHERE some_id='''.$_REQUEST[''DeleteThisId'].'''';

edit/add: and you probably want to add some security, such as adding this line before
if (!is_integer($_REQUEST[''DeleteThisId'])) die ('Invalid selection');
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

So. I have the script that echos out all rows in a table, creates a radio button for each row and assings the value of that radio button to the ID of the row it represents. It does that until there are no more rows in the table. The user clicks the 3rd radio button which happens to be representing the row with an ID of three, then clicks submit. How do I actually carry out the deletion of that row?

I mean I am posting all data in the form to the script entitled delete.php, What would it look like if lets say my username is test and pass test for the MySQL database on localhost?

I dont mean to be jerking you round like this. I am just having trouble putting the whole thing together.
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

uhm I don't really follow you now.. this you mean?

Code: Select all

<?php
  if (!is_numeric($_REQUEST['DeleteThisId'])) die ('Invalid selection');  
  $qry = "DELETE FROM tablename WHERE some_id='".mysql_escape_string(stripslashes($_REQUEST['DeleteThisId']))."'";

  $db = mysql_connect('localhost','username','password');
  if (!$db) die ('Unable to connect to db');
  if (!mysql_select_db('mydbname',$db)) die ('Unable to select db');

  $r = mysql_query($qry,$db);

   if (!$r) die ('Query failed: '. mysql_error());

   echo mysql_affected_rows($r) . ' rows deleted.';
?>
the escape string and stripslashes arent realy needed, I just make it a rule of thumb to always use that in a query when using request variables (most/default php installs use magic quotes, therefor stripslashes).

Edit: phpBB is messing up on escaped quotes here.. changed the code a little bit..
DeGauss
Forum Contributor
Posts: 105
Joined: Tue Oct 22, 2002 9:44 am
Location: Gainesville, FL

Post by DeGauss »

Do you mean to be using a database extraction layer?

I mean, if it's not phpBB you're trying to mod (or whatever) and you know you're using MySQL, then why use an abstraction layer?

But then, what do i know...

Anyway

$sqlBlah=mysql_query("select blah from blah where blah='blah' asc");
while ($blahArray=mysql_fetch_array($sqlBlah)) {
print $blahArray["blah"]." <input type='radio' name='switch' value='on'";
if ($blahArray["blah"]=="blah") {
$isChecked=" checked";
} else {
$isChecked="";
}
print $isChecked.">";
}

And so on.
User avatar
puckeye
Forum Contributor
Posts: 105
Joined: Fri Dec 06, 2002 7:26 pm
Location: Joliette, QC, CA
Contact:

Post by puckeye »

DeGauss wrote: print $blahArray["blah"]." <input type='radio' name='switch' value='on'";
if ($blahArray["blah"]=="blah") {
$isChecked=" checked";
} else {
$isChecked="";
}
print $isChecked.">";
For the Checked keyword I prefer to use the following code:

Code: Select all

print $blahArray&#1111;"blah"]." <input type='radio' name='switch' value='on'".($blahArray&#1111;"blah"] == "blah" ? " CHECKED" : "").">";
Ìt's shorter and all on one line...
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

all on one line and shorter is not always easily readable... btw using echo/print and double quotes when there is no need to is a waste of resources..

<?php foreach( $mystuff as $stuff ) { ?>
<input type="radio" name="myradio" value="<?php
echo htmlspecialchars($stuff['key']);
?>" <?php
if (!strcmp($stuff['key'],$_REQUEST['myradio'])) {
?>checked="1"<?php } ?>><?php
echo htmlspecialchars($stuff['name']); ?>

That may be going a little overboard :p
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Thanks a lot guys/girls. That worked. Now in order to truly benifet from your help I just have to spend some time looking at the code and evaulating it so I actually understand what is going on. Thanks again, help is greatly appreciated.
Post Reply