我在网站上使用CRUD系统。 我显示一个包含所有现有记录的表(avisos.php)。 当用户单击ADD NEW RECORD按钮时,将启动以下脚本:
$("#btn-add").click(function(){
$(".content-loader").fadeOut('slow', function()
{
$(".content-loader").fadeIn('slow');
$(".content-loader").load('nuevo_aviso.php');
$("#btn-add").hide();
$("#btn-view").show();
});
});
nuevo_aviso.php包含一个用于添加新记录的HTML表单。这是它的内容:
<div id="dis">
<!-- here message will be displayed -->
</div>
<form method='post' id='emp-SaveForm' action="#">
<table class='table table-bordered'>
<tr>
<td>Titulo</td>
<td><input type='text' name='titulo_anuncio' class='form-control' placeholder='EX : P.M.I. DUCTO DE JUAREZ' required /></td>
</tr>
<tr>
<td>Texto</td>
<td><input type='text' name='texto_anuncio' class='form-control' placeholder='EX : Mantenimiento preventivo el dia 17 de agosto' required></td>
</tr>
<tr>
<td colspan="2">
<button type="submit" class="btn btn-primary" name="btn-save" id="btn-save">
<span class="glyphicon glyphicon-plus"></span> Guardar Aviso/Anuncio
</button>
</td>
</tr>
</table>
</form>
从这里调用avisos.php的JS文件(crud.js)应该将表单数据传递给create_aviso.php: 这是crud.js中的函数:
/* Data Insert Starts Here */
$(document).on('submit', '#emp-SaveForm', function() {
$.post("create_aviso.php", $(this).serialize())
.done(function(data){
$("#dis").fadeOut();
$("#dis").fadeIn('slow', function(){
$("#dis").html('<div class="alert alert-info">'+data+'</div>');
$("#emp-SaveForm")[0].reset();
});
});
return false;
});
然后,在create_aviso.php中,应将接收到的数据插入数据库中:
<?php
require_once 'dbconfig.php';
if($_POST)
{
$titulo_anuncio = $_POST['titulo_anuncio'];
$texto_anuncio = $_POST['texto_anuncio'];
try{
$stmt = $db_con->prepare("INSERT INTO tbAnuncios(titulo_anuncio,texto_anuncio) VALUES(:etitulo, :etexto)");
$stmt->bindParam(":etitulo", $titulo_anuncio);
$stmt->bindParam(":etexto", $texto_anuncio);
if($stmt->execute())
{
echo "Registro añadido";
}
else{
echo "Error";
}
}
catch(PDOException $e){
echo $e->getMessage();
}
}
?>
问题是,在create_aviso.php的第12行,浏览器会抛出错误:
致命错误:在第12行的/../create_aviso.php中调用null上的成员函数prepare()。这是第12行:
$stmt = $db_con->prepare("INSERT INTO tbAnuncios(titulo_anuncio,texto_anuncio) VALUES(:etitulo, :etexto)");
我检查了数据,发现只有$_POST['titulo_anuncio']
的值会被赋予create_aviso.php
。
未收到$_POST['texto_anuncio']
。这应该是异常的原因,但我没有在代码中找到错误。
答案 0 :(得分:1)
dbconfig.php
的内容遗失了,但我认为您的问题是因为$db_con
必须在12
行中访问。
在您的情况下,$db_con
可能未定义,或者它的范围不同。
尝试使用
global $db_con;
11
create_aviso.php
行