Jump to content

Deleting off a database without a form


jonoc33

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.