Mysql Insert

Discussion in 'MySQL' started by Alexj17, Nov 21, 2008.

  1. #1
    Hi All

    I have used this query millions of times, but now i am realising that each time it is run, it places two entries onto my table. One with all the details on and the other just blank. Normally i can just ignore this but when reading the last value off the table there is always a blank one. I cannot afford to delete these blank entries everytime one is created.

    I have a form that passes the varibale into this seperate page....

    <?php 
    
    mysql_query("INSERT INTO comments (com_member_av, com_poster, com_member_id, com_date, com_group_id, comment) VALUES('$_POST[member_av]', '$_POST[username]', '$_POST[member_id]', '$_POST[date]', '$_POST[group_id]', '$_POST[comment]') ") or die(mysql_error());
    echo "<META HTTP-EQUIV='Refresh' CONTENT='1; URL=holiday.php?id=group&destination=$destination&group_id=$group_id#comments'>";
    ?>  
    PHP:
    Any ideas why two entries are being created ??
     
    Alexj17, Nov 21, 2008 IP
  2. ariefsyu

    ariefsyu Active Member

    Messages:
    192
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #2
    Perhaps the second blank rows inserted were caused by meta refresh. Please tell me what is the filename of above scripts

    Arief
     
    ariefsyu, Nov 24, 2008 IP
  3. cipals15

    cipals15 Well-Known Member

    Messages:
    1,085
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    100
    #3
    have you tried removing the echo line?. It might be the reason. It opened again the php file that inserted the values. Since the second time it opened the file, the $_POST[] values are blank does resulting to blank fields.
     
    cipals15, Nov 28, 2008 IP
  4. Alexj17

    Alexj17 Member

    Messages:
    173
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    28
    #4
    Yeah i think that has fixed it, since removing the echo REFRESH i havent had any blank messages. Thanks

    My next problem now tho, is to do with an 'UPDATE'.

    I have a form which displays all the users current data, they can edit this information and click submit. This then passes the variables to another page where the UPDATE code is. Often the data doesnt get updated and infact all the data is removed from that entry and just left blank. (so the old data doesnt get updated, it gets deleted).

    
    
    <?php 
    
    mysql_query("UPDATE groups SET group_name = '$_POST[groupname]', group_des = '$_POST[destination]', group_date = '$_POST[date]', group_year = '$_POST[year]', group_days = '$_POST[days]', group_hotel ='$_POST[hotel]', group_info = '$_POST[group_info]', group_other_dest = '$_POST[other_des]', group_howmany = '$_POST[howmany]' WHERE group_id = '$group_id'") or die(mysql_error()); 
    
    
    echo" <h2 class='h2'>Changes Made</h2>
    
    <div class='table'><ul><li >
    
    <p class='center bold'> Your changes have been made.
    
    LINK 1 - LINK 2 - LINK 3";
    ?>
    
    GOOGLE BANNER
    </p>
    
    </li></ul></div>
    
    
    Code (markup):
    Any ideas why sometimes this is happening ??
     
    Alexj17, Dec 9, 2008 IP
  5. Sadie

    Sadie Active Member

    Messages:
    144
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #5
    Could be the 'echo'...
     
    Sadie, Dec 9, 2008 IP
  6. Alexj17

    Alexj17 Member

    Messages:
    173
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    28
    #6
    but how would that effect it ?
     
    Alexj17, Dec 9, 2008 IP
  7. Dirty-Rockstar

    Dirty-Rockstar Guest

    Messages:
    252
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #7

    The users could be submitting blank data, do you have any php above to check if the data is blank being submitted? Even if its just a "reset" button back to zero, if you are using hidden html post for that someone could edit the html, point it to the urls and run it off their desktop
    
    if($_POST[groupname]!=""){
    	if($_POST[destination]!=""){
    		if($_POST[date]!=""){
    			if($_POST[year]!=""){
    				if($_POST[days]!=""){
    					if($_POST[hotel]!=""){
    						if($_POST[group_info]!=""){
    							if($_POST[other_des]!=""){
    								if($_POST[howmany]!=""){
    mysql_query("UPDATE groups SET group_name = '$_POST[groupname]', group_des = '$_POST[destination]', group_date = '$_POST[date]', group_year = '$_POST[year]', group_days = '$_POST[days]', group_hotel ='$_POST[hotel]', group_info = '$_POST[group_info]', group_other_dest = '$_POST[other_des]', group_howmany = '$_POST[howmany]' WHERE group_id = '$group_id'") or die(mysql_error()); 
    								}else{print "you left something blank";}
    							}else{print "you left something blank";}
    						}else{print "you left something blank";}
    					}else{print "you left something blank";}
    				}else{print "you left something blank";}
    			}else{print "you left something blank";}
    		}else{print "you left something blank";}
    	}else{print "you left something blank";}
    }else{print "you left something blank";}
    
    PHP:
    Quickest thing i could cook up in 5 mins. you might also want to run is_numeric thru anything that has to be a number too, and run some security thru your post. Change the else to specific print statements of what was left blank.

    hopefully that helps you
     
    Dirty-Rockstar, Dec 11, 2008 IP
  8. Lexiseek

    Lexiseek Banned

    Messages:
    2,499
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    0
    #8
    It's a good idea to make sure your fields are populated before doing any inserts or updates.
     
    Lexiseek, Dec 11, 2008 IP