Hi

The code below inserts data into a mysql db, for some weird reason IE inserts it 2 times and firefox just once.
It should only insert it once.

 $KOSoep = $_POST['KOsoep'];
 $KOHoofdgerecht = $_POST['KOhoofdgerecht'];
 $KODessert = $_POST['KOdessert'];                
 //insert query kinderopvang
 $insert_kinderopvang="INSERT INTO `menukinderopvang` (
 `datum` ,  `soep` , `hoofdgerecht`,`dessert` )
 VALUES 
 ($Datum','$KOSoep','$KOHoofdgerecht','$KODessert')";
  mysql_query($insert_kinderopvang);                    

I searched my code and I use only once mysql_query($insert_kinderopvang);

Thanks in advance.

Member Avatar for diafol

Any javascript running? Are you sending this form to the same page to be processed instead of sending it to a processing file?

no javascript, I send it to the same page to be processed

Member Avatar for diafol

SHow your code (more of it) and form.

BTW your sql query is open to SQL injection - don't use it on a live site.

The form:

<form id="menu" method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
 <table>
                <tr>
                    <td>
                        <button onclick="Opslaan();"><img src="css/save.png"  height="25" width="25" alt="">  Opslaan</button> 
                    </td>
                </tr>
</table>
 <table id='box-table-a'>
            <tr>
              <th scope='col' width="200">School</th>    
                <th scope='col' width="200">Kinderopvang</th>                  
            </tr>
             <tr>
                <th rowspan="2">Ontbijt</th>
                 <td><input type="hidden" name="SHID" value="<?php echo $SHID; ?>"></td>
                <td><input type="hidden" name="KOID" value="<?php echo $KOID; ?>"></td>
            </tr>
             <tr>
                <th>Soep</th>
                  <td><div class="ui-widget"><input class="soep" type="text" id="SHsoep" name="SHsoep" value="<?php echo $SHsoep; ?>" size="30"></div></td>
                <td><div class="ui-widget"><input class="soep" type="text" id="KOsoep" name="KOsoep" value="<?php echo $KOsoep; ?>" size="30"></div></td>                   
            </tr> 
            <tr>
                <th>Hoofdgerecht</th>
                   <td><div class="ui-widget"><textarea class="hoofdgerecht" id="SHhoofdgerecht" name="SHhoofdgerecht" rows="3" cols="30"><?php echo $SHhoofdgerecht; ?></textarea></div></td>
                <td><div class="ui-widget"><textarea class="hoofdgerecht" id="KOhoofdgerecht" name="KOhoofdgerecht" rows="3" cols="30"><?php echo $KOhoofdgerecht; ?></textarea></div></td>           
            </tr> 
            <tr>
                <th>Nagerecht</th>
                 <td><div class="ui-widget"><input class="dessert" type="text" id="KOdessert" name="KOdessert" value="<?php echo $KOdessert; ?>" size="30"></div></td>
            </tr>
 </table>    
  function Opslaan()
            {
                document.getElementById('opslaan').value = "opslaan";
                document.getElementById("menu").submit();
            }

PHP:
This code works correct

  //Nakijken of menu school reeds bestaat
  if(!empty($_POST['SHID']))
  {
      //Menu school wijzigen
      $SHID = $_POST['SHID'];
      $SHSoep = $_POST['SHsoep'];
      $SHHoofdgerecht = $_POST['SHhoofdgerecht'];
      //update query school
      $update_school="UPDATE`menuschool` SET  `soep`='$SHSoep',`hoofdgerecht`='$SHHoofdgerecht'  WHERE`menuschool`.`Id`='$SHID'";
      mysql_query($update_school);
   }
   else
   {    
      //Menu school opslaan
      $SHSoep = $_POST['SHsoep'];
      $SHHoofdgerecht = $_POST['SHhoofdgerecht'];
      //insert query school
      $insert_school="INSERT INTO `menuschool` (
      `Id` , `datum` ,  `soep` , `hoofdgerecht` )
      VALUES 
      ( NULL ,'$Datum','$SHSoep','$SHHoofdgerecht')";
      mysql_query($insert_school);
   }

But this code below which is the same as te above inserts it 2 time with IE and only 1 time with Firefox, it should only do 1 insert.

//Nakijken of menu kinderopvang reeds bestaat
if(!empty($_POST['KOID']))
{
      //Menu kinderopvang wijzigen
      $KOID = $_POST['KOID'];
      $KOSoep = $_POST['KOsoep'];
      $KOHoofdgerecht = $_POST['KOhoofdgerecht'];
      $KODessert = $_POST['KOdessert'];
      //update query kinderopvang
      $update_kinderopvang="UPDATE`menukinderopvang` SET  `soep`='$KOSoep',`hoofdgerecht`='$KOHoofdgerecht',`dessert`='$KODessert'  WHERE`menukinderopvang`.`Id`='$KOID'";           
      mysql_query($update_kinderopvang);
}
     else
     {    
       //Menu kinderopvang opslaan
       $KOSoep = $_POST['KOsoep'];
       $KOHoofdgerecht = $_POST['KOhoofdgerecht'];
       $KODessert = $_POST['KOdessert'];                
       //insert query kinderopvang
       $insert_kinderopvang="INSERT INTO `menukinderopvang` (
       `datum` ,  `soep` , `hoofdgerecht`,`dessert` )
       VALUES 
       ( '$Datum','$KOSoep','$KOHoofdgerecht','$KODessert')";
       mysql_query($insert_kinderopvang);                    
     }

I hope this is enough code, I know the risk for mysqlinjection but it is some old code which run on local intranet where the risk of mysqlinjection is really low, now I use mysqli with prepared statements

Member Avatar for diafol

no javascript, I send it to the same page to be processed

But you are using js

function Opslaan()
        {
            document.getElementById('opslaan').value = "opslaan";
            document.getElementById("menu").submit();
        }

Using this:

        <button onclick="Opslaan();"><img src="css/save.png"  height="25" width="25" alt="">  Opslaan</button>

Not sure if that's the issue though. Comment out the button and add a normal submit button to see if that has any effect.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.