我有这样的表格:
<select id="mudar_produto">
<option></option>
<option value="#produto_1">Novo Produto Higiene</option>
</select>
<section class="hide-section" id="produto_1">
<form class="form-validate" id="feedback_form">
<div class="campo">
<fieldset>
<h1>
<legend>
<center>
<strong>Produtos de Higiene</strong>
</center>
</h1><br>
</div>
<fieldset class="grupo">
<div class="campo">
<strong><label for="Nome do Produto">Nome do Produto</label></strong>
<input type="text" id="DescricaoProd" name="DescricaoProd" required="" style="width:350px">
</div>
<div class="campo">
<strong><label for="Unidade">Unidade</label></strong>
<input type="text" id="DescricaoUnid" name="DescricaoUnid" style="width:160px" required="" size="120">
</div>
</fieldset>
<button class="btn btn-success btn_contact" type="button">Registo</button>
</form>
</section>
我有这个ajax来发送数据而不刷新页面并清理输入:
<script type="text/javascript">
$(".btn_contact").click(function () {
$.ajax({
type: "POST",
url: "inserir",
data: $("#feedback_form").serialize(), // serializes the form's elements.
success: function (data)
{
if ($.trim(data) == 'true') {
$("#feedback_form").find('input').val(''); //clear text
$(".success_messages").removeClass('hide'); // success message
} else {
$(".error_message").removeClass('hide'); // error message
}
}
});
});
</script>
在插入页面上,我有php代码:
<?php
$servername = "xxx.xxx.x.xx";
$username = "xxxxxx";
$password = "xxxxxxxxx";
$dbname = "xxxxxxxx";
$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset('utf8');
$name = isset($_POST["DescricaoProd"]) ? $_POST["DescricaoProd"] : '';
$unid = isset($_POST["DescricaoUnid"]) ? $_POST["DescricaoUnid"] : '';
if (!empty($name)) && (!empty($unid)) {
echo 'true';
} else {
echo 'false';
}
$sql = "INSERT INTO ProdHigieneteste (DescricaoProd,DescricaoUnid)
VALUES ('$name','$unid')";
if ($conn->query($sql) === TRUE);
$sql1 = "INSERT INTO StockHigieneteste (DescricaoProd,DescricaoUnid)
VALUES ('$name','$unid')";
if ($conn->query($sql1) === TRUE);
//Count total number of rows
$rowCount = $query->num_rows;
$conn->close();
?>
但是当我单击注册按钮时,我在控制台中收到以下错误:
POST http://xxx.xxx.x.xx/wordpress/index.php/inserir 500(内部 服务器错误)
并且不会插入到数据库表中。
除了@Jeff识别出的语法错误之外的错误:
if (!empty ($name)) && (!empty ($unid)) {
我不得不切换到:
if (!empty ($name) && !empty ($unid)) {
答案 0 :(得分:1)
此处至少有2个语法错误
if ($conn->query($sql) === TRUE);
然后
if ($conn->query($sql1) === TRUE);
所以将它们改为
if ($conn->query($sql)) { // check for === TRUE is not necessary
// either put the second query in here, or just enjoy the success
} else {
// get the error, throw a message...
}
$sql1 = "INSERT INTO StockHigieneteste (DescricaoProd,DescricaoUnid)
VALUES ('$name','$unid')";
if ($conn->query($sql1)) {
//Count total number of rows
$rowCount = $query->num_rows;
} else {
// get the error, throw a message...
}
$conn->close(); // put that OUTSIDE the condition
除此之外:
目前还不清楚是否在某个地方设置了$ conn
编辑:在新代码中显示。更好的方法是包含一个执行数据库连接的单独文件(因为您可能更需要它)
您可以使用sql注入attacs !
使用prepared statements。
这可能不会出错,这只是显而易见的事情。