shortj75 Posted March 14, 2006 Share Posted March 14, 2006 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 advanceif($_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: <input type=text name=title><BR>Subject: <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>";}} Link to comment https://forums.phpfreaks.com/topic/4938-haveing-a-problem-with-mysql_queryinsert/ Share on other sites More sharing options...
hitman6003 Posted March 15, 2006 Share Posted March 15, 2006 That is without a doubt, the messiest code I have ever seen. It just makes no sense at all. Please clean up your code. Link to comment https://forums.phpfreaks.com/topic/4938-haveing-a-problem-with-mysql_queryinsert/#findComment-17613 Share on other sites More sharing options...
shortj75 Posted March 15, 2006 Author Share Posted March 15, 2006 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] Link to comment https://forums.phpfreaks.com/topic/4938-haveing-a-problem-with-mysql_queryinsert/#findComment-17729 Share on other sites More sharing options...
greycap Posted March 15, 2006 Share Posted March 15, 2006 [!--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" Link to comment https://forums.phpfreaks.com/topic/4938-haveing-a-problem-with-mysql_queryinsert/#findComment-17732 Share on other sites More sharing options...
keeB Posted March 15, 2006 Share Posted March 15, 2006 That gave me a headache.. how can you read that? Link to comment https://forums.phpfreaks.com/topic/4938-haveing-a-problem-with-mysql_queryinsert/#findComment-17735 Share on other sites More sharing options...
shortj75 Posted March 16, 2006 Author Share Posted March 16, 2006 I took it out of the while loop and it works right now thanks for the help Link to comment https://forums.phpfreaks.com/topic/4938-haveing-a-problem-with-mysql_queryinsert/#findComment-18098 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.