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: $nbs;$ID   Enemy name: $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.
Help with code
Moderator: General Moderators
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');
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');
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.
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.
uhm I don't really follow you now.. this you mean?
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..
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.';
?>Edit: phpBB is messing up on escaped quotes here.. changed the code a little bit..
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.
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.
- puckeye
- Forum Contributor
- Posts: 105
- Joined: Fri Dec 06, 2002 7:26 pm
- Location: Joliette, QC, CA
- Contact:
For the Checked keyword I prefer to use the following code:DeGauss wrote: print $blahArray["blah"]." <input type='radio' name='switch' value='on'";
if ($blahArray["blah"]=="blah") {
$isChecked=" checked";
} else {
$isChecked="";
}
print $isChecked.">";
Code: Select all
print $blahArrayї"blah"]." <input type='radio' name='switch' value='on'".($blahArrayї"blah"] == "blah" ? " CHECKED" : "").">";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
<?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