Form not inserting information into mysql table? - php

Hi i'm having a problem with my forum comment form. Basically i am trying to allow a user to read a users post and comment on it, the form data is suppose to be inserted into mysql.
the values i need inserted are the comment_id which is the session id of the user and the post_id the id of the post their commenting on and the content which they type.
For some reason this is not being inserted into mysql and is coming up with my echoed success message without any errors. can someone please show me where im going wrong.
Thanks
<?php
$page_title = "Read Post";
include('includes/header.php');
include ('includes/mod_login/login_form2.php'); ?>
<?php
confirm_logged_in();
if (isset ($_GET['post'])) {
$forum_id = $_GET['post'];
}
?>
<?php include('includes/copyrightbar.php'); ?>
<?
$read_forum_set = read_forum_set();
while ($forum = mysql_fetch_array($read_forum_set)) {?>
<div class="modtitle">
<div class="modtitle-text"><?php echo "{$forum['display_name']}"; ?>'s Forum Post</div>
<? } ?>
</div>
<div class="modcontent57">
<br /><br /><br/><br/>
<div class="forum">
<div class="forum-pic"><?php echo "<img src=\"data/photos/{$_SESSION['user_id']}/_default.jpg\" width=\"100\" height=\"100\" border=\"0\" align=\"right\" class=\"img-with-border-forum\" />";?>
</div>
<div class="message-links">
<strong><< Back to Forum
</div>
<br /><br /><br/><br/>
<?php
$datesent1 = $forum['date_sent']; ?>
<?php
$read_forum_set = read_forum_set();
while ($forum = mysql_fetch_array($read_forum_set)) {
$prof_photo = "data/photos/{$forum['user_id']}/_default.jpg";
$result = mysql_query("UPDATE ptb_forum SET ptb_forum.read_forum='1' WHERE ptb_forum.id='$forum'")
or die(mysql_error());
?>
<div class="message-date">
<?php echo "".date('D M jS, Y - g:ia', strtotime($forum['date_sent'])).""; ?></div>
<div class="img-with-border-frm-read"><?php echo "<img width=\"60px\" height=\"60px\" src=\"{$prof_photo}\"><br />"; ?></div>
<?php echo "<div class=\"forum-content2\"><div class=\"forum_subject\"><strong>Subject:</strong></div><div class=\"forum_subject2\"><i>{$forum['title']}</i></div><div class=\"forum_body\"><strong>Post:<br/></strong></br ><i>{$forum['content']}</i></div></div>";?>
<?php
// check if the review form has been sent
if(isset($_POST['forum_comment']))
{
$content = $_POST['forum_comment'];
//We remove slashes depending on the configuration
if(get_magic_quotes_gpc())
{
$content = stripslashes($content);
}
//We check if all the fields are filled
if($_POST['forum_comment']!='')
{
{
$sql = "INSERT INTO ptb_forum_comments (comment_id, post_id, content) VALUES (NULL, '".$_SESSION['user_id']."', '".$profile_id."', '".$content."');";
mysql_query($sql, $connection);
echo "<div class=\"infobox-profile4\"><strong>Thank You</strong> - Your review has been sent for approval.</div>";
} }
}
?>
<?php if(isset ($_SESSION['user_id'])) { ?>
<div class="forum-comment-box">
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
<textarea name="forum_comment" id="forum_comment" style="resize:none; height:100px; width:543px;"></textarea><input type="submit" name="send_button" id="send_button" style="float:right; margin-right:30px; text-align:center;" value="Reply to {$message['display_name']}" /></form></div>
<? } } ?>
</div>
</div>
<?php include('includes/footer.php'); ?>
</div>

You have some missing table names here:
$sql = "INSERT INTO ptb_forum_comments (comment_id, post_id, content) VALUES (NULL, '".$_SESSION['user_id']."', '".$profile_id."', '".$content."');";
as you can see you have 3 tables names and 4 values.

Related

How to avoid blank first result when searching from databse

I have search webpage, but when the search is run, there is a blank first result.
<?php include "headnav.php";
$count = 0;
$sql = "SELECT * FROM lost_property";
if (!empty($_POST)) {
$name = mysqli_real_escape_string($dbconnect, htmlspecialchars($_POST['name']));
$item = mysqli_real_escape_string($dbconnect, htmlspecialchars($_POST['item']));
$area = mysqli_real_escape_string($dbconnect, htmlspecialchars($_POST['area']));
$sql = "
SELECT *
FROM lost_property
JOIN item
ON lost_property.itemID = item.itemID
JOIN area
ON lost_property.areaID = area.areaID
WHERE name LIKE '%$name%'
AND item LIKE '%$item%'
AND area LIKE '%$area%'
ORDER
BY lost_property.name ASC
";
$search_query = mysqli_query($dbconnect, $sql);
$count = mysqli_num_rows($search_query);
}
$result = $dbconnect->query($sql);
?>
<body>
<div class="form">
<h1>Search for lost property here:</h1>
<form action="" method="POST">
Name:
<input type="text" placeholder="Name" name="name">
Item type:
<select name="item" class="dropdown">
<option value="" disabled selected>Item</option>
<?php
$item_sql = "SELECT DISTINCT item FROM `lost_property`
JOIN item ON (lost_property.itemID = item.itemID)
ORDER BY item ASC
";
$item_query = mysqli_query($dbconnect, $item_sql);
$item_rs = mysqli_fetch_assoc($item_query);
do {
?>
<option value="<?php echo $item_rs['item']; ?>"><?php echo $item_rs['item']; ?></option>
<?php
} while ($item_rs = mysqli_fetch_assoc($item_query));
?>
</select>
Area (where it was found):
<select name="area" class="dropdown">
<option value="" disabled selected>Area</option>
<?php
$area_sql = "SELECT DISTINCT area FROM `lost_property`
JOIN area ON (lost_property.areaID = area.areaID)
ORDER BY area ASC
";
$area_query = mysqli_query($dbconnect, $area_sql);
$area_rs = mysqli_fetch_assoc($area_query);
do {
?>
<option value="<?php echo $area_rs['area']; ?>"><?php echo $area_rs['area']; ?></option>
<?php
} while ($area_rs = mysqli_fetch_assoc($area_query));
?>
</select>
<input type="submit" value="Search" name="btn">
</form>
</div>
<div class="gallery">
<h2>Search results:</h2>
<?php
//check for results. If there are none display error
if ($count < 1) {
?>
<div class="error">
<h1>No results were found.</h1>
</div>
<?php
} //end if
else {
do {
?>
<!-- display image and information from database and show in gallery -->
<div class="results">
<h3><?php echo $search_rs['name']; ?></h3>
<h3><?php echo $search_rs['item']; ?></h3>
<p><?php echo $search_rs['area']; ?></p>
</div>
<?php
} // end of do
while ($search_rs = mysqli_fetch_assoc($search_query));
} //end else
//if there are any display
?>
</div>
</table>
</body>
</html>
It's hard to see without any CSS, but no matter what is searched, there is always one result, but the h3 and p fields don't have any content. If there wasn't the no results error message it would pop up there too. What is causing this first result?
Use while (){}, if you use do instead it will run once first (credit to Magnus Eriksson).
It ends up like
else {
while ($search_rs = mysqli_fetch_assoc($search_query)) {
?>
<!-- display image and information from database and show in gallery -->
<div class="results">
<h3><?php echo $search_rs['name']; ?></h3>
<h3><?php echo $search_rs['item']; ?></h3>
<p><?php echo $search_rs['area']; ?></p>
</div>
<?php
} // end of do
} //end else
//if there are any display
?>
instead of
else {
do {
?>
<!-- display image and information from database and show in gallery -->
<div class="results">
<h3><?php echo $search_rs['name']; ?></h3>
<h3><?php echo $search_rs['item']; ?></h3>
<p><?php echo $search_rs['area']; ?></p>
</div>
<?php
} // end of do
while ($search_rs = mysqli_fetch_assoc($search_query));
} //end else
//if there are any display
?>

php form submitting info twice

I have a form that's supposed to enter a reply to a forum topic into the database and redirect the user back to the same topic. After much trial and error I have finally got the form to work, only it is putting two identical entries into the db every time. I cannot figure out why. I have looked up this same problem and most of the other people were not redirecting after the form submission or they were using AJAX or jquery or something. Here is my page info:
<?php
session_start();
include_once('includes/config.php');
include_once('classes/topic.php');
include_once('classes/post.php');
include('includes/header.php');
?>
<link rel="stylesheet" href="css/dd.css">
<?php
$topic = new Topic;
if (isset($_GET['id']))
{
$topic_id = $_GET['id'];
$data = $topic->fetch_data($topic_id);
if (isset($_POST['content']))
{
// someone posted a reply
$date = date('Y-m-d H:i:s');
$by = $_SESSION['user_id'];
$query = $pdo->prepare("INSERT INTO dd_posts (post_content, post_date, post_by, post_topic) VALUES (? ,? ,?, ?)");
$query->bindParam(1, $_POST['content']);
$query->bindParam(2, $date);
$query->bindParam(3, $by);
$query->bindParam(4, $_GET['id']);
$query->execute();
$result = $query->execute();
header("location:topic.php?id=".$_GET['id']);
exit;
}
?>
<div id ="wrapper">
<div class="drop-section">
<div id="menu">
<a class="item" href="drop_index.php">Dead Drop</a>
<a class="item" href="add_topic.php">New Post</a>
<a class="item" href="admin/add_cat.php">New Category</a>
<div id="userbar">
<?php
if( $user->is_logged_in() ) {
echo 'Hello ' . $_SESSION['user_name'] . '. How are you?';
} else {
echo '<a class="item" href="login.php">Sign in</a> or <a class="item" href="index.php">Create an account</a>';
}
?>
</div>
</div>
<table>
<tr class = "header-row">
<div id = "sans">
<?php echo $data['topic_subject']; ?>
- <small>started by <?php echo $data['user_name']; ?> </small><br />
<?php echo $data['topic_content']; ?>
</div>
</tr>
<?php
// retrieve all the replies to the original topic
$post = new Post;
$topic_id = $_GET['id'];
$posts = $post->fetch_all_posts_by_topic($topic_id);
?>
<tr>
<td class="first-column">
<?php foreach ($posts as $post) { ?>
<div class="drop-content-box">
<li><?php echo $post['post_content']; ?><br />
<div class = "forum-user-info">
<a href="player.php?id=<?php echo $post['user_id']; ?>">
<?php echo $post['user_name']; ?></a> - level:
<?php echo $post['user_level']; ?>
</div>
</li>
</div>
<?php } ?>
</td>
</tr>
</table>
<?php
if( $user->is_logged_in() )
{
?>
<div id = "header-section">Reply</div>
<?php if (isset($error)) { ?>
<small><?php echo $error; ?></small>
<?php } ?>
<form action="<?php echo "topic.php?id=".$_GET['id']?>" method="post" autocomplete="off">
<small><i>Do not post the actual answer to any level.</i></small><br />
<textarea rows="15" cols="50" name="content" placeholder="Give us your thoughts..."></textarea><br />
<input type="submit" value="Post" />
</form>
</div>
</div>
<?php
} else {
echo '<div id = "errors"><small>You must be signed in to reply.</div></small>';
}
}
include_once('includes/footer.php');
?>
You're executing the query twice.
$query->execute();
$result = $query->execute();

Search results not showing

I have a problem with my search.php file to render me results...
I dont get any strings of error, but when I type an existing keyword I get no results...
The format of the results are the same as viewed in my main content on the website (grid view)...
The code:
<body>
<?php include_once("analyticstracking.php") ?>
<div class='container'> <!--Start of the container-->
<div><?php include("includes/header.php"); ?></div>
<div><?php include("includes/navbar.php"); ?></div>
<div><?php include("includes/left_col.php"); ?></div>
<div class='main_col'>
<div class='main_content'>
<?php
include("includes/connect.php");
if(isset($_GET['search'])){
$search_id = $_GET['q'];
$search_query = "SELECT * FROM games WHERE game_keywords LIKE '%$search_id%'";
$run_query = mysql_query($search_query);
echo '<table>';
$games = 0;
while($search_row = mysql_fetch_array($run_query)){
// make a new row after 9 games
if($games%9 == 0) {
if($games > 0) {
// and close the previous row only if it's not the first
echo '</tr>';
}
echo '<tr>';
}
// make a new column after 3 games
if($games%3 == 0) {
if($games > 0) {
// and only close it if it's not the first game
echo '</td>';
}
echo '<td>';
}
$game_id = $search_row['game_id'];
$game_name = $search_row['game_name'];
$game_category = $search_row['game_name'];
$game_keywords = $search_row['game_name'];
$game_image = $search_row['game_image'];
?>
<div class="game_grid">
<a href="game_page.php?id=<?php echo $game_id; ?>"><img src="images/games_images/<?php echo $game_image; ?>" width="120" height="120" />
<span><?php echo $game_name; ?></span>
</div>
<?php
$games++;
}
}
?>
</table>
</div>
</div>
<div><?php include("includes/footer.php"); ?></div>
</div> <!--End of the container-->
</body>
Any idea?
EDIT:
I solved my problem, its a small mistake I made,
In the HTML form of the search I forgot to give the submit button: "name="search", I removed it accidently... now everything works perfectly :)
You have a typo in code
change code as below
if(isset($_GET['search'])){
$search_id = $_GET['search']; //$_GET['q'];
.
.
.
}
I solved my problem, its a small mistake I made, In the HTML form of the search I forgot to give the submit button: "name="search", I removed it accidentally... now everything works perfectly :)

Why does my form get replaced with echo "0"?

I got an error on my page that I can't identify, and when I view the site online it replaces all my site content with the value "0".
How come I can't see the forms, and is it communicating with the SQL server properly?
PS: Login works and session is created, so the connect.php does work properly.
Here is the code for members.php:
<?php
// starting session
session_start();
// check if user is logged in
if (!isset($_SESSION['username']))
{
header('Location: http://wwww.gjertgjersund.com/');
exit();
}
else
{
// database connection
require ('connect.php');
//post record count
$post_count = mysql_query("SELECT * FROM posts");
$post_count_result = mysql_num_rows($post_count);
//comment count
$comment_count = mysql_query("SELECT * FROM comments");
$comment_count_result = mysql_num_rows($comment_count);
if(isset($_POST['submit']))
{
$newcategory = $_POST['newcategory'];
if(!empty($newcategory))
{
$query = mysql_query("INSERT * INTO categories (category) VALUES ('$newcategory')";
if($query)
{
echo 'New category added';
}
}
else
{
echo 'Error';
}
}
else
{
echo 'Missing newcategory';
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title> Folder </title>
<body>
<div class="wrap">
<div id="menu">
<ul>
<li><a href='#'>Home</a></li>
<li><a href='#'>Create New Post</a></li>
<li><a href='#'>Delete Post</a></li>
<li><a href='logout.php'>Log Out</a></li>
<li><a href='#'>Blog Home Page</a></li>
</ul>
</div>
<div id="maincontent">
<table>
<tr>
<td>Total Blog Post</td>
<td><?php echo $post_count_result ?></td>
</tr>
<tr>
<td>Total Comments</td>
<td><?php echo $comment_count_result ?></td>
</tr>
</table>
<div id="categoryform">
<form action="members.php" method="post">
<label for="category">Add New Category</label>
<input type="text" name="newcategory"/>
<input type="submit" name="submit" value="Create"/>
</form>
</div>
</div>
</div>
</body>
</html>
$query = mysql_query("INSERT * INTO categories (category) VALUES ('$newcategory')";
should be
$query = mysql_query("INSERT INTO categories (category) VALUES ('$newcategory')";

using php server self for form action?

i have a profile page which uses php includes to get reviews.php and posts.php displayed on the page.
the page reviews.php has a form and i am trying to get this form to submit/use the action php server self but when i do this for some reason it ignores the mysql query on the page reviews.php and instead of inserting it into the table ptb_reviews it inserts it into ptb_posts. so the problem here is its carrying out the query on posts.php and i dont know why this is happening, can someone please help me?
here's the reviews.php page
<?php ob_start(); ?>
<?php
// check if the review form has been sent
if(isset($_POST['review_content']))
{
$content = $_POST['review_content'];
//We remove slashes depending on the configuration
if(get_magic_quotes_gpc())
{
$content = stripslashes($content);
}
//We check if all the fields are filled
if($_POST['review_content']!='')
{
{
$sql = "INSERT INTO ptb_reviews (id, from_user_id, to_user_id, content) VALUES (NULL, '".$_SESSION['user_id']."', '".$profile_id."', '".$content."');";
mysql_query($sql, $connection);
$_SESSION['message']="<div class=\"infobox-wallpost\"><strong>Thank You</strong> - Your review has been sent to the Escort and is awaiting approval.</div><div class=\"infobox-close4\"></div>";
header("Location: {$_SERVER['HTTP_REFERER']}");
} }
}
?>
<div class="reviewcontent">
<?php
$profile_bits = get_profile_bits();
while ($profile = mysql_fetch_array($profile_bits)) { ?>
<?php if ($user['account_type'] == "Escort"){
?>
<?php
$days = $reviews['date_added'];
function days_from_date2($days) {
$age = date_diff(date_create($days), date_create('now'))->d;
return $age;
}
?>
<div class="reviewcontent1">
<?php
$reviews_set = get_reviews();
?>
<br/>
<h3><span class="underline"><?php echo "".$profile['display_name'].""; ?>'s Latest Reviews</span></h3>
<br/>
<?php
if(mysql_num_rows($reviews_set) > 0) {
while ($reviews = mysql_fetch_array($reviews_set)) {
$age = days_from_date($reviews['date_added']);
?>
<div class="review_box" id="reviewcase">
<div class="review-content2">
<?php echo "{$reviews['content']}"; ?>
</div>
<div class="message_pic2">
<?php echo "<img width=\"50px\" height=\"50px\" src=\"data/photos/{$reviews['from_user_id']}/_default.jpg\">";?>
</div>
<div class="reviews_footer2">
<?php echo "Posted by {$reviews['display_name']}"; ?> <?
$datetime1 = new DateTime();
$datetime2 = new DateTime ($reviews['date_added']);
$interval = $datetime1->diff($datetime2);
$mdhms = explode('-',$interval->format('%m-%d-%H-%i-%s'));
$labels = Array(' months', ' days', ' hours', ' minutes', ' seconds');
$i = 0;
foreach($mdhms as $t){
if($t > 0) break;
$i+=1;
}
echo "about ".$t.$labels[$i]." ago"; ?>
</div>
</div>
<?
$account_type = account_type();
while ($acctype = mysql_fetch_array($account_type))
if ($acctype['account_type'] == 'Client') {
echo "
<div class=\"add_review_text\">add a review now</div>";
} ?>
<?
}
}else{
if (!logged_in()) {
echo "
<div class=\"review_box_example\"><div class=\"example_review_pic\"><img width=50px height= 50px src=\"data/photos/0/post.jpg\"/>
</div><div class=\"example_review_text2\">{$profile['display_name']}, you were fantastic!<br/>I can not wait to meet with you again, you were very professional and made me feel very comfortable.<br/><br/>John x</div><div class=\"example_review_pic2\"></div></div><div class=\"example_box_container2\"><div class=\"review_example_arrow2\"></div><div class=\"review_example_box2\"></div><div class=\"review_example_text2\"><strong>Here's an Example</strong></div></div>";
}else{
if (logged_in()) {
echo "
<div class=\"review_box_example\"><div class=\"example_review_pic\"><img width=50px height= 50px src=\"data/photos/0/post.jpg\"/>
</div><div class=\"example_review_text2\">{$profile['display_name']}, you were fantastic!<br/>I can not wait to meet with you again, you were very professional and made me feel very comfortable.<br/><br/>John x</div><div class=\"example_review_pic2\"></div></div><div class=\"example_box_container2\"><div class=\"review_example_arrow2\"></div><div class=\"review_example_box2\"></div><div class=\"review_example_text2\"><strong>Here's an Example</strong></div></div>"; ?>
<?
$account_type = account_type();
while ($acctype = mysql_fetch_array($account_type))
if ($acctype['account_type'] == 'Client') {
echo "
<div class=\"add_review_text\">add a review now</div>";
} ?>
<?
$account_type = account_type();
if ($acctype['account_type'] == 'Escort') {
echo "<div class=\"infobox-noreviews\"><i>{$profile['display_name']} doesn't currently have any posts on his wall.<br/>Why not post a comment to his wall now.</i></div>";
}
}
?>
<?
} }
?>
<?php
{?>
<? } } } ?>
</div>
</div>
<div class="reviewcontent2">
i love me
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
<textarea name="review_content" id="review_content" maxlength="180" cols="33" rows="5" class="review_input_box2" style="resize: none;"></textarea>
<input name="add_review" type="image" src="http://www.playtimeboys.com/assets/img/icons/save-edit.png" BORDER="0" ALT="SUBMIT!"class="review_submit4" /></form>
</div>
<script>
$(".reviewcontent2").hide();
$('.add_review_text').click(function () {
if ($('.reviewcontent1').is(":visible")) {
$(".reviewcontent1").fadeOut(300);
$('.reviewcontent2').delay(400).fadeIn(300);
} else if ($('.reviewcontent2').is(":visible")) {
$('.reviewcontent2').fadeOut(300);
$('.reviewcontent1').delay(400).fadeIn(300);
}
});
</script>
<?php ob_end_flush() ?>
To submit a form to the current page, which is what it seems you are trying to do, just specify an empty action:
<form action="" method="post">
The form will now submit to the same URL as the current page is.

Categories