大家好,我想向其他有帮助的程序员求助! 在我的代码中显示
查询为空
这是我的 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>
这是我的 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);
}
?>
请大家帮帮我!我正在使用mysqli程序,我认为我有一点错误,因为我无法将变量提交给我的查询
答案 0 :(得分:3)
你的问题很简单。出于某种奇怪的原因,您试图执行两次查询。
要避免此错误,您只需要保留mysqli_query调用。但不管怎样,它会使你的代码容易出现SQL注入。
更好的是,使用准备好的陈述
$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);
从mysqli_error部分开始,请记住you're not the only user of your site并且不要立即回复错误。
答案 1 :(得分:1)
从此
转向您的查询$query = mysqli_query($conn,"INSERT INTO my_hobbies (hobbies) VALUES ('$values')");
$query = mysqli_query($conn,"INSERT INTO test (hobbies) VALUES ('$name')");
到这个
$query = "INSERT INTO my_hobbies (hobbies) VALUES ('$values');";
$query .= "INSERT INTO test (name) VALUES ('$name');";
因为你在mysqli程序中吗? Insert Multiple in Mysqli