-1

Hi guys I would like to ask help to other helpful programmer out there! In my code it displays the

Query was empty

this is my index.php

<!DOCTYPE html>
<html>
<head>
    <title>Add Input File Dynamically</title>
</head>
<script src="admin/Bootstrap/js/jquery.min.js"></script>
<script src="admin/Bootstrap/js/bootstrap.min.js"></script>
<script src="admin/Bootstrap/js/npm.js"></script>
<body>

    <form method="post" action="collect_vals.php">
    <div class="input_fields_wrap">
        <button class="add_field_button">Add More Fields</button>
        <input type="submit" name="submit_val" value="Submit" />
        <div><input id="field_1" type="text" name="mytext[]"></div>
    </div>
    Name:<input type="text" name="name" />
    </form>

<script>

    $(document).ready(function() {
    var max_fields      = 10; //maximum input boxes allowed
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_field_button"); //Add button ID

    var x = 1; //initlal text box count
    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++; //text box increment
            $(wrapper).append('<div>'+ x +' <input id="field_'+ x +'" type="text" name="mytext[]"/><a href="#" class="remove_field">Remove</a></div>'); //add input box
        }
    });

    $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault(); $(this).parent('div').remove(); x--;
    })
});

</script>
</body>
</html>

and this is my collect_vals.php

<?php
    require('dbcon.php');
    if (isset($_POST['submit_val'])) {

        if ($_POST['mytext']) {
        foreach ( $_POST['mytext'] as $key=>$value ) {
        $values = mysqli_real_escape_string($conn, $value);
             }
        }

$name = $_POST['name'];

    $query = mysqli_query($conn,"INSERT INTO my_hobbies (hobbies) VALUES ('$values')");
    $query = mysqli_query($conn,"INSERT INTO test (hobbies) VALUES ('$name')");

    if (mysqli_multi_query($conn, $query)) {
        echo "New records created successfully";
        echo "<i><h2><strong>" . count($_POST['mytext']) . "</strong> Hobbies Added</h2></i>";
    } else {
        echo "Error: " . $query . "<br>" . mysqli_error($conn);
    }
mysqli_close($conn);
}
?>

Please guys help me! I'm using mysqli procedural and I think I have a little mistake because I can't submit the variables to my query

lokis
  • 67
  • 8

2 Answers2

4

Your problem is simple. For some strange reason you are trying to execute your queries twice.

To avoid this error, you would need to keep only mysqli_query calls. But either way, it would make your code prone to SQL injection.

better yet, use prepared statements

$query = $conn->prepare("INSERT INTO my_hobbies (hobbies) VALUES (?)");
$query->bind_param("s", $values);
$query->execute();
$query = $conn->prepare("INSERT INTO test (hobbies) VALUES (?)");
$query->bind_param("s", $name);
$query->execute();
mysqli_close($conn);

As of mysqli_error part, remember you're not the only user of your site and never echo errors right away.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
0

turn your query from this

$query = mysqli_query($conn,"INSERT INTO my_hobbies (hobbies) VALUES ('$values')");
$query = mysqli_query($conn,"INSERT INTO test (hobbies) VALUES ('$name')");

to this

$query = "INSERT INTO my_hobbies (hobbies) VALUES ('$values');";
$query .= "INSERT INTO test (name) VALUES ('$name');";

because you are in mysqli procedural right? Insert Multiple in Mysqli

LecheDeCrema
  • 392
  • 5
  • 21