Forum Moderators: coopster
<html><SCRIPT language="JavaScript">
function submitform()
{
document.myform.submit();
}
</SCRIPT>
See interesting facts from this category: <br />
<form name="myform" action="delet.php" method="get">
<?php$con= mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect to the database' . mysql_error());
}mysql_select_db("facts", $con);
$result=mysql_query("select * from content");
while($row=mysql_fetch_array($result))
{$id=$row['id'] ;
echo $row['id']. '. '. $row['facts'] . ' <input type="button" value="Delete" name="'.$id.'" onclick="javascript: submitform()" /><br />' ;}
mysql_close($con);
?></form>
</html>
<?phpif(isset($_GET["name"])) { $a=$_GET["name"];
$con= mysql_connect("localhost","root","");if (!$con)
{
die('Could not connect to the database' . mysql_error());
}mysql_select_db("facts", $con);
$delete=mysql_query("delete from content where id='$a'") ;
if (!$delete)
{
die('Could not delete row');
}mysql_close($con); } else echo 'Error!';
?>
Actually what i want to do with these files is to delete rows from the database. Everything works perfect except $_GET["name"] . delet.php just doesnt want to take the information from the form in 1.php .
Somebody tell me please what i did wrong. Thanks in advance!
<input type="button" value="Delete" name="'.$id.'" onclick="javascript: submitform()" />
In the second file, change:
if(isset($_GET["name"])) { $a=$_GET["name"];
To
if(isset($_GET["Delete"])) { $a=$_GET["Delete"];
When you understand the changes, you can change the names as well, but what is mentioned above should solve your problem as it is. If you face any problem, please post it.
Habtom
Also, I would recommend doing an intval() or mysql_real_escape_string() on the input before you use it in your delete query--otherwise a malicious user could end up deleting much more than one row. :)
<input type="button" value="Delete" name="'.$id.'" onclick="javascript: submitform()" />
It seems the loop is going to generage a number of buttons.
Instead of the above line, see if the following works, it will provide you with a text link to delete the row.
<a href="delet.php?name=$id"> Delete </a>
echo '<pre>';
print_r [uk2.php.net]($_GET);
echo '</pre>';
And set your error reporting to E_ALL, that will help you debug many issues early on.
dc
Not a problem. Basically, $_GET is an array. Not sure how familiar you are with arrays, but when working with them, you can always see the values of arrays by using the above code. This is sometimes useful when using any of the superglobal arrays that PHP has. For information see the following:
[uk2.php.net...]
When you are coding, popping the following at the top of your script after the opening <?php tag activates error reporting:
error_reporting(E_ALL);
You`ll find that useful for future projects because anytime an error is encountered thats PHP related, the error will be shown on the screen. Sometimes a quick look at the error message can help you debug an issue, rather than staring at your code for hours and hours without any clues. You can also activate this error reporting level in your PHP.ini file if you have access to that. This is your server PHP config file.
You should always try and code your projects to run under E_ALL if possible. In the long run it will make you a better programmer.
Good luck,
dc