Jump to content


Photo

haveing a problem with mysql_query("insert


  • Please log in to reply
5 replies to this topic

#1 shortj75

shortj75
  • Members
  • PipPipPip
  • Advanced Member
  • 118 posts
  • LocationPort Huron,mi

Posted 14 March 2006 - 04:00 PM


hey everyone i am haveing a problem inserting into my db my code works it inserts the input to the db but when it does it inserts it 2-3 times when i hit the submit button i have benn trying to figure this out for a white now and i cant figure it out and was wondering if any one can help it would be great here it the code and thanks in advance


if($_POST['submit']==true){

$getforums="SELECT * from forum";
$getforums2=mysql_query($getforums) or die(mysql_error());
while($getforums3=mysql_fetch_array($getforums2))
{
$cat=$_POST['cid'];
$for=$_POST['fid'];
$user=$_SESSION['user'];
$date=date("n/j/y g:i:s A");
$title=$_POST['title'];
$subject=$_POST['subject'];
$post=$_POST['post'];
$sql="INSERT into posted(forum,cat,user,date,title,subject,post)values('$for', '$cat', '$user', '$date', '$title', '$subject', '$post')";
mysql_query($sql) or die ("Could not Post");
mysql_query("UPDATE register set post=post ++1 where user='$_SESSION[user]'") or die("could not update posts");
}

print "<center><table cellpaddin=0 cellspacing=0 width=50% class=td><th class=th>$ID<TR><TD classspecialtable>";
echo "Post Submitted <a href=viewcat.php?cid=$cat&fid=$for>back to forum</a>";
echo "</center></th></tr></td></table></center>";
}else{
$ID=$_GET['cid'];
$for=$_GET['fid'];
print "<center><table cellpaddin=0 cellspacing=0 width=50% class=td><th class=th>$ID<TR><TD>";
if (isset($_SESSION['user']))
{
$user = $_SESSION['user'];
$getuser = "SELECT * from register where user='$user'";
$getuser2 = mysql_query($getuser) or die("Could not get user info");
$getuser3 = mysql_fetch_array($getuser2);

print "<table cellpadding=0 cellspacing=0 width=100%><TD class=specialtable>
<form method=post action=post.php>
<input type=hidden name=cid value=$ID>
<input type=hidden name=fid value=$for>
Title:&nbsp<input type=text name=title><BR>
Subject:&nbsp<input type=text name=subject><BR>
Post:<BR>
<textarea name=post cols=60 rows=15></textarea><br>
<input type=submit name=submit value=Post style=border-style:solid;border-color:black;border-width:1px;bgcolor:silver;color:black;>
</form></th></td></table>";
print "</th></td></table>";
}}
The Short One

hopfully this helps if it doesn't sorry
i won't write the code for you!!! you don't learn that way

#2 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 15 March 2006 - 12:04 AM

That is without a doubt, the messiest code I have ever seen. It just makes no sense at all. Please clean up your code.

#3 shortj75

shortj75
  • Members
  • PipPipPip
  • Advanced Member
  • 118 posts
  • LocationPort Huron,mi

Posted 15 March 2006 - 08:00 AM

this is the part of the code that is inserting twice

$cat=$_POST['cid'];
$for=$_POST['fid'];
$user=$_SESSION['user'];
$date=date("n/j/y g:i:s A");
$title=$_POST['title'];
$subject=$_POST['subject'];
$post=$_POST['post'];
$sql="INSERT into posted(forum,cat,user,date,title,subject,post)values('$for', '$cat', '$user', '$date', '$title', '$subject', '$post')";
mysql_query($sql) or die ("Could not Post");

The Short One

hopfully this helps if it doesn't sorry
i won't write the code for you!!! you don't learn that way

#4 greycap

greycap
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 15 March 2006 - 08:15 AM

[!--quoteo(post=355262:date=Mar 15 2006, 02:00 AM:name=shortj75)--][div class=\'quotetop\']QUOTE(shortj75 @ Mar 15 2006, 02:00 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
this is the part of the code that is inserting twice

$cat=$_POST['cid'];
$for=$_POST['fid'];
$user=$_SESSION['user'];
$date=date("n/j/y g:i:s A");
$title=$_POST['title'];
$subject=$_POST['subject'];
$post=$_POST['post'];
$sql="INSERT into posted(forum,cat,user,date,title,subject,post)values('$for', '$cat', '$user', '$date', '$title', '$subject', '$post')";
mysql_query($sql) or die ("Could not Post");
[/quote]


Your insert takes place inside a while loop which iterates through each row returned in "SELECT * from forum"

#5 keeB

keeB
  • Staff Alumni
  • Advanced Member
  • 1,078 posts
  • LocationCalifornia

Posted 15 March 2006 - 08:27 AM

That gave me a headache.. how can you read that?

Come visit my site to see my latest projects
http://nick.stinemates.org/wordpress/


#6 shortj75

shortj75
  • Members
  • PipPipPip
  • Advanced Member
  • 118 posts
  • LocationPort Huron,mi

Posted 16 March 2006 - 08:06 PM

I took it out of the while loop and it works right now thanks for the help
The Short One

hopfully this helps if it doesn't sorry
i won't write the code for you!!! you don't learn that way




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users