There are multiple blog entries and each should be shown once. There is a "comment" link that shows and hides the comments for every blog entry. I can't figure out why the same first result keeps showing up. It happens every time I add a new comment (currently there's 5 and the blog section is displayed 5 times and only the first comment is shown for each).
Based on whether the user is logged in the "comments" link either shows a a log in link or (if logged in) a form field to submit a comment.
<?php
$conn = mysql_connect("...", "...", "...");
mysql_select_db("...");
$result = mysql_query("select * from blog");
$result2 = mysql_query("select * from blogcomment where blog.ID = blogcomment.blogID");
$i = 1;
while ($row = mysql_fetch_array($result))
{
echo "<h1>$row[title]</h1>";
echo "<p class ='second'>$row[blog_content]</p> ";
echo "<p class='meta'>Posted by .... • $row[date] • Comments<div id='something$i' style='display: none;'><p class='third' >";
$i++;
while ($row = mysql_fetch_array($result2))
{
echo "$row[commentdate] • $row[username]</p><p>said:</p> <p>$row[comment]</p>";
if (isset ($_SESSION["gatekeeper"])) {
echo '<form method="post" action="postcomment.php"><input name="ID" type = "hidden" value = "' . $row[ID] . '" /><input name="comment" id="comment" type="text" style="margin-left:20px;"/><input type="submit" value="Add comment" /></form></div>';
}
else {
echo '<p class="third">Signup to post a comment</p></div>';
}
}
}
mysql_close($conn);
?>
$i = 1;
while($row = mysql_fetch_array($result))
{
Related
OK, Let's start again. The purpose of this code is to update a database table (db.aotm) comprising the club members who will be featured during the coming year as "The Artist of the Month". The first (html)table shows a list of members who gualify as Features Artists. Their names are hald in the $featuredArtist array.
Next, the $aotm array is populated with the current list of "month" and "member" held in the db.aotm table these are used as the default values of the form.
Then the datalist is populated with names from the $featuredArtist array.
The problem is that when the Form Input box is clicked the datalist box only shows the first name of each artist and any selection only enters the first name.
I have tried using a simple variable instead ogf th array in the datalist but the same happens if $name = "John Smith"; then only "John" is displayed and entered into the input box. If I replace the soft datalist a hard coded datalist then it functions normally. So it seems tha any variable in the datalist only serves the first word.
My code is :
<?php
echo '<div class="dbrd">
$query = " SELECT id, status, fullname FROM members ";
$result = mysqli_query($db, $query);
$i = 0;
while ($row = mysqli_fetch_assoc($result)) {
if (bitN($row[status],2) == 1 && $row[id] > 20){
$featuredArtist[$i]['id'] = $row['id'];
$featuredArtist[$i]['artist'] = $row['fullname'];
$i++;
}
}
?>
<div style="text-align:center;width:100%;">
<table style="width:100%;">
<thead>
<tr><th colspan="3">Featured Artists</th></tr>
</thead>
<tbody>
<?php
$j = 0;
while ($j < $i){
echo " <tr><td>".$featuredArtist[$j]['artist']."</td>";
$j++;
echo " <td>".$featuredArtist[$j]['artist']."</td>";
$j++;
echo " <td>".$featuredArtist[$j]['artist']."</td></tr>";
$j++;
}
?>
</tbody>
</table>
<br />
</div>
<?php
$query = "SELECT id, artist FROM aotm";
$result = mysqli_query($db, $query);
$k = 0;
while ($row = mysqli_fetch_assoc($result)) {
$aotm[$k]['id'] = $row['id'];
$aotm[$k]['artist'] = $row['artist'];
$k++;
}
$l = 0;
echo '<datalist id = "artist">';
while ($l < $i) {
$artist = $featuredArtist[$l]['artist'];
echo " <option value = ".$artist.">";
$l++;
}
?>
</datalist>
<form action = "" method = "POST">
<table style="width:100%;">
<tbody>
<tr><td>Jan</td><td><input type = "text" list = "artist" name="1" size = "45" ></td></tr>
<tr><td>Sept</td><td><input list = "artist" type = "text" name="2" value = "<?php echo $aotm[1]['artist'];?>" size = "45" /></td></tr>
<tr><td colspan="2"><input type = "submit" name = "aotm" value = "Save" /></td></tr>
</tbody>
</table>
</form>
</div>
Ok, I'm not very good at php but trying to learn as much as possible. So I've made website with admin panel. In admin panel I show all rows from database with 2 buttons - 'Delete' and 'Edit'. Delete button is working but I have trouble with Edit. So here is how I show results with the buttons
for ($i = $start; $i < $end; $i++)
{
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) { break; }
// echo out the contents of each row into a table
echo "<tr>";
echo '<td><p>' . mysql_result($result, $i, 'id') . '</p></td>';
echo '<td>' . mysql_result($result, $i, 'caption') . '</td>';
echo '<td>' . mysql_result($result, $i, 'name') . '</td>';
echo '<td>' . mysql_result($result, $i, 'alt') . '</td>';
echo '<td>' . mysql_result($result, $i, 'title') . '</td>';
echo '<td>Delete</td>';
echo '<td>Edit</td>';
echo "</tr>";
}
When I click on Edit page goes to edit.php and url is with ID of choosen image. For ex (/edit.php?id=68). Here is edit.php
<form action="" method="post" enctype="multipart/form-data">
Choose category
<select name="img_category">
<option value="1">Cars</option>
<option value="2">Animals</option>
<option value="3" >PC's</option>
<option value="4" >Sport</option>
</select><br/><br />
Caption
<input type="text" name="caption" /><br /><br />
Alt
<input type="text" name="alt" /><br /><br />
Title
<input type="text" name="title" /><br /><br />
<input type="submit" name="submit" id="submit" value="Edit" /><br /><br />
</form>
<?php
if (isset($_POST['submit']))
{
require_once("../include/db.php");
$id =$_POST['id'];
$caption = $_POST['caption'];
$title = $_POST['title'];
$query = "UPDATE images SET caption = '$caption', title = '$title' WHERE id = '$id'";
$result = mysqli_query($con, $query) or die("Error in query: ".mysqli_error($con));
}
?>
I want to be able to edit caption, alt and title of the image. Now when I press 'Edit' nothing happen. I'm sure is not so hard but for me is kind of.
In your sql-statement, you don't seem to add in your id.
$query = "UPDATE images SET caption = '$caption', title = '$title' WHERE id = 'id'";
You should add the $
$query = "UPDATE images SET caption = '$caption', title = '$title' WHERE id = '$id'";
Update:
There doesn't seem to be an ID field in your form. You try to retrieve it in POST, but there is no such field to retrieve data from. You should pass the id to the query through the form or an other way.
Update2:
As others have said, easiest way to do this is gonna be to get your ID from GET instead of POST.
Change your
$id =$_POST['id'];
to
$id =$_GET['id'];
use GET method to catch id from url
just change
$id =$_POST['id'];
to
$id =$_GET['id'];
You are trying to retrieve a $_GET value with a $_POST, which would leave the $id variable empty.
Change the $id =$_POST['id']; to $id = $_GET['id'];
Also change your query to
$query = "UPDATE images SET caption = '$caption', title = '$title' WHERE id = '$id'";
Add an
<input type="hidden" name="id" value='<?=(int)$_GET['id'] ?>'/>
somewhere in the form.
Have to add: there are a couple of security issues with your code. Remember your code is not production ready! But that is not your question so i wont go into that.
try this
<?php
if (isset($_POST['submit']))
{
require_once("../include/db.php");
$id =$_GET['id'];
$caption = $_POST['caption'];
$title = $_POST['title'];
$query = "UPDATE images SET caption = '$caption', title = '$title' WHERE id = '$id'";
$result = mysqli_query($con, $query) or die("Error in query: ".mysqli_error($con));
}
?>
[FIXED]
I fixed my problem. As suggested by Fred -ii- I visited this Q&A on SO. I saw he used checkboxes which to seems much more useful than buttons since you can take multiple objects out at a time. Also he attached the id of the object to the button like so just like Subin suggested as well.
<form action="" method='POST'>
<input style='display:block; margin:0 auto;'type='submit' name='delete_button' value='<?php echo row['id']; ?>'/>
</form>
Here is the fixed code. I am now able to delete the boxes individually. Thank you for all the suggestions. I am now using mysqli as well.
$query = mysqli_query($connect, "SELECT * FROM shouts ORDER BY `id` DESC LIMIT 5") or die('<p id="formbox" style="text-align:center;">There was an unexpected error grabbing news from the database</p>');
while ($row = mysqli_fetch_array($query)) {
$title2 = $row['title'];
$post2 = $row['post'];
$date2 = $row['date'];
$author = $row['author'];
echo '<div class="news-title"><b style="float:left;">'.$author.'</b><b style="text-align:center; color:green;">'.$title2.'</b>'.$date2.'</div>';
echo '<div class="news-body">'.$post2.'</div>';
if (isset($_SESSION['username']) && ($_SESSION['level'] >= 3 || $_SESSION['group'] == 'Admin')) {
?>
<form action="" method='POST'>
<input style='display:block; margin:0 auto;'type='submit' name='delete_button[]' value="<?php echo $row['id']; ?>"/>
</form>
<?php
}
echo '<br>';
}
if(isset($_POST['delete_button'])) {
$boxid = $_POST['delete_button'];
for($i=0;$i<count($boxid);$i++){
$del_id = $boxid[$i];
mysqli_query($connect, "DELETE FROM `shoutbox`.`shouts` WHERE `shouts`.`id` = '$del_id'") or die('<p id="formbox" style="text-align:center;">There was an unexpected error deleting the post from the database</p>');
}
}
[QUESTION]
So currently I've been working on a website just for my learning purposes and Google thus far has been good help. Although, I can't seem to figure this problem out. I have a "news feed"
http://i.stack.imgur.com/oieXJ.png
it doesn't let me post images but its the only visual idea that I can give. Unless you want to visit my main page http://yuriah.net the news feed is just there without the delete buttons of course.
I want to add a "delete" button to each post so that I can delete each of them individually when I want. I am having problems with the current code im using. It deletes all of them from the database but I only want to delete the "post" that I click delete. Here is my source:
$query = mysql_query("SELECT * FROM shouts ORDER BY `id` DESC LIMIT 5") or die('<p id="formbox" style="text-align:center;">There was an unexpected error grabbing news from the database</p>');
while ($row = mysql_fetch_array($query)) {
$title2 = $row['title'];
$post2 = $row['post'];
$date2 = $row['date'];
$author = $row['author'];
$boxid = $row['id'];
echo '<div class="news-title"><b style="float:left;">'.$author.'</b><b style="text-align:center; color:green;">'.$title2.'</b>'.$date2.'</div>';
echo '<div class="news-body">'.$post2.'</div>';
if ($_SESSION['level'] > 3 || $_SESSION['group'] == 'Admin' || $_SESSION['group'] == 'Owner') {
?>
<form action="" method='post'>
<input style='display:block; margin:0 auto;'type='submit' name='delete_button' value='Delete' />
</form>
<?php
if(isset($_POST['delete_button'])) {
$con = mysql_query("DELETE FROM `shoutbox`.`shouts` WHERE `shouts`.`id` = '$boxid'") or die('<p id="formbox" style="text-align:center;">There was an unexpected error deleting the post from the database</p>');
header('refresh:1; url=/');
mysql_close($con);
}
}
echo '<br>';
}
I am fairly new at PHP HTML CSS MYSQL etc. I am open to all suggestions and comments. Any help will be appreciated thank you.
First you are selecting several rows:
mysql_query("SELECT * FROM shouts ORDER BY `id` DESC LIMIT 5")
Then you're grabbing an array for each row
while ($row = mysql_fetch_array($query)) {
Then setting the boxid for that row, and then running a delete query inside the loop for that row, where your WHERE clause looks for that boxid.
So, given this code flow you will ALWAYS delete all rows from the table.
mysql_query("DELETE FROM `shoutbox`.`shouts` WHERE `shouts`.`id` = '$boxid'")
Now, the real question becomes, how does one fix this?
In your select statement, just add a where clause that only returns the row with the boxid or whatever id you actually need so that just one row is returned (or exactly the rows to be deleted) are returned
"SELECT FROM `shoutbox`.`shouts` WHERE `shouts`.`id` = '$someSortOfID'"
Additionally, mysql_* is deprecated, so please see the following documentation on how to "upgrade" as it were to mysqli, the new standard.
I don't recommend you to use depreciated mysql_* function at all !!!!!
What you need to do is change the input value to row id :
<form action="" method='POST'>
<input style='display:block; margin:0 auto;'type='submit' name='delete_button' value='<?echo$boxid;?>' />
</form>
Then move the whole code that conatins in if(isset($_POST['delete_button'])) outside the while loop and the whole code will be :
<?php
$query = mysql_query("SELECT * FROM shouts ORDER BY `id` DESC LIMIT 5") or die('<p id="formbox" style="text-align:center;">There was an unexpected error grabbing news from the database</p>');
while ($row = mysql_fetch_array($query)) {
$title2 = $row['title'];
$post2 = $row['post'];
$date2 = $row['date'];
$author = $row['author'];
$boxid = $row['id'];
echo '<div class="news-title"><b style="float:left;">'.$author.'</b><b style="text-align:center; color:green;">'.$title2.'</b>'.$date2.'</div>';
echo '<div class="news-body">'.$post2.'</div>';
if ($_SESSION['level'] > 3 || $_SESSION['group'] == 'Admin' || $_SESSION['group'] == 'Owner') {
?>
<form action="" method='post'>
<input style='display:block; margin:0 auto;'type='submit' name='delete_button' value='<?echo$boxid;?>' />
</form>
<?php
}
echo '<br>';
}
if(isset($_POST['delete_button'])) {
$boxid = $_POST['delete_button'];
$con = mysql_query("DELETE FROM `shoutbox`.`shouts` WHERE `shouts`.`id` = '$boxid'") or die('<p id="formbox" style="text-align:center;">There was an unexpected error deleting the post from the database</p>');
header('Location: http://'.$_SERVER['HTTP_HOST']);
mysql_close($con);
}
?>
I want to display question papers according to student's branch.
I create 5 tables in database "aptitude".
tables are as following:
itque,
etrxque,
extcque,
civilque,
mechque.
branch name is store in 'register' database.
If student is in IT department then only IT's question paper should display.
but there is some problem in logic. question paper is not displayed according to the student's branch.
code of test.php:
<html>
<body>
<?php
require_once('appvars.php');
require_once('connectvars.php');
// Make sure the user is logged in before going any further.
if (!isset($_SESSION['user_id'])) {
echo '<span style="color:#FFF;text-align:center;"><p class="login">Please log in to access this page.</p></span>';
exit();
}
else {
include("menu.php");
}
?>
<?php
$dbc=mysqli_connect("localhost","root","","register");
$query = "SELECT branch FROM user WHERE m_id = '" . $_SESSION['user_id'] . "'";
$data = mysqli_query($dbc, $query);
$row = mysqli_fetch_array($data);
if ($row != NULL) {
$Branch = $row['branch'];
}
else {
echo '<p class="error">There was a problem .</p>';
}
?>
<form action="./result.php" method="post">
<?php
$connect = mysql_connect("localhost" ,"root","");
mysql_select_db("aptitude");
$query = mysql_query("SELECT * FROM `$Branch` ORDER BY RAND() LIMIT 10 ");
while($rows = mysql_fetch_array($query)):
$q = $rows['Q_no'];
$qus = $rows['Question'];
$a = $rows['answer1'];
$b = $rows['answer2'];
$c = $rows['answer3'];
$d = $rows['answer4'];
$ans = $rows['correct'];
echo" <p> </p>";
echo "<p>Q:- $qus <br></p>";
echo "<p> A <input type=radio name = 'answer[$q]' value = '$a'></input>$a    <br>
";
echo " B <input type=radio name = 'answer[$q]' value = '$b'></input>$b    <br>
";
echo " C <input type=radio name = 'answer[$q]' value = '$c'></input>$c    <br>
";
echo " D <input type=radio name = 'answer[$q]' value = '$d'></input>$d <br><br> </p>";
endwhile;
?>
<center><input name="cmdSubmit" type="submit" id="cmdSubmit" value="Submit"/>
</center>
</form>
</body>
</html>
The biggest logic flaw is that if user_id isn't in the session and the user_id isn't in a cookie then you still allow processing to continue. You should force the user to login again.
Apache has the default session time of 20 minutes so you might want to increase the time to allow the students to complete the assessment otherwise when they come to save their answers there will be a server time out and no data saved.
required small changes
$query = mysql_query("SELECT * FROM ".$branch." ORDER BY RAND() LIMIT 10 ");
Ive been working on my delete.php file to delete selected messages for literally hours on end without any headway. How would I delete messages with the codes I have set up?
Heres the code to how my delete.php is set up right now
<?php
session_start();
header("Location:inbox.php");
$user = $_SESSION['username'];
include 'connect.php';
foreach($_POST['messages'] as $num => $messages_id)
mysql_query("DELETE FROM messages WHERE messages_id='$messages_id' AND to_user='$user'");
?>
Here's the code to my entire inbox.php
<?php
require "connect.php";
echo "<hr><br><div id='mail'>";
// get the messages from the table.
$get_messages = mysql_query("SELECT messages_id FROM messages WHERE
to_user='$userfinal' ORDER BY messages_id DESC") or die(mysql_error());
$get_messages2 = mysql_query("SELECT * FROM messages WHERE to_user='$userfinal'
ORDER BY messages_id DESC") or die(mysql_error());
$num_messages = mysql_num_rows($get_messages);
// display each message title, with a link to their content
echo '<ul>';
for($count = 1; $count <= $num_messages; $count++)
{
$row = mysql_fetch_array($get_messages2);
$fromuser = $row['from_user'];
$messageid = $row['messages_id'];
$messagetitle = $row['message_title'];
$messageread = $row['message_read'];
$messagedate = $row['message_date'];
echo " <form name='send' method='post' action='delete.php'><input type='checkbox' name='delete' value='$messageid'></td><font face='helvetica'><font size='3'>$fromuser</font></font> • <div align='right'><a href='read_message.php?messages_id=$messageid'>$messagetitle</a></div>
<center><hr></center>
";
}
?>
<input type="submit" name="Submit" value="Delete Selected">
</table>
</div>
Please help out before my head explodes
You are using header("Location:inbox.php"); in the second line of your code, so the rest of it never gets executed.
Just move it to the end of the script, after you have deleted the messages you wanted to delete.
name='delete[]' value='<?php echo $messageid?>'
$_POST['delete']
What seems to be the problem is that you are adding a form tag for each checkbox (and not closing that form tag). Instead put all the checkboxes in the same form, and name them delete[] the brackets at the end put each checkbox in an array inside the $_POST array. So you can loop through them.
Here is an edited version (Note: not tested)
<?php
require "connect.php";
echo "<hr><br><div id='mail'>";
// get the messages from the table.
$get_messages = mysql_query("SELECT messages_id FROM messages WHERE
to_user='$userfinal' ORDER BY messages_id DESC") or die(mysql_error());
$get_messages2 = mysql_query("SELECT * FROM messages WHERE to_user='$userfinal'
ORDER BY messages_id DESC") or die(mysql_error());
$num_messages = mysql_num_rows($get_messages);
// display each message title, with a link to their content
echo "<form name='send' method='post' action='delete.php'>";
echo '<ul>';
for($count = 1; $count <= $num_messages; $count++)
{
$row = mysql_fetch_array($get_messages2);
$fromuser = $row['from_user'];
$messageid = $row['messages_id'];
$messagetitle = $row['message_title'];
$messageread = $row['message_read'];
$messagedate = $row['message_date'];
echo "
<input type='checkbox' name='messages[]' value='$messageid'></td>
<font face='helvetica' size='3'>$fromuser</font> •
<div align='right'>
<a href='read_message.php?messages_id=$messageid'>$messagetitle</a>
</div>
<center><hr></center>
\n\n\n";
}
echo '</form>';
?>
<input type="submit" name="Submit" value="Delete Selected">
</table>
</div>