0

I'm new whit PHP and I'm trying to make a web app where I can create sales items modify them ECC ECC. it's a kind of warehouse management software.

the code below is the form to insert a new sales item.

the second code is the function called by the code in my form to insert the sales items data in the database.

I have an error in my code but I don't understand it.

this its my page form:

<?php
require('includes\dbconn.php');
require('includes\dbfunction.php');
session_start();

if(isset($_SESSION["username"])){
  ?>
  <!DOCTYPE html>
  <html>
  <title>W3.CSS Template</title>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Raleway">
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.5.0/css/all.css" integrity="sha384-B4dIYHKNBt8Bc12p+WXckhzcICo0wtJAoU8YZTY5qE0Id1GSseTk6S+L3BlXeVIU" crossorigin="anonymous">
  <style>
  html,body,h1,h2,h3,h4,h5 {font-family: "Raleway", sans-serif}
  </style>
  <body class="w3-light-grey">

  <!-- Top container -->
  <div class="w3-bar w3-top w3-black w3-large" style="z-index:4">
    <button class="w3-bar-item w3-button w3-hide-large w3-hover-none w3-hover-text-light-grey" onclick="w3_open();"><i class="fa fa-bars"></i>  Menu</button>
    <span class="w3-bar-item w3-right">BK2 Gestionale</span>
  </div>

  <!-- Sidebar/menu -->
  <nav class="w3-sidebar w3-collapse w3-white" style="z-index:3;width:300px;" id="mySidebar"><br>
    <div class="w3-container w3-row">
      <div class="w3-col s12 w3-bar w3-center">
        <?php
          if(isset($_SESSION["username"])){
            echo "<p>Benvenuto " . $_SESSION["username"] . "</p>";
            ?>
            <a href="#" class="w3-bar-item w3-button"><i class="fa fa-user"></i></a>
            <a href="impostazioni.php" class="w3-bar-item w3-button"><i class="fa fa-cog"></i></a>
            <a href="logout.php" class="w3-bar-item w3-button"><i class="fas fa-sign-in-alt"></i></a>
            <?php
          }
          else{
            header( "refresh:3;url=index.php" );
          }
            ?>
      </div>
    </div>
    <hr>
    <div class="w3-container">
      <h5>Dashboard</h5>
    </div>
    <div class="w3-bar-block">
      <a href="#" class="w3-bar-item w3-button w3-padding-16 w3-hide-large w3-dark-grey w3-hover-black" onclick="w3_close()" title="close menu"><i class="fa fa-remove fa-fw"></i>  Close Menu</a>
      <a href="index.php" class="w3-bar-item w3-button w3-padding"><i class="fas fa-home"></i>  Home</a>
      <?php
        if(isset($_SESSION["username"])){
          ?>
          <a href="statistiche.php" class="w3-bar-item w3-button w3-padding"><i class="fas fa-chart-area"></i>  Statistiche</a>
          <a href="magazzino.php" class="w3-bar-item w3-button w3-padding w3-blue"><i class="fas fa-warehouse"></i>  Magazzino</a>
          <a href="contabilita.php" class="w3-bar-item w3-button w3-padding"><i class="fas fa-file-invoice-dollar"></i>  Contabilità</a>
          <a href="fornitori.php" class="w3-bar-item w3-button w3-padding"><i class="fas fa-parachute-box"></i>  Fornitori</a>
          <a href="clienti.php" class="w3-bar-item w3-button w3-padding"><i class="fas fa-users"></i>  Clienti</a>
          <a href="dipendenti.php" class="w3-bar-item w3-button w3-padding"><i class="fas fa-people-carry"></i>  Dipendenti</a>
          <a href="storico.php" class="w3-bar-item w3-button w3-padding"><i class="fas fa-history"></i>  Storico</a>
          <a href="impostazioni.php" class="w3-bar-item w3-button w3-padding"><i class="fa fa-cog fa-fw"></i>  Impostazioni</a><br><br>
          <?php
        }
        else{
        }
       ?>
    </div>
  </nav>
  <!-- Overlay effect when opening sidebar on small screens -->
  <div class="w3-overlay w3-hide-large w3-animate-opacity" onclick="w3_close()" style="cursor:pointer" title="close side menu" id="myOverlay"></div>
  <!-- !PAGE CONTENT! -->
  <div class="w3-main" style="margin-left:300px;margin-top:43px;">
    <div class="w3-container">
      <div class="w3-row">
        <div class="w3-col s12 l3">
          <br>
        </div>
        <div class="w3-col s12 l5">
          <form class="w3-container" action="nuovoarticolo.php" method="post">

            <label class="w3-text-blue"><b>Descrizione articolo:</b></label>
            <input class="w3-input w3-border" name="nomearticolo" type="text" required><br>

            <label class="w3-text-blue"><b>Codice articolo:</b></label>
            <input class="w3-input w3-border" name="codicearticolo" type="text" required><br>

            <label class="w3-text-blue"><b>Barcode:</b></label>
            <input class="w3-input w3-border" name="barcode" type="text" required><br>

            <label class="w3-text-blue"><b>Prezzo acquisto:</b></label>
            <input class="w3-input w3-border" name="prezzoAcquisto" type="text" required><br>

            <label class="w3-text-blue"><b>Prezzo vendita:</b></label>
            <input class="w3-input w3-border" name="prezzoVendita" type="text" required><br>

            <label class="w3-text-blue"><b>Quantità:</b></label>
            <input class="w3-input w3-border" name="quantita" type="text" required><br>

            <label class="w3-text-blue"><b>Sconto %:</b></label>
            <input class="w3-input w3-border" name="scontoPercentuale" type="text" required><br>

            <button class="w3-btn w3-blue" type="submit">CREA ARTICOLO</button>
          </form>

          <?php
          if(isset($_POST["nomearticolo"]) && isset($_POST["codicearticolo"]) && isset($_POST["barcode"])
          && isset($_POST["prezzoAcquisto"]) && isset($_POST["prezzoVendita"]) && isset($_POST["quantita"])
          && isset($_POST["scontoPercentuale"])){
            $nomeArticolo = $_POST["nomearticolo"];
            $codiceArticolo = $_POST["codicearticolo"];
            $barcodeArticolo = $_POST["barcode"];
            $prezzoAcquistoArticolo = $_POST["prezzoAcquisto"];
            $prezzoVenditaArticolo = $_POST["prezzoVendita"];
            $quantitaArticolo = $_POST["quantita"];
            $scontoPercentualeArticolo = $_POST["scontoPercentuale"];

            creaNuovoArticolo($nomeArticolo, $codiceArticolo, $barcodeArticolo, $prezzoAcquistoArticolo, $prezzoVenditaArticolo, $quantitaArticolo, $scontoPercentualeArticolo);

          }

           ?>
        </div>
        <div class="w3-col s12 l3">
          <br>
        </div>
      </div>








    </div>
  </div>
















  <script>
  // Get the Sidebar
  var mySidebar = document.getElementById("mySidebar");

  // Get the DIV with overlay effect
  var overlayBg = document.getElementById("myOverlay");

  // Toggle between showing and hiding the sidebar, and add overlay effect
  function w3_open() {
      if (mySidebar.style.display === 'block') {
          mySidebar.style.display = 'none';
          overlayBg.style.display = "none";
      } else {
          mySidebar.style.display = 'block';
          overlayBg.style.display = "block";
      }
  }

  // Close the sidebar with the close button
  function w3_close() {
      mySidebar.style.display = "none";
      overlayBg.style.display = "none";
  }
  </script>

  </body>
  </html>
<?php
}
else{
    header("location: accessonegato.php");
}
?>

and here is my funcion code on onother file:

function creaNuovoArticolo($nomeArticolo, $codiceArticolo, $barcodeArticolo, $prezzoAcquistoArticolo, $prezzoVenditaArticolo, $quantitaArticolo, $scontoPercentualeArticolo){
  require('dbconn.php');
  $sql = "INSERT INTO articoli (nomeArticolo, codiceArticolo, barcode, prezzoAcquisto, prezzoVendita, quantita, scontoPercentuale) VALUES('$nomeArticolo', '$codiceArticolo', '$barcodeArticolo', '$prezzoAcquistoArticolo', '$prezzoVenditaArticolo', '$quantitaArticolo', '$scontoPercentualeArticolo')";
  if ($conn->query($sql) === TRUE) {
    header('Location: articolocreato.php');
  } else {
    echo "Error: " . $sql . "<br>" . $conn->error;
  }
}

when I insert a record in my form and click the button, I get this error:

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\bk2\nuovoarticolo.php:69) in C:\xampp\htdocs\bk2\includes\dbfunction.php on line 42

I don't understand how to solve this problem. if someone can check my code and show me my error I appreciate.

Sorry for my bad English! ;)

Aram Grigoryan
  • 740
  • 1
  • 6
  • 24
  • 1
    Please do not create SQL queries by joining strings together. You should always use paramterised queries, otherwise you have a significant security risk from SQL injection. – Dragonthoughts Nov 09 '18 at 09:11
  • The error is clear you send the header 2 times. First when you use dbfunction.php and then in your nuovoarticolo.php. – Sfili_81 Nov 09 '18 at 09:14
  • You send header('Location: articolocreato.php'); after sending html-lines to the client server. Outcomment and it will work. Do the relocation somewhere else. – ivion Nov 09 '18 at 09:14
  • To help with this, we really need to know where line 42 of your file dbFunction.php is. My guess is that it is this line ` header( "refresh:3;url=index.php" );`. – Dragonthoughts Nov 09 '18 at 09:14
  • the line 42 in my dbfunction.php is this one: header('Location: articolocreato.php'); – BIKAPPA2 A Vaccari T Sossi Nov 09 '18 at 09:50

1 Answers1

1

Your question is closely related to How to fix "Headers already sent" error in PHP.

Your error occurs when you have any output occurring before a call to header().

This output could be HTML mark-up or other output from your PHP, it could even be a blank space at the start of your php file or a Byte Order Mark that is added by your code editor.

You need to move all of the logic relating to headers to the start of your code or use PHP output buffering - see What is output buffering? for buffering discussions.

Dragonthoughts
  • 2,180
  • 8
  • 25
  • 28