...

View Full Version : unable to delete a record using php



sesc
09-03-2007, 10:52 PM
I am having a form which displays a couple of values from database and a delete button which is used to delete that particular record. Below is the code that I am using, but I am unable to delete the record. Please help me to get the record deleted. Thanks in advance!

with the below php code I am displaying the username and password on the page along with a delete button
When clicked on the delete button I used a javascript function which takes me to another page where I am actually executing the delete record functionality. My problem is when I used input type as "button", I am expecting that the javascript function should get executed which is not happening now. If I use the input type as submit it is taking me to the next page which is specified in the action property. Here when button is clicked, first a confirm button should popup which should ask for the confirmation of the user. If the user continues, then the record should be deleted. If the user cancels, then control should be in the currentpage only.

<?php
@session_start();
include("dbconnect.php");
$nbr = $_GET['Nbr'];
$Query = "Select * from tab where nbr = $nbr";
$res = mysql_query($Query);
if(mysql_num_rows($res) >0)
{
$row = mysql_fetch_assoc($res);
Email : $row[eMail]";
$uname = "$row[uname]";
$pwd = "$row[pwd]";
}
?>

<html>
<head>
<script language="javascript" type="text/javascript" >
function deleteNbr()
{
if(confirm("This cannot be undone!"))
{
location.href = "DelRec.php";
}
}
</script>
</head>
<body>
<form name="deletefrm" method="POST" action="delrec.php">
<table>
<tr>
<td >username </td>
<td width="5">:</td>
<td><?php echo "$uname"?></td>
<td >&nbsp;</td>
<td >password:</td>
<td ><?php echo "$pwd"?></td>
<td><input name="delete" type="Submit" value="Delete " onClick="return deleteNbr();"/>
</td>
</tr>
</table>
</form>
</body>
</html>

anarchy3200
09-03-2007, 11:11 PM
Ok, basically at no point if the confirmation fails do you tell it to stop, the way i do it is without the external function:

<input name="delete" type="Submit" value="Delete " onClick="if (!confirm(\'This cannot be undone!\')) return false">

Just swap your submit button line for that, has always worked for me :P

ess
09-03-2007, 11:13 PM
I think your JavaScript is the cause of the problem...as you are performing a re-direct when you use location.href = "DelRec.php";

instead, if the user answers yes, you should "return true" which would tell the browser to send a "POST" request based on the "action" specified in the form tag.

so, change your javascript to the following.

<script language="javascript" type="text/javascript" >
function deleteNbr()
{
if(confirm("This cannot be undone!"))
{
return true; // submit form is user says yes.
} else {
return false; // don't submit form if user says no
}
}
</script>

let's know if that worked or not.

cheers,
ess

sesc
09-04-2007, 03:55 PM
above two solutions are not working.
If I use "Submit", record is deleted without confirmation.
If I use "Button", javascript error saying that property not supported.

My problem is there is a record displayed on the browser with a "delete" button below the record. If user wishes to delete the record, user should get confirmation and is yes, then that record should be deleted otherwise the control should stay there. can I do this without redirection. Then this is very much helpful for me. any help is appreciated.

sesc
09-04-2007, 04:33 PM
It is my mistake. above code is working perfectly fine. I did not close a HTML tag. Thanks for the help.

ess
09-05-2007, 11:19 AM
sesc....do you use JS Debugger?

if not, I recommend that you give FireBug a test drive...which is a plugin that you can add to Firefox. It is very handy when writing JS code...as it allows you to create break points etc...and hence, trace the line that is throwing the error.

you can download firebug from the following location
http://www.getfirebug.com/

Cheers,
Ess

diezaq
03-14-2009, 06:38 PM
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="ADUAN"; // Database name
$tbl_name="adu"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="400" border="1" cellspacing="0" cellpadding="3">


<tr>
<td align="center"><strong>Nama</strong></td>
<td align="center"><strong>Sekolah</strong></td>
<td align="center"><strong>Masalah</strong></td>
<td align="center"><strong>Maklumbalas</strong></td>
<td align="center"><strong>Delete</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>

<tr>
<td><?php echo $rows["Nama"]; ?></td>
<td><?php echo $rows["Sekolah"]; ?></td>
<td><?php echo $rows["Masalah"]; ?></td>
<td><?php echo $rows["Maklumbalas"]; ?></td>


<td align="center"><a href="delete_ac.php?id=<?php echo $rows['id']; ?>">delete</a></td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
</table>
<?php
mysql_close();
?>



delete_ac

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<?php

$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="ADUAN"; // Database name
$tbl_name="test_mysql"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// get value of id that sent from address bar
$id=$_GET['id'];

// Delete data in mysql from row that has this id
$sql="DELETE FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);

// if successfully deleted
if($result){
echo "Deleted Successfully";
echo "<BR>";
echo "<a href='delete.php'>Back to main page</a>";
}
<form name="form1" method="post" action="checklogin.php">
else {
echo "ERROR";
}

// close connection
mysql_close();

?>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum