更新和插入查询在php中不起作用
我现在被卡住了,因为我的更新和插入查询不工作,选择查询工作得很好 下面是我的代码更新和插入查询在php中不起作用,php,mysql,Php,Mysql,我现在被卡住了,因为我的更新和插入查询不工作,选择查询工作得很好 下面是我的代码 <?php if(isset($_POST['save'])){ $exam_type=$_POST['exam_type']; $exam_from=$_POST['exam_from']; $exam_to=$_POST['exam_to']; $total_points=$_POST['total_points'];
<?php
if(isset($_POST['save'])){
$exam_type=$_POST['exam_type'];
$exam_from=$_POST['exam_from'];
$exam_to=$_POST['exam_to'];
$total_points=$_POST['total_points'];
$passing_grade=$_POST['passing_grade'];
$time_limit=$_POST['hrs']*360 + $_POST['min']*60;
$cid=$_GET['cid'];
$sql = $con->prepare("UPDATE new_oes.exam"
. " SET exam_type=?,exam_from =?, exam_to=?,"
. "modified_by=?,passing_score=?, time_limit=?,passing_grade=? "
. "WHERE exam_type = ? AND cid =? ");
$sql->execute(array($exam_type, $exam_from, $exam_to, $username,
$total_points, $time_limit, $passing_grade, $exam_type,$cid)) or die(mysqli_error($con));
// echo $result1;
// $this->query($sql);
if(mysqli_affected_rows($con)>0){
echo "<script type='text/javascript'>alert('Exam Updated!!!')</script>";
exit;
} else{
echo "An error occurred, try again later!!!";
}
}
?>
似乎$username
没有定义,您应该定义$username
$username = "SOME_ONE"; // maybe you saved it in $_SESSION or its in $_POST
通常,在尝试执行查询之前,您需要检查尝试准备
sql语句的返回值。我不确定执行调用是如何执行的,因为它将数组指定为参数,这在PDO中更为常见-我认为您应该在执行之前将参数绑定到占位符,如下所示:
<?php
try{
if( isset(
$_POST['save'],
$_POST['exam_type'],
$_POST['exam_from'],
$_POST['exam_to'],
$_POST['total_points'],
$_POST['passing_grade'],
$_POST['hrs'],
$_GET['cid'],
$_GET['username'])
){
$exam_type=$_POST['exam_type'];
$exam_from=$_POST['exam_from'];
$exam_to=$_POST['exam_to'];
$total_points=$_POST['total_points'];
$passing_grade=$_POST['passing_grade'];
$time_limit=$_POST['hrs']*360 + $_POST['min']*60;
$cid=$_GET['cid'];
$username=$_GET['username'];
$stmt = $con->prepare("update `new_oes`.`exam`
set `exam_type`=?, `exam_from` =?, `exam_to`=?, `modified_by`=?, `passing_score`=?, `time_limit`=?, `passing_grade`=?
where `exam_type` = ? and `cid` =? ");
if( $stmt ){
/*
Assumed to be that all parameters are strings apart from $cid which I took to be an integer
Change each letter in first parameter of `bind_param` accoring to whether the input variable is
either string or numeric.
s=string
i=integer
*/
$stmt->bind_param( 'ssssssssi', $exam_type, $exam_from, $exam_to, $username, $total_points, $time_limit, $passing_grade, $exam_type, $cid );
$stmt->execute();
if( $stmt->affected_rows > 0 ){
exit( "<script>alert('Exam Updated!!!')</script>" );
} else{
throw new Exception('An error occurred, try again later!!!');
}
} else {
throw new Exception('Failed to prepare SQL statement!');
}
} else {
throw new Exception('Required variables are not yet set');
}
}catch( Exception $e ){
echo $e->getMessage();
}
?>
这是mysqli还是PDO?哪里设置了$username?@NigelRen$username来自会话,设置在该代码之前的某个位置。建议也将其设置到if语句中?此处没有插入提示!终于成功了!。我的html表单有问题。再次感谢