jonoc33 Posted November 7, 2007 Share Posted November 7, 2007 Having trouble with this: In the admin section of my page i'm trying to find a way to delete a record off the database without having to type in the name of it in a box. So basically having a delete button. Here is what I have at the moment: {This is the form where you type in the name of the Scrim} scrimadmin.php <form method="POST" action="scrimdelete.php"> <label>Type name of server you wish to delete: <br /> <input name="servername" type="text" id="servername" size="40" /> </label> <input type="submit" name="submit" value="Submit" /> </p> </form> <form id="form1" name="form1" method="post" action=""> </form> You type in the Server Name of the scrim and hit submit and it takes you to scrimdelete.php, where this happens: <?php error_reporting(E_ALL); $con = mysql_connect("localhost","jonoc33","*****") or die('Could not connect: '.mysql_error()); mysql_select_db("*****", $con); mysql_query("DELETE FROM alpha_scrims WHERE servername='$_POST[servername]' ") or die(mysql_error()); mysql_close($con); echo "<center>Record Deleted."; echo "<center><a href=admin.php>Back</a>"; ?> That deletes it off the database through typing in the name of the record, which I don't want to have to do. I have a list of scrims which shows what is in the database, it has this code: <? $results = mysql_query("SELECT servername, ipaddress, time, date, password FROM alpha_scrims"); while($values = mysql_fetch_array($results)){ echo $values['servername']."<br />"; echo "IP: ".$values['ipaddress']."<br />"; echo "Time: ".$values['time'].", Date: ".$values['date']."<br />"; echo "Password: ".$values['password']."<br />"; } ?> It echos it like this: [sERVERNAME] [iP ADDRESS] [TIME], [DATE] [PASSWORD] I need it to say this: [sERVERNAME] [iP ADDRESS] [TIME], [DATE] [PASSWORD] [Delete] Pressing delete will delete it off the database. May sound extremely confusing but read through and you'll understand. Impossible it may seem! Jono Link to comment https://forums.phpfreaks.com/topic/76347-deleting-off-a-database-without-a-form/ Share on other sites More sharing options...
farkewie Posted November 7, 2007 Share Posted November 7, 2007 This should do it <?php $results = mysql_query("SELECT servername, ipaddress, time, date, password FROM alpha_scrims"); while($values = mysql_fetch_array($results)){ print $values['servername']."<br />"; print "IP: ".$values['ipaddress']."<br />"; print "Time: ".$values['time'].", Date: ".$values['date']."<br />"; print "Password: ".$values['password']."<br />"; print "<form name=\"form1\" method=\"post\" action=\"scrimdelete.php\">"; print "<input type=\"submit\" name=\"button\" id=\"button\" value=\"Delete ".$values['servername']." \">"; print "<input name=\"servername\" type=\"hidden\" id=\"servername\" value=\"".$values['servername']."\">"; print "</form>"; } ?> Link to comment https://forums.phpfreaks.com/topic/76347-deleting-off-a-database-without-a-form/#findComment-386564 Share on other sites More sharing options...
Foser Posted November 7, 2007 Share Posted November 7, 2007 nvm, delete this. Link to comment https://forums.phpfreaks.com/topic/76347-deleting-off-a-database-without-a-form/#findComment-386565 Share on other sites More sharing options...
aschk Posted November 7, 2007 Share Posted November 7, 2007 Does anyone else see this as being a dropdown box option? Pseudocode : [on display] while (reading server from db) { print next dropdown option } [on post] read $posted_server delete $posted_server from db Link to comment https://forums.phpfreaks.com/topic/76347-deleting-off-a-database-without-a-form/#findComment-386568 Share on other sites More sharing options...
farkewie Posted November 7, 2007 Share Posted November 7, 2007 If you want just one dropdown box its something like this <?php $results = mysql_query("SELECT servername, ipaddress, time, date, password FROM alpha_scrims"); print "<form name=\"form1\" method=\"post\" action=\"YOUR DELETE FILE\">"; print "<select name=\"servername\" id=\"servername\">"; while($values = mysql_fetch_array($results)){ print "<option value=\"".$values['servername']."\">".$values['servername']."</option>"; print "</select>"; } print "<input type=\"submit\" name=\"button\" id=\"button\" value=\"Delete ".$;values['servername']." \">"; print "</form>"; ?> Link to comment https://forums.phpfreaks.com/topic/76347-deleting-off-a-database-without-a-form/#findComment-386571 Share on other sites More sharing options...
RavenStar Posted November 7, 2007 Share Posted November 7, 2007 The below code will connect to your database and select the table, then list all your rows. IF "del" is set (eg. myscript.php?del=SomeServer) it will attempt to delete the given server name. As for your listed results it will display a DELETE link, which once clicked will ask if you're sure, if you click OK it will delete. Please excuse any typos in the script, I'm a little tired <? error_reporting(E_ALL); $con = mysql_connect("localhost","jonoc33","*****") or die('Could not connect: '.mysql_error()); mysql_select_db("*****", $con); if (isset($_GET["del"])) { mysql_query("DELETE FROM alpha_scrims WHERE servername='$_GET[del]' LIMIT 1") or die(mysql_error()); echo "Record was successfully deleted!"; } $results = mysql_query("SELECT servername, ipaddress, time, date, password FROM alpha_scrims"); $thisfile = $_SERVER['SCRIPT_FILENAME']; // This is only here because I don't know the name of your script file. while($values = mysql_fetch_array($results)){ echo $values['servername']."<br />"; echo "IP: ".$values['ipaddress']."<br />"; echo "Time: ".$values['time'].", Date: ".$values['date']."<br />"; echo "Password: ".$values['password']."<br />"; echo '<a onclick="return confirm("Are you sure you want to permently delete this record?\n\nPress OK to delete!");" href="$thisfile?del=<? echo $values["servername"]; ?>">DELETE</a><br />'; } mysql_close($con); ?> Link to comment https://forums.phpfreaks.com/topic/76347-deleting-off-a-database-without-a-form/#findComment-386739 Share on other sites More sharing options...
jonoc33 Posted November 14, 2007 Author Share Posted November 14, 2007 THANK YOU!!!! THIS FORUM IS A LIFESAVER! Link to comment https://forums.phpfreaks.com/topic/76347-deleting-off-a-database-without-a-form/#findComment-391236 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.