I am creating a database wherein users can select a title (search.php) and then the specific page for that title will be shown with its corresponding details (indiv.php).
They will be able to select a title from search.php and here is the code for the title link:
<a href="indiv.php?titleID=' . $row['titleID'] . '">
Then the indiv.php will be able to show the details for that title based on the titleID they have selected from search.php. Here is the code for indiv.php:
<?php
include "databaseconnect.php";
$titleID = $_GET["titleID"];
$sql = ("SELECT titleID, authorsID, yearID,
FROM table
WHERE titleID = '$titleID'");
$result = $mysqli->query($sql);
$row = mysqli_fetch_assoc($result);
$mysqli->close();
?>
In this indiv.php, I also added a comment section form.
<form action="indiv.php" method="POST">
<input type="text" name="nameID" placeholder="Enter your name" required>
<textarea name="commentID" placeholder="Write your comment here" required></textarea>
<input type="submit" name="submit" value="Post Comment">
</form>
The problem is, every time I try to submit a comment, the page refreshes and does not show its corresponding details (just shows an error - see below) because $titleID = $_GET["titleID"]; cannot fetch the titleID anymore from search.php but tries to find it in indiv.php because of my <form action="indiv.php" method="POST"> in the comment section. Is there a way to prevent this from happening? I want to still be able to show the details based on titleID even after posting a comment.
The error is: Notice: Trying to access array offset on value of type null in...
Thanks in advance! I am not familiar with PHP so I hope you can help me out.
Related
I have a search bar on my page, however it is about half way down, and when you submit a search query, the page scrolls back to the top.
Is there a way to get the page to stay where the user was when they search? My search is on the same page, and all of the content also stays on the page.
This is my current search code:
$search=mysql_real_escape_string($_POST['search']);
//- Queries the table to get content from rows -
$QuerySelect = "SELECT thumbnail,image,title,category,description
FROM homepage
WHERE category LIKE '%$search%' ";
//----------------- QUERY the TABLE, store THE CONTENT IN A PHP VARIABLE -----------------
$result = mysql_query($QuerySelect);
HTML :
<div class="searchcontainer">
<form method="post" action="index.php">
<input type="text" class="input" name="search" size="40" placeholder="Search by Category...">
<input type="submit" class="button" name="Submit" value="Search" id="Submit" >
</form>
</div>
<?php
$numrows = mysql_num_rows ($result );
if($numrows == 0){
Javascript & JQuery code is fine.
If you are posting to the same page you are on, you can make your action be index.php#my-form and give your form an ID of my-form. Of course change my-form to whatever you want.
I am making a facebook like wallpost for a simple social network system that we are building. It works pretty well so far except for the displaying of comments. In my database, I have a table for the posts and another one for the comments. The way I displayed the comments in their specific post is that, in my comments table, I have a column named comment_id . So, example if the id of the post is 7 and the comment_id is also 7 then the comment will appear below that post with the same id. Now, my problem is that I can successfully display the comments in their respective posts but only 1 is being displayed. I wanted all the comments to be displayed in every posts they belong but I can't seem to make it work.
<?php
require_once 'dbconfig.php';
$user = $_SESSION['user'];;
echo '<form action="post.php" method="post" class="wallpost"><input
type="text" name="post" size="50"><input type="submit" name="wallpost"
value="Post"></form><br/>';
$query = $con->query("SELECT * FROM statuspost LEFT JOIN comments ON
statuspost.id=comments.comment_id group by statuspost.id DESC");
while($i = $query->fetch_object()){
echo '<span class="user">'.$i->user.'</span>'.'<br>'.'<span
class="post">'.$i->post.'</span>'.' <form action="editpost.php?type=post&
id='.$i->id.'" method="post"><span id="edit'.$i->id.'" class="editfield"
style="display:none;"><input type="text" name="edit"><input
type="submit"
value="Edit" class="editb"><br/></span><a href="#"
onclick="showEdit(edit'.$i->id.');">Edit </a><a
href="remove.php?type=post&
id='.$i->id.'" >Remove</a> Comment</form>'.'<br
/>'.'<form action="comment.php?for='.$i->id.'" method="post"
id="comment">
<input type="text" name="comment" id="comment">
<input type="submit" value="Comment" id="commentb"></form>'.'<br
/><ul><li>'.$i->comment.'</li></ul>';
}
?>
So, example if the id of the post is 7 and the comment_id is also 7 then the comment will appear below that post with the same id. [...] but only 1 is being displayed
If you're linking the comments with the post ID, you will only get one comment per post since you can only have one unique post ID (assuming that you have the post ID as primary key).
You should have a column post in your comments table, and have that column reference the post they're commented on.
When you do your query, you can inner join the comments with the posts like so :
INNER JOIN comments ON comments.post = posts.id
I'm stuck on this. What I'm trying to do is this. A notification system. When I comment on something, I need it to get all other users who previously commented and send them a notification as well as the owner of the item, that the post has new activity.
Here's the issue. I'm populating a comment box underneath the post, and you write your comment, and hit enter, and it sends the post ID your commenting on, as well as your username and comment to a php script which calls a function to send a notification.
So, as hidden inputs, I'm pulling out from the database in the comment form, anyone who has previously commented on the post. Here's the issue, let's say User A has commented 3 times, the database would have him as a commenter 3 times, so his name will be pulled into the comment form as three different inputs. Here is some code for the mysql call that is pulling the previous commenters:
// Query the comments to see who has commented.
// Their username will be in the owner column.
// $postid is being pulled from the actual post id that you are commenting on.
// At this point the post content and previous comments are already
// processed, this is just processing the comment form.
$sql = "SELECT * FROM comments WHERE postid = '$postid'";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)){
echo '<input type="hidden" name="commented[]" value="'.$row['owner'].'" />';
}
If user A has commented 4 times, the comment form would look something like this, then:
<form id="comment-form-##">
<input type="hidden" name="commented[]" value="usera" />
<input type="hidden" name="commented[]" value="usera" />
<input type="hidden" name="commented[]" value="usera" />
<input type="hidden" name="commented[]" value="usera" />
<input type="hidden" name="commented[]" value="userc" />
<input type="hidden" name="commented[]" value="userc" />
// And the id of the post you're commenting on
<input type="hidden" name="postid" value="24" />
// And then the actual input box for your comment:
<input type="text" name="comment" placeholder="Write a comment" />
So how can I pull the users who have previously commented, and skip their multiple listings in the database. Ideally, I'd like the comment form to look like this when complete:
<form id="comment-form-##">
<input type="hidden" name="commented[]" value="usera" />
<input type="hidden" name="commented[]" value="userc" />
// And the id of the post you're commenting on
<input type="hidden" name="postid" value="24" />
// And then the actual input box for your comment:
<input type="text" name="comment" placeholder="Write a comment" />
Hope there is a way to do this as this was my first inclination on how to notify previous commenters to the new activity. Thanks for any and all help I can get here.
Too much work.
SELECT DISTINCT owner
FROM comments
WHERE postid = ?
This is what I suggest, insert all owners into an array, then use array_unique on it so duplicates are removed, then run a foreach loop that would display the hidden form fields
$sql = "SELECT * FROM comments WHERE postid = '$postid'";
$result = mysqli_query($con,$sql);
$owners = array();
while($row = mysqli_fetch_array($result)) $owners[] = $row['owner'];
$owners = array_unique($owners);
foreach($owners as $owner) echo '<input type="hidden" name="commented[]" value="'.$owner.'" />';
Also, let me mention that I think this is not the best way to do what you want. These hidden form fields could be tampered with easily and you would be sending notifications to the wrong people.
Since you send the post_id as a hidden form field to a page that notifies people, I suggest you modify that page that so that it queries everyone who commented on that specific post_id except the one that is currently commenting and send them a notification instead of simply adding their ids in a hidden form field.
This should work
SELECT * FROM comments WHERE postid = '$postid' GROUP BY owner
Change your query to
$sql = "SELECT DISTINCT owner from comments where postid = '$postid'";
Then you'll get a list of the users who've commented without any duplicates.
I have a simple Form along side a PHP update query that simply isn't working! I know the PHP is working on the page as there are several validation checks that need to be passed before hand which are working perfectly. The form its self is inside the Colorbox Popup tool.
My HTML Form Code is:
<div id="stylized" class="myform">
<form action="#" method="post">
<input type="hidden" name="user_id" value="<?php echo $user_id; ?>" />
<label>First Name:<span class="small">Enter your forename</span></label>
<input id="first_name" type="text" name="first_name" maxlength="50" placeholder="e.g. Joe" required autofocus/>
<div class="spacer"></div>
<input type="submit" id="update" name="update" value="Continue to Step 2!">
</form>
</div>
With the PHP Code as follows (this is above the HTML code on the page):
<?php
if($_POST['update']){
$user_i = $_POST['user_id'];
$f_name = $_POST['first_name'];
$first_name = ucfirst($f_name);
mysql_query("UPDATE user SET first_name = '$first_name' WHERE user_id = '$user_i'") or die(mysql_error());
} ?>
The actual submit appears to be working, with the Popup refreshing afterwards, but the database does not update! I have triple checked the syntax and the database fields. 'user' and 'first_name' and 'user_id' is correct.
Update: Because the popup box refreshes, I cannot view the error's from the 'or die(mysql_error()) unfortunately, other wise i might have been one step closer.
Any help would be hugely appreciated.
Many thanks in advance.
When you say pop-up box, I assume you are using ajax to communicate from the form to the server, which as you stated is difficult to view submitted data. If this is the case try:
error_log(serialize($_POST));
This will force an entry in your error log with the $_POST data in serialized format, so you can check the values you are submitting are populated correctly.
You will also want to sanitize the variables you are adding to the SQL:
$sql = "UPDATE user SET first_name = " . mysql_real_escape_string($first_name) . " WHERE user_id = " . mysql_real_escape_string($user_i) . " LIMIT 1";
mysql_query($sql);
I would:
print_r($_POST); to view the POST data.
Generate the SQL from a string so it can be printed for debugging purposes, like so:
$sql = "UPDATE user SET first_name = '$first_name' WHERE user_id = '$user_i'";
echo $sql;
mysql_query($sql) or die(mysql_error());
One of these techniques will likely tell you why the PHP-generated SQL doesn't update your database record.
you set your user_id field by echo $user_id; but your variable name is set to $user_i = $_POST['user_id'];
therefore your user id field is not set and your Mysql command will fail.
Okay... I'm having the following problem - in my facebook application I let people add quotes sending them to my DB with their user_id, post_id and date. Then there's a browse page where people see the posts - so far, so good. But then I add a button that lets my users set the said post as their status. Still all good. However when I hit the button what it does is setting all the posts in the DB as my status, one by one until they all print out as my statuses. I'm sure that it is because of the 'while' function I use and because I am not sure how to print out all posts and being able to add to each the said button, holding only the specific post_id from the db ._.'
So in other words, it is a PHP/MySQL problem mainly, so even if you are not familiar with FBML, you can still help me out with the code...
Think of it as a list of posts and each should have a button doing something and being somehow attached to only the specific post.
The code is the following:
.... some code here ....
$query = 'SELECT * FROM tb_table ORDER BY `time` DESC';
$results = mysql_query($query);
---some other code---
while($line = mysql_fetch_assoc($results)) {
echo '<TABLE BORDER=0><TR VALIGN=TOP><TD>';
echo "<fb:profile-pic uid=".$line['userid']." size='square' facebook-logo='true'></fb:profile-pic></TD>";
echo "<TD>".$line['postid']."<br>"."<br>"."Posted by: ".$data['first_name'].$data['last_name']."<br>".date("F j, Y, g:i a", $line['fb_time'])."</TD>";
//Problems start from here
echo $facebook->api_client->users_setStatus($line['postid']) ;
echo '<div id="statusdiv" style="display:<?=$visibility;?>;">
<form method="POST">
<input type="submit" value="change status" />
</form>
</div> ';
echo "</TR></TABLE><br>";
The thing you're doing now is every time you loop through your mysql resultset you call the users_setStatus function which then sets your status (for every iteration).
You want to add a hidden input field to the form which contains the $line['postid'] and then call users_setStatus after a POST of the form. In that way you only change your status once.
Update
Remove the call to users_setStatus from the while loop.
Change your form like this:
echo '<div id="statusdiv" style="display:<?=$visibility;?>;">
<form method="POST">
<input type="submit" value="change status" />
<input type="hidden" name="line" value="'.$line['postid]'.'" />
</form>
</div> ';
Then, catch the POST variable upon submitting. With something like:
if (isset($_POST['line'])) {
echo $facebook->api_client->users_setStatus($_POST['line']) ;
}
See http://www.w3schools.com/php/php_forms.asp for more info.