This is the code below! It echos out all answers for a specific question in my forum. I want about 6 answers shown on each page. Hence if there are 12 answers on one specific question then it should show the first six from the start then if the user clicks "next page" it should show the user the next 6 answers. I don't know how to fix this step in my progress. Any help is more than appreciated! Thanks.
<?php
$tbl_name2="forum_answers"; // Switch to table "forum_answer"
$sql2="SELECT * FROM $tbl_name2 WHERE question_id='$id'";
$result2=mysqli_query($con, $sql2)or die(mysqli_error($con));
while($rows=mysqli_fetch_array($result2)){
?>
<!DOCTYPE HTML SYSTEM>
<table id="answers" width="400" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td id="answerid">#<?php echo $rows['a_id']; ?></td>
</tr>
<tr>
<td><table width="100%" border="0" cellpadding="3" cellspacing="1" >
<tr>
<?php
INCLUDE 'dbh.php';
$query = mysqli_query($conn,"SELECT * FROM forum_answers LEFT JOIN users ON forum_answers.a_name = users.username WHERE forum_answers.a_id=".$rows['a_id']." AND forum_answers.question_id=".$rows['question_id']);
while ($row = mysqli_fetch_assoc($query)) {
if ($row ['image'] == "") {
echo "<img src='bilder/default.jpg'";
?><td width="77%" bgcolor="#F8F7F1"> <?php echo $rows['a_name']; ?></td><?php
}
else {
echo "<img src='bilder/".$row ['image']."'";
?> <td width="77%" bgcolor="#F8F7F1"> <?php echo $rows['a_name']; ?></td><?php
}
echo "<br>";
}
?>
<td id="datetimeanswer" bgcolor="#F8F7F1"><?php echo $rows['a_datetime']; ?></td>
</tr>
<tr>
<td id="answertext" bgcolor="#F8F7F1"><strong>Answer:</strong></td>
<td bgcolor="#F8F7F1"><?php echo $rows['a_answer']; ?>
<button id="removeanswer" name="remove">Remove</button>
<button id="removeanswer">Edit</button>
</td>
</tr>
</table></td>
</tr>
</table>
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
My database data is echoed in a table list but I will like for the list to be ordered for reference purposes. I have tried several approaches but it doesn't seem to work. Any way possible? here is my code
<table width="auto" border="0" align="center" cellpadding="2" cellspacing="1" class="text">
<tr align="center" id="listTableHeader">
<td>S/N</td>
<td>First Name</td>
<td>Last Name</td>
</tr>
<?php
while($row = dbFetchAssoc($result)) {
extract($row);
if ($i%2) {
$class = 'row1';
} else {
$class = 'row2';
}
$i += 1;
?>
<tr class="<?php echo $class; ?>">
<td width="70" align="center">Delete</td>
<td></td>
<td><?php echo $Firstame; ?></td>
<td><?php echo $LastName; ?></td>
</tr>
<?php
} // end while
?>
<tr>
<td colspan="5"> </td>
</tr>
<tr>
<td colspan="5" align="right"></td>
</tr>
</table>
I can see some mistakes in your code like disorder of some statements.I don't know what is your actual code.How ever here iI am share with my knowledge through the small example.I think it may be helps to solve your issue.You just look the following code and change it with your tables and database connection credential.If any problem in this,you can tell me.I will give the further solution as I can.
<table width="auto" border="0" align="center" cellpadding="2" cellspacing="1" class="text">
<tr align="center" id="listTableHeader">
<td></td>
<td>S/N</td>
<td>First Name</td>
<td>Last Name</td>
</tr>
<?php
$conn= mysqli_connect("localhost", "root", "", "test");
if(!$conn)
{
echo 'not';die;
}
$result=mysqli_query($conn,"select * from animals");
$i=0;
$j=1;
while($row = mysqli_fetch_array($result)) {
$Firstname=$row['name'];
$LastName=$row['animal'];
$id=$row['animal_ID'];
if ($i%2) {
$class = 'row1';
} else {
$class = 'row2';
}
$i += 1;
?>
<tr class="<?php echo $class; ?>">
<td width="70" align="center">Delete</td>
<td><?php echo $j;?></td>
<td><?php echo $Firstname; ?></td>
<td><?php echo $LastName; ?></td>
</tr>
<?php
$j++;
} // end while
?>
<tr>
<td colspan="5"> </td>
</tr>
<tr>
<td colspan="5" align="right"></td>
</tr>
</table>
Im trying to show the respective comments to the respective question ID but all the comments are shown in every post.
actually the value of qusetion ID is not going to the desired table where it suppose to be and that table
name: add_topic.php
<body>
<?php
$con=mysqli_connect('localhost','root','');
if(!$con)
{
die("could not connect to the server".mysqli_error());
}
mysqli_select_db($con,'forum');
// Get value of id that sent from hidden field
$id=$_POST['id'];
// Find highest answer number.
$q="SELECT MAX(a_id) AS Maxa_id FROM forum_ans WHERE que_id='$id'";
$result=mysqli_query($con,$q);
$row=mysqli_fetch_assoc($result);
// add + 1 to highest answer number and keep it in variable name "$Max_id". if there no answer yet set it = 1
if ($row) {
$Max_id = $row['Maxa_id']+1;
}
else {
$Max_id = 1;
}
// get values that sent from form
$a_name=$_POST['a_name'];
$a_email=$_POST['a_email'];
$a_ans=$_POST['a_ans'];
$datetime=date("d/m/y H:i:s"); // create date and time
// Insert answer
$q2="INSERT INTO forum_ans(que_id, a_id, a_name, a_email, a_ans, a_datetime)VALUES('$id', '$Max_id', '$a_name', '$a_email', '$a_ans', '$datetime')";
$result2=mysqli_query($con,$q2);
if($result2){
echo "Successful<BR>";
echo "<a href='view_topic.php?id=".$id."'>View your answer</a>";
// If added new answer, add value +1 in reply column
$q3="UPDATE forum_que SET reply='$Max_id' WHERE id='$id'";
$result3=mysqli_query($con,$q3);
}
else {
echo "ERROR";
}
// Close connection
mysqli_close($con);
?>
</body>
block that named as que_id is showing the value of 0 (as i have set 0 as its default value) and that is the main reason of it but im not able to solve this problem. plz help...
name: view_topic.php
<body>
<?php
$con=mysqli_connect('localhost','root','');
if(!$con)
{
die("could not connect to the server".mysqli_error());
}
mysqli_select_db($con,'forum');
// get value of id that sent from address bar
$id=$_GET['id'];
$q="SELECT * FROM forum_que WHERE id='$id'";
$result=mysqli_query($con,$q);
$row=mysqli_fetch_assoc($result);
?>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td><table width="100%" border="0" cellpadding="3" cellspacing="1" bordercolor="1" bgcolor="#FFFFFF">
<tr>
<td bgcolor="#F8F7F1"><strong><?php echo $row['topic']; ?></strong></td>
</tr>
<tr>
<td bgcolor="#F8F7F1"><?php echo $row['detail']; ?></td>
</tr>
<tr>
<td bgcolor="#F8F7F1"><strong>By :</strong> <?php echo $row['name']; ?> <strong>Email : </strong><?php echo $row['email'];?></td>
</tr>
<tr>
<td bgcolor="#F8F7F1"><strong>Date/time : </strong><?php echo $row['datetime']; ?></td>
</tr>
</table></td>
</tr>
</table>
<BR>
<?php
// Switch to table "forum_answer"
$q2="SELECT * FROM forum_ans WHERE que_id='$id'";
$result2=mysqli_query($con,$q2);
while($row2=mysqli_fetch_assoc($result2)){
?>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td><table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td bgcolor="#F8F7F1"><strong>ID</strong></td>
<td bgcolor="#F8F7F1">:</td>
<td bgcolor="#F8F7F1"><?php echo $row2['a_id']; ?></td>
</tr>
<tr>
<td width="18%" bgcolor="#F8F7F1"><strong>Name</strong></td>
<td width="5%" bgcolor="#F8F7F1">:</td>
<td width="77%" bgcolor="#F8F7F1"><?php echo $row2['a_name']; ?></td>
</tr>
<tr>
<td bgcolor="#F8F7F1"><strong>Email</strong></td>
<td bgcolor="#F8F7F1">:</td>
<td bgcolor="#F8F7F1"><?php echo $row2['a_email']; ?></td>
</tr>
<tr>
<td bgcolor="#F8F7F1"><strong>Answer</strong></td>
<td bgcolor="#F8F7F1">:</td>
<td bgcolor="#F8F7F1"><?php echo $row2['a_ans']; ?></td>
</tr>
<tr>
<td bgcolor="#F8F7F1"><strong>Date/Time</strong></td>
<td bgcolor="#F8F7F1">:</td>
<td bgcolor="#F8F7F1"><?php echo $row2['a_datetime']; ?></td>
</tr>
</table></td>
</tr>
</table><br>
<?php
}
$q3="SELECT view FROM forum_que WHERE id='$id'";
$result3=mysqli_query($con,$q3);
$row3=mysqli_fetch_assoc($result3);
$view=$row3['view'];
// if have no counter value set counter = 1
if(empty($view)){
$view=1;
$q4="INSERT INTO forum_que(view) VALUES('$view') WHERE id='$id'";
$result4=mysqli_query($con,$q4);
}
// count more value
$addview=$view+1;
$q5="update forum_que set view='$addview' WHERE id='$id'";
$result5=mysqli_query($con,$q5);
mysqli_close($con);
?>
<BR>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="add_ans.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td width="18%"><strong>Name</strong></td>
<td width="3%">:</td>
<td width="79%"><input name="a_name" type="text" id="a_name" size="45"></td>
</tr>
<tr>
<td><strong>Email</strong></td>
<td>:</td>
<td><input name="a_email" type="text" id="a_email" size="45"></td>
</tr>
<tr>
<td valign="top"><strong>Answer</strong></td>
<td valign="top">:</td>
<td><textarea name="a_ans" cols="45" rows="3" id="a_ans"></textarea></td>
</tr>
<tr>
<td> </td>
<td><input name="id" type="hidden" value="<? echo $id; ?>"></td>
<td><input type="submit" name="Submit" value="Submit"> <input type="reset" name="Submit2" value="Reset"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
</body>
name: add_topic.php
<body>
<?php
// get data that sent from form
$topic=$_REQUEST['topic'];
$detail=$_REQUEST['detail'];
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$datetime=date("d/m/y h:i:s"); //create date time
$con=mysqli_connect('localhost','root','');
if(!$con)
{
die("could not connect to the server".mysqli_error());
}
mysqli_select_db($con,'forum');
$q="INSERT INTO`forum`.`forum_que`(topic, detail, name, email, datetime)VALUES('$topic', '$detail', '$name', '$email', '$datetime')";
if(mysqli_query($con,$q))
{
echo '<script>alert("Data Inserted Now redirecting to login page");</script>';
header('location:main_forum.php');
}
else
{
echo "error inserting record" . mysqli_error($con,$query);
echo '<script>alert("Error while inserting data");</script>';
}
mysqli_close($con);
?>
</body>
You have two add_topic.php should one be add_ans.php
I don't see any exact problems with this code are you sure that the $id is being used correctly in the hidden field of your form?
Possibly try doing a print_r($_POST); on the add_ans.php page to see what data is actually being passed by the form
Sorry, but i ve got another problem... with connection, I guess.
I try to delete record, but my database is empty. And look like this:
And this is the code. I know, this probably trivial matter but I'm amateur in PHP.
<?php
$con=mysqli_connect("localhost","root","","kluby ranking");
// select record from mysql
$sql="SELECT * FROM europa";
$result=mysqli_query($con,$sql);
?>
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td colspan="5" bgcolor="#FFFFFF"><strong>Delete data in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF"><strong>ID</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Nacja</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>LiczbaPkt</strong></td>
<td align="center" bgcolor="#FFFFFF"> </td>
</tr>
<?php
while($rows=mysqli_fetch_array($result)){
?>
<tr>
<td bgcolor="#FFFFFF"><? echo $rows['ID']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['Nacja']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['LiczbaPkt']; ?></td>
<td bgcolor="#FFFFFF">delete</td>
</tr>
<?php
// close while loop
}
?>
</table>
<?php
// close connection;
mysqli_close($con);
?>
<?php
$con=mysqli_connect("localhost","root","","kluby ranking");
// 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 europa WHERE ID='ID'";
$result=mysqli_query($con,$sql);
// if successfully deleted
if($result){
echo "Deleted Successfully";
echo "<BR>";
echo "<a href='usuw.php'>Back to main page</a>";
}
else {
echo "ERROR";
}
?>
<?php
// close connection
mysqli_close($con);
?>
I'm trying to get a checkbox form to delete entries from the database/ftp server but I just can't get the delete button to delete something. Probably because I'm missing out on something as the error reporting keeps telling that the variable delete isn't determined but I can't find why it says that.
This is my code,
<?php error_reporting(E_ALL); ini_set('display_errors', 1);
// Get our database connector
require("includes/conn.php");
?>
<?php
$sql="SELECT * FROM people ORDER BY ID";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Selecteer welke auto('s) verkocht zijn</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Foto</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Merkt/Type</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Beschrijving</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
echo "<div class=\"picture\">";
echo "<p>";
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo "<img src=\"content/uploads/" . $rows['filename'] . "\" alt=\"\" height=\"125\" width=\"200\" /><br />" . "<br />"; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['fname']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['lname']; ?></td>
</tr>
<?php
echo "</p>";
echo "</div>";
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?php
// Check if delete button active, start this
if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM people WHERE id='$del_id'";
$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">";
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
Another problem I'm having with that code is that I want it to delete the image from the FTP too, not only the file name entry in the database.
P.S. I know about MySQL being deprecated, I'm going edit that after I have the script working like I want it too. Didn't have the time yet to take a look at PDO and MySQLi.
Try this:
<?php
if(isset($_POST['delete'])){ // Check delete button is clicked
foreach($_POST['checkbox'] as $del_id){ // loop only checked items and delete
$sql = "DELETE FROM people WHERE id='$del_id'";
$result = mysql_query($sql);
}
}
?>
and also what ever your logic you have written for delete move to the top of the page before the html code for your desired output.
You're trying to use variable delete although it's not declared.
Try with
<?php
// Check if delete button active, start this
if($_POST['delete']){
// Delete the record
}
?>
<form action="" method="post">
<input name="delete" type="submit" id="delete" value="Delete">
</form>
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Deleing Multiple Rows Using Checkboxes, PHP and MySQL
I wonder whether someone may be able to help me please.
I'm trying to put together a script which creates a form which gives the user the ability to delete a record via the selection of a checkbox and then pressing a 'submit' button.
From reading through many articles, I've put together the following script which is the section of code that builds the table, checkboxes and submit button.
<?php
$query = "SELECT l.*, COUNT(f.locationid) totalfinds FROM detectinglocations l LEFT JOIN finds f ON f.locationid = l.locationid WHERE l.userid = '$idnum' GROUP BY l.locationname";
$result=mysql_query($query);
$count=mysql_num_rows($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="del" id="del" action="deletelocation.php" method="post">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['locationid']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo $rows['locationid']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['locationname']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['returnedaddress']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['totalfinds']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input type="submit" value="Delete" /></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
The code below, then deals with the deletion of the record.
<?php
$del_id = $_POST['checkbox'];
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $detectinglocations WHERE locationid='$del_id'";
$result = mysql_query($sql);
}
?>
The correct information is retrieved and shown in the form table, but the problem I'm having is that I'm unable to get the deletion of the record to work. I've run this through JavaScript Console, but unfortunately I don't receive an error message which may help me to solve the problem.
I just wondered whether someone could possibly take a look at this please and let me know where I'm going wrong.
Change your PHP code as below
$del_id = $_POST['checkbox'];
$detectinglocations = 'your database table name';
foreach($del_id as $value){
$sql = "DELETE FROM ".$detectinglocations." WHERE id='".$value."'";
$result = mysql_query($sql);
}