-1

Warning: implode() [function.implode]: Invalid arguments passed in C:\xampp\htdocs\tempahperalatan\Page2.php on line 7

Warning: mysqli_error() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\tempahperalatan\Page2.php on line 23

i am doing a form to insert data from multiple checkboxes, the data that i submit are inserted perfectly, however the above two errors appear once i open the page(page2.php) can anyone tell me what have i missed in my coding or where is the problem? thank you in advance.

for reference below is my php coding:

<?php

// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("tempahperalatan") or die(mysql_error());

    $checkBox = implode(',', $_POST['item']);
    $microphones = $_POST['microphones'];
    $amplifiers = $_POST['amplifiers'];
    $loudspeakers = $_POST['loudspeakers'];
    $mixers = $_POST['mixers'];
    $catatan = $_POST['catatan'];   

if(isset($_POST['submit']))
{       
    $query="INSERT INTO pasystems (item, microphones, amplifiers, loudspeakers, mixers, catatan) VALUES ('" . $checkBox . "', '$microphones', '$amplifiers', '$loudspeakers', '$mixers', '$catatan')";     

    mysql_query($query) or die (mysql_error() );

    echo "<script type='text/javascript'>alert('Submitted successfully!')</script>";
}
    else{
    echo "<script type='text/javascript'>alert('Failed!')</script>" . $sql . "<br>" . mysqli_error($conn);  
    }
?>

and below is my form:

<form action="page2.php" method="POST">


        <div class="form-group row text-left">
          <label for="example-date-input" class="col-2 col-form-label">Nama Peralatan: </label>
          <div class="col-10">

            <div class="form-group">
              <div class="form-row">
                <div class="col-md-2">
                        <div class="form-check text-left">
                            <label class="form-check-label">
                                <input class="form-check-input" name="item[]" type="checkbox" value="Microphones">
                                Microphones
                            </label>
                        </div>
                </div>
                <div class="">
                    <input class="form-control" type="number" value="0" name="microphones" id="example-number-input">                               
                </div>                                  
              </div>
            </div>

            <div class="form-group">
              <div class="form-row">
                <div class="col-md-2">
                        <div class="form-check text-left">
                            <label class="form-check-label">
                                <input class="form-check-input" name="item[]" type="checkbox" value="Amplifiers">
                                Amplifiers
                            </label>
                        </div>
                </div>
                <div class="">
                    <input class="form-control" type="number" value="0" name="amplifiers" id="example-number-input">    
                </div>
              </div>
            </div>

            <div class="form-group">
              <div class="form-row">
                <div class="col-md-2">
                        <div class="form-check text-left">
                            <label class="form-check-label">
                                <input class="form-check-input" name="item[]" type="checkbox" value="Loudspeakers">
                                Loudspeakers
                            </label>
                        </div>
                </div>
                <div class="">
                    <input class="form-control" type="number" value="0" name="loudspeakers" id="example-number-input">  
                </div>
              </div>
            </div>  

            <div class="form-group">
              <div class="form-row">
                <div class="col-md-2">
                        <div class="form-check text-left">
                            <label class="form-check-label">
                                <input class="form-check-input" name="item[]" type="checkbox" value="Mixers">
                                Mixers
                            </label>
                        </div>
                </div>
                <div class="">
                    <input class="form-control" type="number" value="0" name="mixers" id="example-number-input">    
                </div>
              </div>
            </div>                          

          </div>
        </div>                          

    <div class="form-group row text-left">
    <label for="exampleTextarea" class="col-2 col-form-label">Catatan: </label>
        <div class="col-10">
        <textarea class="form-control" name="catatan" id="exampleTextarea" rows="3"></textarea>
        </div>
    </div>

    <center><button type="submit" name="submit" class="btn btn-info">Submit</button></center>

</form> 
user8674032
  • 51
  • 3
  • 9

1 Answers1

5

The first error occurs because you don't check for the existence of $_POST['item'] before attempting to use it. You need to move these lines inside your if block or, even better, check for each element in $_POST that you want to use:

$checkBox = implode(',', $_POST['item']);
$microphones = $_POST['microphones'];
$amplifiers = $_POST['amplifiers'];
$loudspeakers = $_POST['loudspeakers'];
$mixers = $_POST['mixers'];
$catatan = $_POST['catatan'];

The second error is because you are mixing mysql_... functions with mysqli_... functions. You should not be using mysql_... functions at all. The mysql_* functions are outdated, deprecated, and insecure - they have been removed entirely from modern versions of PHP (version 7.0 and higher). Use MySQLi or PDO instead.

elixenide
  • 44,308
  • 16
  • 74
  • 100