I'm Beginner. I need help, please. I am having a small problem in the script I am have in this script ( admin folder) Add contain articles (This page is working to add threads only) My problem in the Page (read_mor.php) i make a query with the database to bring news this is query .

<?php 
include ('config/connect_to_mysql.php');
$id = $_GET["id"];
$fetch = mysql_query("SELECT * FROM posts WHERE id= '$id' ") or die(mysql_error());
while($myrow = mysql_fetch_assoc($fetch)) {
?>
<?php echo $myrow['title']; ?>
<?php echo $myrow['created_on']; ?>
<?php echo $myrow['categories']; ?>
<?php echo $myrow['description']; ?>
<?php echo $myrow['categories']; ?>
<?php } ?>

everything is v god

but when i insert comment in table (comments) all comments in table show with any post ..!!

i need to insert comment with article by id article

this page for insert comment

<?php
include ('config/connect_to_mysql.php');

$query="INSERT INTO comments (com_id,name,url,email,comment,postid)
      VALUES
      ('', '$_POST[name]' , '$_POST[url]', '$_POST[email]', '$_POST[comment]', LAST_INSERT_ID(postid))" or die(mysql_error());

if ($query)

$result = mysql_query($query);

{
echo "<strong>Thanks!</strong><p>Your message was successfully sent.</p>";  
}
?>

Table in database ---- >

comments

 structure for table `comments`
 (
  `com_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `url` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `comment` text COLLATE utf8mb4_unicode_ci NOT NULL,
  `dt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `yes` int(11) NOT NULL,
  `postid` int(11) NOT NULL,
  PRIMARY KEY (`com_id`)
) ENGINE=MyISAM  DEFAULT

table of posts ------>

CREATE TABLE IF NOT EXISTS `posts` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(200) NOT NULL,
  `description` longtext NOT NULL,
  `categories` varchar(200) NOT NULL,
  `post_meta` varchar(200) NOT NULL,
  `post_robots` varchar(200) NOT NULL,
  `meta_title` varchar(200) NOT NULL,
  `meta_description` text NOT NULL,
  `created_by` varchar(100) NOT NULL,
  `created_on` datetime NOT NULL,
  `imageref` varchar(300) NOT NULL,
  `post_status` varchar(70) NOT NULL DEFAULT 'published',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=102 ;


When i do submit comment inserting but when i open the post all comment will show because ID in posts table is not associated with the postid in comment
so i need when i post comment - comment show with post by id how can i insert postid in comments table with id in posts table and how can show topic with comment : if i used inner join How can i used it .. !!

Recommended Answers

All 7 Replies

confusing cause you seem to know how it should be:

articles
id,title
1,article1
2,article2
3,article3

comments
id,artid,comment
1,2,hello
2,2,hi
3,1,nice article
4,3,another comment

article.php?id=1
$Q = "select * from comments where artid = $id";

My problem not with select my problem how i can insert post with comment i can select comment with post no problem - But when i post comment i ned comment show with post

<?php
    include ('config/connect_to_mysql.php');
     
    $query="INSERT INTO comments (com_id,name,url,email,comment,postid)
    VALUES
    ('', '$_POST[name]' , '$_POST[url]', '$_POST[email]', '$_POST[comment]', LAST_INSERT_ID(postid))" or die(mysql_error());
     
    if ($query)
     
    $result = mysql_query($query);
     
    {
    echo "<strong>Thanks!</strong><p>Your message was successfully sent.</p>";
    }
    ?>

in posts table i have ID Okay
In commentes table have postid
I need when visitor submit comment comment inserting in commenting table and postid in comment Set Number of topic How Can i do this when insert ( value of ID = Postid ... !! This is my problem ...

you need to pass on the post id to that page so it knows which post the person is replying to

<?php
    include ('config/connect_to_mysql.php');
$id = $_GET['postid'];
if(ctype_digit($id)){
    $query="INSERT INTO comments (com_id,name,url,email,comment,postid)
    VALUES
    ('', '$_POST[name]' , '$_POST[url]', '$_POST[email]', '$_POST[comment]', $id)" or die(mysql_error());
 
    if ($query) 
    $result = mysql_query($query);
    {
    echo "<strong>Thanks!</strong><p>Your message was successfully sent.</p>";
    }
}else{
echo 'id invalid';
}
    ?>

( ! ) Notice: Undefined index: postid in C:\wamp\www\CmsSystem\submit_to_db.php on line 4
Call Stack
# Time Memory Function Location
1 0.0017 372584 {main}( ) ..\submit_to_db.php:0
id invalid


id invalid

....................................................................................

Do you think that this error could be the result of a page submit_to_db.php ( An external page )
Which this code inside
or because I link php to j query
I will put to you two full pages

....................................................................................

read_mor.php

<!--header-->
<?php include 'styles/header.php'; ?>
<!--header-->
<?php 
include ('config/connect_to_mysql.php');
$id = $_GET["id"];
$fetch = mysql_query("SELECT * FROM posts WHERE id= '$id' ") or die(mysql_error());




while($myrow = mysql_fetch_assoc($fetch)) {
?>
<title>name.COM <?php echo $myrow['title']; ?></title>

<div id="main-content" class="intro">
<div class="entry-holder">	
<h2 class="title"><?php echo $myrow['title']; ?></h2>
</div><!--END entry-holder-->

<div id="inner-content" class="left">
<div class="post" id="contact">		
<div class="two-third" style="width:600px">

<div id="inner-content" class="left">	
<div class="post post-single">
<div class="post-img-holder"><img src="admin/featured-uploads/<?php echo $myrow['imageref']; ?>" alt="" width="630" height="300" /></div>
<div class="post-meta">			

<span>Posted on ( </span><?php echo $myrow['created_on']; ?> 
<span >) <span>category </span><a href="#"><?php echo $myrow['categories']; ?></a>
with </span><a href="#comment-wrap">5 comments</a>									
</div><!--END post-meta-->


<p><?php echo $myrow['description']; ?>.</p>	

<div class="author">
<strong>Description</strong>	
<p><?php echo $myrow['meta_description']; ?>.</p>
</div><!--END author-->	



<div class="post-category">	
<ul class="tags">
<li><span class="icon"></span></li>
<li><a href="#"><?php echo $myrow['categories']; ?></a>,</li>
</ul><!--END tags-->		

<!--share-->								
<ul class="share">
<li class="icon"><span>share</span></li>
<li class="google">
<script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script>
<g:plusone size="medium"></g:plusone>
</li>

<li class="twitter">
<a href="https://twitter.com/share" class="twitter-share-button">Tweet</a>
<script type="text/javascript" src="//platform.twitter.com/widgets.js"></script>
</li>
									
<li class="facebook">					
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>		
<div class="fb-like" data-send="false" data-layout="button_count" data-width="450" data-show-faces="true"></div>
</li>
</ul><!--END share-->













<p></p><p><br /></p><p></p><p><br /></p>


<a href="<?php echo "javascript:self.history.back()" ?>" class="more" style="font-size:16px"> Go Back</a>	

</div><!--END post-category-->
</div><!--END post-entry-->					
</div><!--END post-->	
<?php } ?>	
	


















<div id="comment-wrap">
<div class="comments">
<div class="heading-link"><strong>5 Comments / <a href="#comment-form">Leave a comment &raquo;</a></strong></div>
<ul id="comment-list">	



<?php
//$result = mysql_query("SELECT * FROM comments WHERE com_id='$id' ORDER BY 'postid' DESC");
$result = mysql_query("SELECT * FROM comments WHERE postid='$id'");
$count = mysql_num_rows($result);
if ($count != 0)
{
while ($row = mysql_fetch_array($result)){
$com_id = $row['com_id'];
$name = nl2br($row['name']);
$comment = nl2br($row['comment']);
$url = nl2br($row['url']);
$postid = nl2br($row['postid']);
$dt = nl2br($row['dt']);
$dt=time();
?>  
<li>
<div class="comment-meta">
<div class="avatar">
<img  src="styles/images/avatar.jpg" alt="" />
</div><!--END avatar--> 
<div  style="overflow:hidden">
<span class="comment-author"><a href="#"><?php  echo $row['name']; ?></a></span>
</div>
<div>
<span class="date"><?php  echo(date("Y/m/d  h:i:s")); ?></span>
</div>
</div><!--END comment-mate--> 
                        				
<div class="comment">
<p><?php  echo $row['comment']; ?>.</p>
<span class="reply"><a href="#">Reply &raquo;</a></span> 
</div><!--END comment-->
</li>
<?php
}
}
mysql_free_result($result);
?>   

</ul><!--END comment-list-->
</div><!--END comments-->
    
        







  


<div id="commentform-wrap">
<div class="heading-link"><strong>Leave a comment</strong></div>
<ul>

<form method="post" action="submit_to_db.php" id="comment-form">
<li>
<label for="name">Name</label>
<input type="text" name="name" id="name" />
</li>
<li>	
<label for="email">Email Address</label>		
<input type="text" name="email" id="email" />
</li>
<li>
<label for="url">URL</label>	
<input type="text" name="url" id="url" />
</li>
<li class="textarea">
<label for="comment">Comments/Concerns</label>	
<textarea cols="20" rows="6" name="comment" id="comment"></textarea>		
</li>
<li class="button">
<input  value="Go" type="submit" name="submit" id="submit" style=" background:#0060a1; color:#FFFFFF; font-size:14px; border:0; " />		
</li>
<br />
<br /><br />


</form> 
</ul>


</div><!--END commentform-wrap-->
</div><!--END ID comment-wrap-->












</div><!--END two-third-->
</div><!--END post-->						
</div><!--END inner-content-->
<?php include 'styles/sidebar.php'; ?>

</div><!--END content-->
</div><!--END wrapper-->
<!--footer-->
<?php include 'styles/footer.php'; ?>
<!--footer-->

submit_to_db.php

<?php
include ('config/connect_to_mysql.php');

$id = $_GET['postid'];
if(ctype_digit($id)){
    $query="INSERT INTO comments (com_id,name,url,email,comment,postid)
    VALUES
    ('', '$_POST[name]' , '$_POST[url]', '$_POST[email]', '$_POST[comment]', $id)" or die(mysql_error());
 
    if ($query) 
    $result = mysql_query($query);
    {
    echo "<strong>Thanks!</strong><p>Your message was successfully sent.</p>";
    }
}else{
echo 'id invalid';
}
?>

update the action to this:

<form method="post" action="submit_to_db.php?postid=<?php echo $id;?>" id="comment-form">
Notice: Undefined index: postid in C:\wamp\www\CmsSystem\submit_to_db.php on line 4
Call Stack
#	Time	Memory	Function	Location
1	0.0018	373056	{main}( )	..\submit_to_db.php:0
id invalid

table in database

COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `comment` text COLLATE utf8mb4_unicode_ci NOT NULL,
  `dt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `postid` int(11) NOT NULL,
  PRIMARY KEY (`com_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci AUTO_INCREMENT=217 ;

jquery

<script type="text/javascript">
$(function(){
 
$('#submit').click(function(){
$('#container').append('<img src="img/loader.gif" alt="success" id="success"/>');
var name= $('#name').val();
var email= $('#email').val();
var url= $('#url').val();
var comment= $('#comment').val();
$.ajax({
									
url: 'submit_to_db.php',
type : 'POST',
data : 'name=' + name + '&email=' + email + '&url=' + url + '&comment=' + comment,

success : function(result) {
$('#success').remove();
$('#container').append('<div id="success">' + result + '</div>');
$('#success').fadeOut(500,function(){
$(this).remove();
});
}
});
return false;});});
</script>

You are amazing maaaaaaaaaaaaaaaan thanks V thanks v v thanks

everything is good my error in jquery code if you can help meee XD

when i delete java everything is good

<script type="text/javascript">
$(function(){
 
$('#submit').click(function(){
$('#container').append('<img src="img/loader.gif" alt="success" id="success"/>');
var name= $('#name').val();
var email= $('#email').val();
var url= $('#url').val();
var comment= $('#comment').val();
$.ajax({
									
url: 'submit_to_db.php',
type : 'POST',
data : 'name=' + name + '&email=' + email + '&url=' + url + '&comment=' + comment,

success : function(result) {
$('#success').remove();
$('#container').append('<div id="success">' + result + '</div>');
$('#success').fadeOut(500,function(){
$(this).remove();
});
}
});
return false;});});
</script>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.