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