Jump to content

Recommended Posts

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...

Link to comment
https://forums.phpfreaks.com/topic/150554-query-submitting-twice/
Share on other sites

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.

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.

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.

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';
}
?>

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.

 

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';
   }
?>

 

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??

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 :P

 

 

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;
   
  ?>

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;
   
  ?>

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...

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") {

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????

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.

((((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...";
          }
}
           ?>

((((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.

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

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.