My implementation doesn’t seem to work. Can you point out what might be the problem or point me to a better solution? When I check the checkboxes and click the delete button, it doesn’t seem to do anything… please help me.
<div id="container" class="page">
<img id="disclaimer" class="page" src="images/DISCLAIMER.png" alt="" />
<img id="logo" class="page" src="images/MI-LOGO.png" alt="" />
<div id="postItDiv" class="page">
<?php
$cxn = mysqli_connect('localhost', 'root', 'root', 'TimePost') or die(mysqli_error());
$query = "SELECT PostID, ClientName, VideoName, PostTime, Post, Date FROM tblTimePosts";
$result = mysqli_query($cxn, $query) or die(mysqli_error());
while($row = mysqli_fetch_assoc($result)) {
$post = $row['Post'];
$time = $row['PostTime'];
$idNo = $row['PostID'];
$postColor = rand(1, 4);
echo '<div id="post1"><p class="postParagraph">Post ID No.' . $idNo . '<br />' . $post . ' at ' . $time . ' seconds mark</p><div class="close"><form method="POST" action="videoJudge.php"><input type="checkbox" name="checkbox[]" id="checkbox[]" value="' . $idNo . '" /></form></div></div>';
}
if($_SERVER['REQUEST_METHOD'] == "POST" && $_POST['btnPost']) {
$postColors = rand(1, 4);
$toPost = $_POST['textPost'];
$date = date("y-m-d");
$postTime = $_POST['display'];
$postTime = floor($postTime);
$insertPostQuery = "INSERT INTO tblTimePosts VALUES('','Mimagazine Asia','Chelsea','$postTime','$toPost','$date')";
$query3 = "SELECT PostID FROM tblTimePosts";
$result = mysqli_query($cxn, $insertPostQuery) or die(mysqli_error());
$result3 = mysqli_query($cxn, $query3) or die(mysqli_error());
if($result > 0) {
while($row = mysqli_fetch_assoc($result3)) {
$idNo2 = $row['PostID'];
}
echo '<div id="post1"><p class="postParagraph">Post ID No.' . $idNo2 . '<br />' . $toPost . ' at ' . $postTime . ' seconds mark</p><div class="close"><form method="POST" action="videoJudge.php"><input type="checkbox" name="checkbox[]" id="checkbox[]" value="' . $idNo2 . '" /></form></div></div>';
}
else
echo "Add Failed";
}
/* if($_POST['chkDelete']) {
for($i=0;$i<5;$i++){
$del_id = $checkDelete[$i];
$sql = "DELETE FROM $tblTimePosts WHERE PostID='$del_id'";
$resulta = mysqli_query($cxn,$sql);
}
} */
if($_POST['btnDelete']) { // from button name="delete"
/* $checkbox = $_POST['checkbox']; //from name="checkbox[]"
$countCheck = count($_POST['checkbox']);
echo $countCheck;
for($i=0;$i<$countCheck;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM tblTimePosts WHERE PostID=".$del_id."";
$resulta = mysqli_query($cxn,$sql) or die(mysqli_error());
} */
$tbl_name = 'tblTimePosts';
foreach($_POST['checkbox'] as $id => $value) {
$sql = 'DELETE FROM `' . $tbl_name . '` WHERE `PostID`=' . (int) $id;
mysqli_query($cxn, $sql);
}
header('Location: videoJudge.php');
}
?>
</div>
<div id="videoContainer" class="page">
<video id="video" poster="images/cover.png" controls>
<source src="video/animationTest.m4v" />
</video>
</div>
<div id="etc" class="page">
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="POST">
<textarea name="textPost" id="textPost">
</textarea>
<input type="submit" name="btnPost" id="btnPost" value="Post" />
<div id="tae">
<input type="text" name="display" id="display" value="0" />
</div>
</form>
</div>
<div id="delButtonDiv">
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="POST">
<input type="submit" value="Delete" name="btnDelete" />
</form>
</div>
</div>
HERE IS MY CODE NOW AFTER FOLLOWING ALL YOUR COMMENTS BUT STILL DIDNT WORK. 🙁
<?php
$cxn = mysqli_connect('localhost','root','root','TimePost') or die(mysqli_error());
$query = "SELECT PostID, ClientName, VideoName, PostTime, Post, Date FROM tblTimePosts";
$result = mysqli_query($cxn,$query) or die(mysqli_error());
while($row = mysqli_fetch_assoc($result)) {
$post = $row['Post'];
$time = $row['PostTime'];
$idNo = $row['PostID'];
$postColor = rand(1,4);
echo '<div id="post1"><p class="postParagraph">Post ID No.'.$idNo.'<br />'.$post.' at '.$time.' seconds mark</p><div class="close"><form method="POST" action="videoJudge.php"><input type="checkbox" name="checkbox[]" id="checkbox[]" value="'.$idNo.'" /></form>
</div></div>';
}
if ($_SERVER['REQUEST_METHOD'] == "POST" && $_POST['btnPost']) {
$toPost = $_POST['textPost'];
$date = date("y-m-d");
$postTime = $_POST['display'];
$postTime = floor($postTime);
$insertPostQuery = "INSERT INTO tblTimePosts VALUES('','Mimagazine Asia','Chelsea','$postTime','$toPost','$date')";
$query3 = "SELECT PostID FROM tblTimePosts";
$result = mysqli_query($cxn, $insertPostQuery) or die(mysqli_error());
$result3 = mysqli_query($cxn, $query3) or die(mysqli_error());
if ($result > 0) {
while($row = mysqli_fetch_assoc($result3)) {
$idNo2 = $row['PostID'];
}
echo '<div id="post1"><p class="postParagraph">Post ID No.'.$idNo2.'<br />'.$toPost.' at '.$postTime.' seconds mark</p><div class="close"><form method="POST" action="videoJudge.php"><input type="checkbox" name="checkbox[]" id="checkbox[]" value="'.$idNo2.'"
/></form></div></div>';
}
else
echo "Add Failed";
}
if($_POST['btnDelete'] == "Delete")
{
$tbl_name = 'tblTimePosts';
foreach ($_POST['checkbox'] as $id => $value)
{
$sql = 'DELETE FROM `'.$tbl_name.'` WHERE `PostID`='.(int)$id;
mysqli_query($cxn,$sql);
}
header('Location: videoJudge.php');
}
?>
</div>
<div id="videoContainer" class="page">
<video id="video" poster="images/cover.png" controls>
<source src="video/animationTest.m4v" />
</video>
</div>
<div id="etc" class="page">
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST">
<textarea name="textPost" id="textPost">
</textarea>
<input type="submit" name="btnPost" id="btnPost" value="Post" />
<div id="tae">
<input type="text" name="display" id="display" value="0" />
</div>
<div id="delButtonDiv">
<input type="submit" value="Delete" name="btnDelete" />
</div>
</form>
</div>
Doing this should solve the problem, try:
[EDITED]
You have two submit button in your form, then you need to know what exactly action you are taking, POST or DELETE, right? then you can create an basic controller to your post in your own php script, something like this:
Here is the form ->
<form action="<?php $_SERVER['PHP_SELF']?>" method="POST">and here are the submit buttons
$_SERVER['PHP_SELF']tells you that it will send the form data to itself, if so, then do something like this in the begin of your script:clear now?