PGTibs Posted March 22, 2009 Share Posted March 22, 2009 I'm building a webpage where you can leave comments which go into a database, however when you submit the form the query runs twice and i can't find the problem. I've looked online but that hasn't helped me any. if($comment == "sub") { $comnn = $_SESSION['session_username']; $ccomm = $_POST["ccom"]; $sub = mysql_query("INSERT INTO `$IDC` (`comn`, `comm`) VALUES ('$comnn', '$ccomm');"); echo "Comment Posted..."; echo "<meta http-equiv=\"refresh\" content=\"2;url=profile.php?user=$name\">"; } else if($comment == "add") { echo " <div style='padding: 10px;'><form method='POST' action='profile.php?user=$name&comment=sub'> <textarea style='width: 100%;' id='ccom' name='ccom'>Add comment here...</textarea><br> <div align='right' style='padding-bottom: 10px; border-bottom: #41A317 2px solid;'> <input type='submit' name='submit' value='Post'></div></form>"; } else { etc...etc... Quote Link to comment https://forums.phpfreaks.com/topic/150554-query-submitting-twice/ Share on other sites More sharing options...
Yesideez Posted March 22, 2009 Share Posted March 22, 2009 First thing I suggest is changing the name of the submit button to something else. That way you can check specifically for the submit button, something like this: if ($_POST['subsend']) { //submit button called "subsend" was clicked } The HTML would be... <input type="submit" name="subsend" value="Post" /> Another thing I do when scripts don't go to plan is echo a number out every now and then like immediately after an IF condition so I can see what route is being taken. if (condition) { echo '1'; //code for "true" here } else { echo '2'; //code for "false" here } That way I can follow my script through. I see you're using HTTP_EQUIV to refresh the page - set it to a larger value while testing/debugging to give you time to read the content of a page. You can also view the page source to see where the link should be going if you need to. Quote Link to comment https://forums.phpfreaks.com/topic/150554-query-submitting-twice/#findComment-790789 Share on other sites More sharing options...
jackpf Posted March 22, 2009 Share Posted March 22, 2009 Hmm...the only problem with checking to see if the submit button has been pressed is that if the user presses enter it doesn't recognise it. However, this may only be for ie6 and possibly 7, I haven't fully tested it. I'd check to see if any of the fields are there, as they'll always be present if the form is submitted. And lol, nice signature. Funny. Quote Link to comment https://forums.phpfreaks.com/topic/150554-query-submitting-twice/#findComment-790796 Share on other sites More sharing options...
Yesideez Posted March 22, 2009 Share Posted March 22, 2009 Hmm...the only problem with checking to see if the submit button has been pressed is that if the user presses enter it doesn't recognise it Yes this does work when ENTER is pressed. Pressing ENTER is just like clicking the submit button and I've never experienced problems. Quote Link to comment https://forums.phpfreaks.com/topic/150554-query-submitting-twice/#findComment-790813 Share on other sites More sharing options...
jackpf Posted March 22, 2009 Share Posted March 22, 2009 http://chericos.org/node/35 Read that if you don't believe me... Quote Link to comment https://forums.phpfreaks.com/topic/150554-query-submitting-twice/#findComment-790824 Share on other sites More sharing options...
jackpf Posted March 22, 2009 Share Posted March 22, 2009 In fact, just try this out: <?php if(!isset($_POST['submit'])) { echo 'Try pressing enter in the text input in IE...'; echo '<form action="" method="post"> <div> <input type="text" /> <input type="submit" name="submit" /> </div> </form>'; } else { echo 'Submitted'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/150554-query-submitting-twice/#findComment-790828 Share on other sites More sharing options...
redarrow Posted March 22, 2009 Share Posted March 22, 2009 if(!isset($_POST['submit'])) <<< really not isset lol ((this means if it is set ignore it. !isset hahahahaha if(isset($_POST['submit'])) <<< better The example was this ((we crawl before walking)) <?php if ($_POST['subsend']) { //submit button called "subsend" was clicked } ?> you can add isset, but you no it set, because you was told, to name the submit button, the same name. Quote Link to comment https://forums.phpfreaks.com/topic/150554-query-submitting-twice/#findComment-790838 Share on other sites More sharing options...
jackpf Posted March 22, 2009 Share Posted March 22, 2009 What? It means the same thing. if(isset(whatever)) { do thing } else { do thing 2 } is exactly the same as if(!isset(whatever)) { do thing 2 } else { do thing } Either way it doesn't work in IE Quote Link to comment https://forums.phpfreaks.com/topic/150554-query-submitting-twice/#findComment-790851 Share on other sites More sharing options...
redarrow Posted March 22, 2009 Share Posted March 22, 2009 action=" " not action="" <?php if(isset($_POST['submit'])) { echo 'Try pressing enter in the text input in IE...'; echo '<form action=" " method="POST"> <div> <input type="text" /> <input type="submit" name="submit" /> </div> </form>'; } else { echo 'Submitted'; } ?> or <?php if(isset($_POST['submit'])) { echo 'Try pressing enter in the text input in IE...'; echo '<form method="POST"> <div> <input type="text" /> <input type="submit" name="submit" /> </div> </form>'; } else { echo 'Submitted'; } ?> or <?php if(isset($_POST['submit'])) { $submit_me=$_SERVER['PHP_SELF']; echo 'Try pressing enter in the text input in IE...'; echo '<form action="$submit_me" method="POST"> <div> <input type="text" /> <input type="submit" name="submit" /> </div> </form>'; } else { echo 'Submitted'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/150554-query-submitting-twice/#findComment-790854 Share on other sites More sharing options...
jackpf Posted March 22, 2009 Share Posted March 22, 2009 None of them would work because the form is being displayed when it's submitted, thus is a paradox and cannot be displayed. Do you actually know what isset() does?? Learn about what you're saying before you say it... Also, all of them actions mean exactly the same thing, except for where you left it out, as it's invalid html. What are you trying to do anyway, list as many ways of doing the same thing as possible?? Quote Link to comment https://forums.phpfreaks.com/topic/150554-query-submitting-twice/#findComment-790857 Share on other sites More sharing options...
redarrow Posted March 22, 2009 Share Posted March 22, 2009 jackpf was a example ok dude. try this for fun...... <?php if(isset($_POST['submit2'])){ $go=$_POST['go']; echo $go; exit; } if(isset($_POST['submit1'])) { $my_test=$_POST['my_test']; echo '<form action=" " method="POST"> <div> <input type="hidden" name="go" value="'.$my_test.'"> <input type="submit" name="submit2" value="hit this button now!"> </div> </form>'; exit; } echo '<form action=" " method="POST"> <div> <input name="my_test" type="text"> <input type="submit" name="submit1"> </div> </form>'; ?> What? It means the same thing. if(isset(whatever)) { do thing } else { do thing 2 } is exactly the same as if(!isset(whatever)) { do thing 2 } else { do thing } Either way it doesn't work in IE jackpf !isset wont show a set value try it don't believe me your talking rubish that !isset is the same as isset ok. <?php if(!isset($_POST['submit1'])) { $go-$_POSt['go']; echo $go; } echo '<form action=" " method="POST"> <div> <input type="text" name="go" > <input type="submit" name="submit1" value="hit this button now!"> </div> </form>'; exit; ?> Quote Link to comment https://forums.phpfreaks.com/topic/150554-query-submitting-twice/#findComment-790861 Share on other sites More sharing options...
redarrow Posted March 22, 2009 Share Posted March 22, 2009 jackpf Do you actually know what isset() does?? Learn about what you're saying before you say it... looks like i no what isset does so you tell me what i don't no then about isset. because like i say you wrong read my example come on. how can !isset be the same as isset you explain it to us all This example will clarify my knowledge of !isset as the variable $go can not be seen, because the name off the submit button is !isset. <?php if(!isset($_POST['submit1'])) { $go=$_POST['go']; echo $go; } echo '<form action=" " method="POST"> <div> <input type="text" name="go" > <input type="submit" name="submit1" value="hit this button now!"> </div> </form>'; exit; ?> Quote Link to comment https://forums.phpfreaks.com/topic/150554-query-submitting-twice/#findComment-790866 Share on other sites More sharing options...
PGTibs Posted March 22, 2009 Author Share Posted March 22, 2009 Right well, the $_POST['subsend'] worked, i don't quite see why but none the less it worked, so now i've got another problem which is the same it just uses a link instead? if($_SESSION['session_username'] == "$name") { echo ""; } else { echo "<div style='float:right;'> - <a href='profile.php?user=$name&add=addfriend'>Add As Friend</a> - </div>";}; echo "<h2>Friends</h2>"; if($add == "addfriend") { $friend = mysql_query("SELECT * FROM `members` WHERE `name` = '$usernamem'"); while($friendd = mysql_fetch_array($friend)) { $fid = $friendd["id"]; $IDFF = "$fid f"; $addfriend = mysql_query("INSERT INTO `$IDFF` (`maten`, `matep`) VALUES ('$user', '$photo');"); echo "Friend Added..."; The $_POST['addfriend'] doesn't work for this problem... Quote Link to comment https://forums.phpfreaks.com/topic/150554-query-submitting-twice/#findComment-790871 Share on other sites More sharing options...
redarrow Posted March 22, 2009 Share Posted March 22, 2009 if($_GET['add'] == "addfriend") { Quote Link to comment https://forums.phpfreaks.com/topic/150554-query-submitting-twice/#findComment-790873 Share on other sites More sharing options...
wildteen88 Posted March 22, 2009 Share Posted March 22, 2009 When you retrieve Variables from urls you use $_GET not $_POST. $_POST is used to to retrieve data from forms (only if you set method="post" within your form tag). This line if($add == "addfriend") { Should be if(isset($_GET['add']) && $_GET['add'] == "addfriend") { Quote Link to comment https://forums.phpfreaks.com/topic/150554-query-submitting-twice/#findComment-790874 Share on other sites More sharing options...
redarrow Posted March 22, 2009 Share Posted March 22, 2009 wildteen cleer some think up please. !iseet not the same as isset is it. i lernt that !isset is the opposite of isset am i correct please. Quote Link to comment https://forums.phpfreaks.com/topic/150554-query-submitting-twice/#findComment-790875 Share on other sites More sharing options...
wildteen88 Posted March 22, 2009 Share Posted March 22, 2009 !isset and isset do the same thing, both check to see if a variable is defined and return true or false The only difference is to do with the ! operator. Quote Link to comment https://forums.phpfreaks.com/topic/150554-query-submitting-twice/#findComment-790878 Share on other sites More sharing options...
PGTibs Posted March 22, 2009 Author Share Posted March 22, 2009 When you retrieve Variables from urls you use $_GET not $_POST. $_POST is used to to retrieve data from forms (only if you set method="post" within your form tag). This line if($add == "addfriend") { Should be if(isset($_GET['add']) && $_GET['add'] == "addfriend") { This still causes it to happen twice???? Quote Link to comment https://forums.phpfreaks.com/topic/150554-query-submitting-twice/#findComment-790879 Share on other sites More sharing options...
redarrow Posted March 22, 2009 Share Posted March 22, 2009 '$usernamem' should this be $_GET['user'] $friend = mysql_query("SELECT * FROM `members` WHERE `name` = '$usernamem'"); Quote Link to comment https://forums.phpfreaks.com/topic/150554-query-submitting-twice/#findComment-790883 Share on other sites More sharing options...
jackpf Posted March 22, 2009 Share Posted March 22, 2009 Ok, redarrow, if you actually read my code you'll see that where I've removed the ! operator I've actually switched the conditions round. Test it before you go shooting your mouth of. Yeah, test my code, then test your code. See which one works. I think you'll be surprised. Quote Link to comment https://forums.phpfreaks.com/topic/150554-query-submitting-twice/#findComment-790884 Share on other sites More sharing options...
redarrow Posted March 22, 2009 Share Posted March 22, 2009 ((((jackpf ok, i see what your saying sorry my fault mate so sorry.)) try this mate see what happens. <?php if(isset($_SESSION['session_username']) && $_SESSION['session_username'] == $name) { echo ""; } else { echo "<div style='float:right;'> - <a href='profile.php?user=$name&add=addfriend'>Add As Friend</a> - </div>";}; echo "<h2>Friends</h2>"; if(isset($_GET['add']) && $_GET['add']== "addfriend") { $usernamem=isset($_GET['user']); $friend = mysql_query("SELECT * FROM `members` WHERE `name` = '$usernamem'"); while($friendd = mysql_fetch_array($friend)) { $fid = $friendd["id"]; $IDFF = "$fid f"; $addfriend = mysql_query("INSERT INTO `$IDFF` (`maten`, `matep`) VALUES ('$user', '$photo');"); echo "Friend Added..."; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/150554-query-submitting-twice/#findComment-790894 Share on other sites More sharing options...
PGTibs Posted March 22, 2009 Author Share Posted March 22, 2009 ((((jackpf ok, i see what your saying sorry my fault mate so sorry.)) try this mate see what happens. <?php if(isset($_SESSION['session_username']) && $_SESSION['session_username'] == $name) { echo ""; } else { echo "<div style='float:right;'> - <a href='profile.php?user=$name&add=addfriend'>Add As Friend</a> - </div>";}; echo "<h2>Friends</h2>"; if(isset($_GET['add']) && $_GET['add']== "addfriend") { $usernamem=isset($_GET['user']); $friend = mysql_query("SELECT * FROM `members` WHERE `name` = '$usernamem'"); while($friendd = mysql_fetch_array($friend)) { $fid = $friendd["id"]; $IDFF = "$fid f"; $addfriend = mysql_query("INSERT INTO `$IDFF` (`maten`, `matep`) VALUES ('$user', '$photo');"); echo "Friend Added..."; } } ?> It brings up the webaddress as &add=addfriend but nothing else happens, it doesn't add anything to the database or echo anything. Quote Link to comment https://forums.phpfreaks.com/topic/150554-query-submitting-twice/#findComment-790898 Share on other sites More sharing options...
redarrow Posted March 22, 2009 Share Posted March 22, 2009 try now. i added this? $usernamem=isset($_GET['user']); // dont no if it correct thro <?php session_start() if($_SESSION['session_username'] == "$name") { echo ""; } else { echo "<div style='float:right;'> - <a href='profile.php?user=$name&add=addfriend'>Add As Friend</a> - </div>";}; echo "<h2>Friends</h2>"; if(isset($_GET['add']) && $_GET['add']== "addfriend") { $usernamem=isset($_GET['user']); $friend = mysql_query("SELECT * FROM `members` WHERE `name` = '$usernamem'"); while($friendd = mysql_fetch_array($friend)) { $fid = $friendd["id"]; $IDFF = "$fid f"; $addfriend = mysql_query("INSERT INTO `$IDFF` (`maten`, `matep`) VALUES ('$user', '$photo');"); echo "Friend Added..."; } } ?> make sure the session is equel to the $name varable. example. <?php session_start(); $name="redarrow"; $_SESSION['session_username']="redarrow"; if(isset($_SESSION['session_username']) && $_SESSION['session_username'] == $name) { echo $name; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/150554-query-submitting-twice/#findComment-790900 Share on other sites More sharing options...
PGTibs Posted March 22, 2009 Author Share Posted March 22, 2009 make sure the session is equel to the $name varable. but i only want you to be able to add the friend when it isn't on your page, if its the same as $name then its only going to show on your own page? Quote Link to comment https://forums.phpfreaks.com/topic/150554-query-submitting-twice/#findComment-790906 Share on other sites More sharing options...
jackpf Posted March 22, 2009 Share Posted March 22, 2009 Lol yeah, redarrow, my apologies too. Quote Link to comment https://forums.phpfreaks.com/topic/150554-query-submitting-twice/#findComment-790911 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.