2015-07-12 36 views
0

我希望用戶能夠從數據庫中刪除他們的帳戶,這將通過點擊我的php論壇中的按鈕來完成,看代碼下面。如何使用我的php變量UserNameID刪除數據庫中的用戶行

<div class="btn-group"> 
    <button style="width:200px" type="button" class="btn btn-primary">Change details</button> 
    <button style="width:200px" type="button" class="btn btn-primary">Add details</button> 
    <button onclick="myFunction()" style="width:200px" type="button" class="btn btn-primary">Delete details</button> 
    </div> 

我已經有數據庫連接設置

<?php 
$connection = mysql_connect('localhost', 'root', 'Oliver'); 
mysql_select_db('users');  
$query = "SELECT * FROM username WHERE username='$login_session'"; 
$result = mysql_query($query);  
while($row = mysql_fetch_array($result)){  
$name = $row['name']; 
$username = $row['username']; 
$sex = $row['sex']; 
$phone = $row['phone']; 
$email = $row['email']; 
$dob = $row['dob']; 
$image = $row['imagelink']; 
$hobby = $row['hobby']; 
$bio = $row['Bio']; 
$Level = $row['Level']; 
$UserNameID = $row['UserNameID']; 
}  
mysql_close(); 
?> 

的數據庫行將由PHP變量UserNameID這是從數據庫

$UserNameID = $row['UserNameID']; 

如果您需要任何更多的信息來識別請給我一個迴應,我很新的編碼,因此需要合作:D非常感謝

更新 反而不喜歡,請告訴我,我在做什麼錯的,那將是非常感激

更新

這是會話代碼

<?php 
// Establishing Connection with Server by passing server_name, user_id and password as a parameter 
$connection = mysql_connect("localhost", "root", "Oliver"); 
// Selecting Database 
$db = mysql_select_db("users", $connection); 
session_start();// Starting Session 
// Storing Session 
$user_check=$_SESSION['login_user']; 
// SQL Query To Fetch Complete Information Of User 
$ses_sql=mysql_query("select username from username where username='$user_check'", $connection); 
$row = mysql_fetch_assoc($ses_sql); 
$login_session =$row['username']; 
if(!isset($login_session)){ 
mysql_close($connection); // Closing Connection 
header('Location: home.php'); // Redirecting To Home Page 
} 
?> 

這只是記錄用戶如果在數據庫中的詳細信息匹配並啓動會話以解鎖其他頁面

+0

哪裏是你'的onclick = 「myFunction的()」'代碼? – Shehary

+0

你面臨什麼問題? '$ login_session'的值是多少?用戶的ID?然後只是刪除配置文件....簡單的是... – Umair

+0

感謝您的答覆,我很努力弄清楚我是如何設置按鈕按下刪除用戶名中位於數據庫中的行 –

回答

0

我認爲這會讓你更接近你的目標。

<div class="btn-group"> 
    <button style="width:200px" type="button" class="btn btn-primary">Change details</button> 
    <button style="width:200px" type="button" class="btn btn-primary">Add details</button 
    <form action="" method="post"> 
     <button type="submit" style="width:200px" class="btn btn-primary">Delete details</button> 
     <input type="hidden" name="UserName id="UserName" value="<?php echo $login_session;?>" /> 
    </form> 
</div> 

<?php 
$connection = mysql_connect('localhost', 'root', 'Oliver'); 
mysql_select_db('users');  
$query = "SELECT * FROM username WHERE username='$login_session'"; 
$result = mysql_query($query); 
//By the way you don't need loop here  
    while($row = mysql_fetch_array($result)){  
     $name = $row['name']; 
     $username = $row['username']; 
     $sex = $row['sex']; 
     $phone = $row['phone']; 
     $email = $row['email']; 
     $dob = $row['dob']; 
     $image = $row['imagelink']; 
     $hobby = $row['hobby']; 
     $bio = $row['Bio']; 
     $Level = $row['Level']; 
     $UserNameID = $row['UserNameID']; 
    } 
    //Here you delete record from database 
     if(isset($_POST['UserName'])){ 
     $user = mysql_real_escape_string($_POST['UserName']); 
     //Strongly suggest that use UserId to delete record not username 
     $sql = "DELETE FROM username WHERE username='$user' LIMIT 1"; 
    }  
mysql_close(); 
?> 

編輯根據OP請求

<?php 
$connection = mysql_connect('localhost', 'root', 'Oliver'); 
mysql_select_db('users'); 
if(isset($_POST['UserName'])){ 
$user = mysql_real_escape_string($_POST['UserName']); 
$query = "SELECT * FROM username WHERE username='$user'"; 
$result = mysql_query($query); 
$total = mysql_num_rows($result); 
$row = mysql_fetch_array($result); 
// You dont need to fetch all records just `$UserNameID` enough 
$name = $row['name']; 
$username = $row['username']; 
$sex = $row['sex']; 
$phone = $row['phone']; 
$email = $row['email']; 
$dob = $row['dob']; 
$image = $row['imagelink']; 
$hobby = $row['hobby']; 
$bio = $row['Bio']; 
$Level = $row['Level']; 
$UserNameID = $row['UserNameID']; 

//Here you delete record from database 
    if($total>0){ 
     $sql = "DELETE FROM username WHERE UserNameID='$UserNameID' LIMIT 1"; 
    } 
} 
mysql_close(); 
?> 
+0

好吧不只是100%困惑:D我是編程的超級新手,我明白問題在於bootstrap模式,並且您希望我創建一個新的php文檔,您希望我將它放在什麼位置以及應該如何保存它? –

+0

給我一下,wiil創建另一個pastebin – Shehary

+0

好的,謝謝隊友 –

相關問題