2012-08-29 142 views
0

我想在使用php從mysql刪除特定行之前發出警告,這裏是我的代碼,請告訴我該怎麼做?刪除腳本工作正常。想要在從mysql刪除行之前放置警告觸發器使用php

這是我用戶-delete.php

<?php include("../includes/config.php"); ?> 
<?php 

if ($_SESSION["isadmin"]) 
{ 

$con=mysql_connect($dbserver,$dbusername,$dbpassword); 
if (!$con) { die('Could not connect: ' . mysql_error()); } 

mysql_select_db($dbname, $con); 
$accountid=$_GET["id"]; 
$result = mysql_query("SELECT * FROM accounts WHERE (id='".$accountid."')"); 
while($row = mysql_fetch_array($result)) 
{ 
    $id=$row['id']; 
    $firstname = $row['firstname']; 
    $lastname = $row['lastname']; 
    $password = $row['password']; 
    $email=$row['email']; 
    $type=$row['type']; 
} 
mysql_close($con); 
?> 
<!DOCTYPE HTML> 
<html> 
<head> 
<title>Delete User</title> 
<link rel="StyleSheet" href="../admin/css/style.css" type="text/css" media="screen"> 
</head> 
<body> 
<?php include("../admin/includes/header.php"); ?> 
<?php include("../admin/includes/nav.php"); ?> 
<?php include("../admin/includes/manage-users-aside.php"); ?> 
<div id="maincontent"> 

<div id="breadcrumbs"> 
    <a href="">Home</a> > 
    <a href="">Manage Users</a> > 
    <a href="">List Users</a> > 
    Delete User 
</div> 
<h2>Delete User</h2> 

<form method="post" action="users-delete-action.php"> 
<input type="hidden" value="<?php echo $accountid; ?>" name="id" /> 
<label>Email/Username:</label><input type="text" name="email" value="<?php echo $email;  ?>" /><br /><br /> 
<label>Password:</label><input type="password" name="password" value="<?php echo $password;?>" /><br /><br /> 
<label>First Name:</label><input type="text" name="firstname" value="<?php echo $firstname; ?>" /><br /><br /> 
<label>Last Name:</label><input type="text" name="lastname" value="<?php echo $lastname; ?>" /><br /><br /> 
<label>Type:</label><br /> 
<input type="radio" name="type" value="S" <?php if ($type == 'S') echo 'checked="checked"'; ?> />Student<br /> 
<input type="radio" name="type" value="T" <?php if ($type == 'T') echo 'checked="checked"'; ?> /> Teacher<br /> 

    <input type="submit" value="Delete" /> 
</form> 



</div> 

</body> 
<?php include("../admin/includes/footer.php"); ?> 
</html> 
<?php 
} 
else 
{ 
    header("Location: ".$fullpath."login/unauthorized.php"); 

} 
?> 

這是用戶 - 刪除 - action.php的

<?php include("../includes/config.php");?> 
<?php 

$id=$_POST["id"]; 
$con=mysql_connect($dbserver,$dbusername,$dbpassword); 
if (!$con) { die('Could not connect: ' . mysql_error()); } 
mysql_select_db("ombts", $con); 
$query=("DELETE FROM accounts WHERE id='".$id."'"); 
$result=mysql_query($query); 
if($result){ 
echo "User has been Deleted Successfully!!"; 
}mysql_close($con); 
?> 

請大家幫忙,我會感激:)

+1

https://developer.mozilla.org/en-US/docs/DOM/window.confirm –

回答

0

1的解決辦法是隻修改窗體:

<form name="form" method="post" action="users-delete-action.php"> 
<input type="hidden" value="<?php echo $accountid; ?>" name="id" /> 
<label>Email/Username:</label><input type="text" name="email" value="<?php echo $email;  ?>" /><br /><br /> 
<label>Password:</label><input type="password" name="password" value="<?php echo $password;?>" /><br /><br /> 
<label>First Name:</label><input type="text" name="firstname" value="<?php echo $firstname; ?>" /><br /><br /> 
<label>Last Name:</label><input type="text" name="lastname" value="<?php echo $lastname; ?>" /><br /><br /> 
<label>Type:</label><br /> 
<input type="radio" name="type" value="S" <?php if ($type == 'S') echo 'checked="checked"'; ?> />Student<br /> 
<input type="radio" name="type" value="T" <?php if ($type == 'T') echo 'checked="checked"'; ?> /> Teacher<br /> 

    <input type="button" value="Delete" onClick="if (confirm('Are you sure you want to delete ?')) document.form.submit(); return false;">" /> </form>​ 

注意:

您正在使用要刪除的方法是非常不安全的。如果有人起訴螢火蟲編輯隱藏的輸入,他可以刪除任何人!

編輯:小提琴鏈接 - http://jsfiddle.net/M7fG2/

+0

如果這不是一個好的方式來發出這個警告..還有什麼我應該做的,以防止這種危險??你上面告訴的代碼是一個JavaScript ??我從來沒有使用Java腳本...我對不起,但我應該在哪裏使用document.forms [「form」]。submit(); ???? –

+0

'document.forms [「form」]。submit();'在onClick事件中 - 'onClick =「如果(確認('你確定要刪除?'))document.forms [「form」]。submit(); return false;「>」' – Cosmin

+0

我把一個工作代碼的例子在小提琴 - http: //jsfiddle.net/M7fG2/你可以檢查那裏的代碼 – Cosmin

1

使用提交按鈕上的這個按鈕:

<input type="submit" value="Delete" onclick="return confirmSubmit()" /> 
+0

+1這樣一個簡約但工作的解決方案! – Jeff

+0

我已經使用它就像你告訴,但它不工作..我的意思是它的dleting用戶沒有任何警告.. –

+0

你應該檢查JavaScript是否工作或不在您的網頁上。 – Krishna

2

連接確認上提交

<form method="post" action="users-delete-action.php" 
onsubmit="return confirm("Are you sure you wish to delete?");"> 
+0

使用它不工作:( –