Jump to content

Archived

This topic is now archived and is closed to further replies.

shortj75

haveing a problem with mysql_query("insert

Recommended Posts


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>";
}}

Share this post


Link to post
Share on other sites
That is without a doubt, the messiest code I have ever seen. It just makes no sense at all. Please clean up your code.

Share this post


Link to post
Share on other sites
this is the part of the code that is inserting twice

[code]$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");[/code]

Share this post


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

[code]$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");[/code]
[/quote]


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

Share this post


Link to post
Share on other sites
That gave me a headache.. how can you read that?

Share this post


Link to post
Share on other sites
I took it out of the while loop and it works right now thanks for the help

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.