11 Replies - 4426 Views - Last Post: 10 June 2009 - 06:12 PM Rate Topic: -----

#1 mycraz   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 81
  • Joined: 10-June 09

get data from another page and insert into database

Posted 10 June 2009 - 01:19 AM

hi, i want to pass blog_id from blog.php to comment.php and then insert the value blog_id into the comment_table. I am able to pass it to another page but somehow it did not let me to insert into the database. Can anyone help?
$passit = $_GET["passit"];

if(isset($_POST['postcomment']))
{
	if((isset($_POST['comments']) && !empty($_POST['comments'])) && (isset($_POST['name']) && !empty($_POST['name'])))
	{
			  $time = time();
		  $today = date("Y-m-j");
		  $query = "INSERT INTO comments (blog_id, name, comments_details, comment_date, time) VALUES('$passit', '$_POST[name]','$_POST[comments]','$today', '$time')";	
		  $result = mysql_query($query);




Is This A Good Question/Topic? 0
  • +

Replies To: get data from another page and insert into database

#2 RudiVisser   User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,566
  • Joined: 05-June 09

Re: get data from another page and insert into database

Posted 10 June 2009 - 01:36 AM

So you have a form on that page? Is the form tag linked like this?

<form action="comments.php?passit=<?php echo $passit; ?>" method="post">


Rather than just action="comments.php", because if not it obviously won't be passed.

What's the exact problem though, above will help if the data is getting entered *without an id*, but if it's not getting entered at all, you have a problem with your form.
Was This Post Helpful? 0
  • +
  • -

#3 peihwa330   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 62
  • Joined: 15-March 07

Re: get data from another page and insert into database

Posted 10 June 2009 - 08:06 AM

My problem is i couldn't pass the $passit which is the blog_id from blogdisplay.php to comment.php and then insert $passit into comments_table in database. I got blogdisplay.php like this
$sql="SELECT * FROM blog";
$result=mysql_query($sql);

  
while($nt=mysql_fetch_array($result)){
	
	$blogcontent = html_entity_decode(stripslashes($nt[blog_details]));
	$time = date("h:i:s", $nt[time]);
	$passit = $nt[blog_id];
	echo "<div class='title'><h3><strong>$nt[title]</strong></h3>";
	echo "$blogcontent";
	echo "<label><strong><h5>Posted by :$nt[username]</strong>&nbsp;&nbsp;";
	echo "<label><strong>Category :$nt[category]</strong>&nbsp;";
	echo "<label><strong>$nt[blog_date]</strong>&nbsp;&nbsp; ";
	echo "<label><strong>$time</h5></strong>";
	echo "$passit";
?>
	<html>
	<body>
	<a href="java script:poptastic('comment.php?passit=<?php echo "$passit"; ?>');">Comment</a>
	</body>
	</html>
<?php
 }
?>



and also comment.php like this
$getpassit = $_GET['passit'];
if(isset($_POST['postcomment']))
{
	if((isset($_POST['comments']) && !empty($_POST['comments'])) && (isset($_POST['name']) && !empty($_POST['name'])))
	{
	
		  $time = time();
		  $today = date("Y-m-j");
		  $query = "INSERT INTO comments (blog_id, name, comments_details, comment_date, time) VALUES('$getpassit', '$_POST[name]','$_POST[comments]','$today', '$time')";	
			  $result = mysql_query($query);
			
			if($result){
			$alert = "Your comments is uploaded.Thank you.";
			 echo '<script type="text/javascript">alert("'.$alert.'");</script>';
			}
		else{
			$alert = "Your comments could not be processed. Please try again.";
			 echo '<script type="text/javascript">alert("'.$alert.'");</script>';}
			 }	
	else
	{
		//alert message for empty input
		$alert = "No empty inputs please.";
		echo '<script type="text/javascript">alert("'.$alert.'");</script>';
	}
			 		  
}


Was This Post Helpful? 0
  • +
  • -

#4 gregwhitworth   User is offline

  • Tired.
  • member icon

Reputation: 221
  • View blog
  • Posts: 1,613
  • Joined: 20-January 09

Re: get data from another page and insert into database

Posted 10 June 2009 - 08:13 AM

Have you been able to echo $getpassit on comment.php?
Was This Post Helpful? 0
  • +
  • -

#5 peihwa330   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 62
  • Joined: 15-March 07

Re: get data from another page and insert into database

Posted 10 June 2009 - 09:02 AM

yes. I can echo it and can also display it from the query.i can't figure out why it did not want to insert it. :(
Was This Post Helpful? 0
  • +
  • -

#6 RudiVisser   User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,566
  • Joined: 05-June 09

Re: get data from another page and insert into database

Posted 10 June 2009 - 09:06 AM

So it goes to the else {} after your query? Have you tried reading mysql_error()?
Was This Post Helpful? 0
  • +
  • -

#7 peihwa330   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 62
  • Joined: 15-March 07

Re: get data from another page and insert into database

Posted 10 June 2009 - 09:19 AM

It did not go to the else{}. Every data can be inserted into the database except the field blog_id. It's weird.
Was This Post Helpful? 0
  • +
  • -

#8 RudiVisser   User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,566
  • Joined: 05-June 09

Re: get data from another page and insert into database

Posted 10 June 2009 - 09:26 AM

Okay well, simply echo $getpassit before the query, right before it, within the if() statement.

If it doesn't work then you probably didn't change the <form> tag like I suggested before :)
Was This Post Helpful? 0
  • +
  • -

#9 peihwa330   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 62
  • Joined: 15-March 07

Re: get data from another page and insert into database

Posted 10 June 2009 - 05:33 PM

It works now. Thanks for ur help. :D
Was This Post Helpful? 0
  • +
  • -

#10 mycraz   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 81
  • Joined: 10-June 09

Re: get data from another page and insert into database

Posted 10 June 2009 - 05:39 PM

I've got it now.Thanks :D
But somehow, after I insert into the database, the new data did not come out in blogdisplay.php. I reopen the popup window only can see the newly inserted data. How can I show the data right after the insertion without refreshing the window or reopen the window?
Was This Post Helpful? 0
  • +
  • -

#11 RudiVisser   User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,566
  • Joined: 05-June 09

Re: get data from another page and insert into database

Posted 10 June 2009 - 05:53 PM

Simply make sure that you run the INSERT query before the SELECT query on your page.
Was This Post Helpful? 0
  • +
  • -

#12 mycraz   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 81
  • Joined: 10-June 09

Re: get data from another page and insert into database

Posted 10 June 2009 - 06:12 PM

Got it working perfectly finally. :D Thanks a lot MageUK.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1