PHP Admin

Hello all. I am currently trying to make a admin panel for a commenting script. I am learning php so things are difficult sometimes as one would know. My question is with my code below i have to click the delete button twice in order for the item to delete, I believe its the way i have it scripted, least i got that far on my own i guess. But this is making me crazy,lol … any help with this code is truly appreciated, Thanks

/*

<?php /*** mysql hostname ***/ $hostname = 'mine'; /*** mysql username ***/ $username = 'mine'; /*** mysql password ***/ $password = 'mine'; /*** mysql database name ***/ $dbname = 'mine'; /*** create a new mysqli object with default database***/ $mysqli = @new mysqli($hostname, $username, $password, $dbname); /* check connection */ if(!mysqli_connect_errno()) { /*** if we are successful ***/ echo 'Welcome to Adimn panel
'; /*** our SELECT query ***/ $sql = "SELECT id, qazi_id, name, url, email, description, date FROM comments"; /*** prepare statement ***/ if($stmt = $mysqli->prepare($sql)) { /*** execute our SQL query ***/ $stmt->execute(); /*** bind the results ***/ $stmt->bind_result($id, $qazi, $name, $url, $email, $description, $date); /*** loop over the result set ***/ while ($stmt->fetch()) { /*** echo our table rows ***/ echo ''; } } /*** close connection ***/ $mysqli->close(); } else { /*** if we are unable to connect ***/ echo 'Unable to connect'; exit(); } ?>
id
qazi id
name
url
email
description
date
delete
'.$id.'
'.$qazi.'
'.$name.';
'.$url.'
'.$email.'
'.$description.'
'.$date.'
<?php $con=mysqli_connect("mysql.1freehosting.com","u715455357_clb","dmb31508","u715455357_talk"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $which = $_REQUEST['name'] ; echo " Delete = ";

mysqli_query($con,“DELETE FROM comments WHERE id=’$which’”);

?>

table, td, th { border:1px solid green; } th { background-color:green; color:white; }

*/

Well honestly this whole script is a complete disaster but a big problem is that you don’t have the DELETE query in a if() so it’s running on pageload. Try changing these lines to what I have shown. Plus unless “name” is unique in your mysql table, it’s much wiser to use the id as that should be auto-increment and unique. This way you won’t accidentally delete more than one record.
[php]
// Your $which var is not needed cause the info needs to come from a posted input field.
echo "
Delete = // Notice I changed it to id, so now you enter the id of the row.

";

if(isset($_POST[‘submit’]))
{
$id = (int)$_POST[‘id’];
mysqli_query($con,“DELETE FROM comments WHERE id=’$id’”);
}
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service