gpt4 book ai didi

php - 插入前如何查询数据库

转载 作者:行者123 更新时间:2023-11-30 22:35:00 25 4
gpt4 key购买 nike

我正在尝试查询数据库以检查当前计划的标记在插入之前是否已经在数据库中更新,我的代码如下:

    <?php 
include 'config.php';
if(isset($_POST['submit'])){
$schedule = $_POST['hischedule'];
$array = $_POST['marks'];
//Qry to check Duplicate values Againest Database
$marksupdqry = "select distinct scheduleName from marks";
$mks = mysqli_query($link, $marksupdqry);
while($mksresult = mysqli_fetch_array($mks))
foreach($mksresult as $sdlnme)
if($sdlnme = $schedule){
echo "<script> alert('Marks for this schedule already exist in db');</script>";
echo "<script> window.location.href='marks.php';</script>";
}else{
foreach ($array as $reg_num=>$score)
{
$ins = "INSERT INTO marks (sl_no, scheduleName, obtainedMarks) VALUES ('$reg_num', '$schedule', '$score')";
$res = mysqli_query($link, $ins);
if($res){
//echo "Success";
$Message = urlencode("Marks updated successfully ");
header("Location:marks.php?Message=".$Message);
die;
}
}
}
}
?>

我的问题是我无法同时执行代码的 if 和 else 部分。

最佳答案

像下面这样修改你的代码:

<?php 
include 'config.php';
if(isset($_POST['submit'])){
$schedule = $_POST['hischedule'];
$array = $_POST['marks'];
$marksupdqry = "select distinct scheduleName from marks";
$mks = mysqli_query($link, $marksupdqry);
while($mksresult = mysqli_fetch_array($mks)){
foreach($mksresult as $sdlnme){
if($sdlnme == $schedule){
echo "<script> alert('Marks for this schedule already exist in db');</script>";
echo "<script> window.location.href='marks.php';</script>";
}else{
foreach ($array as $reg_num=>$score)
{
$ins = "INSERT INTO marks (sl_no, scheduleName, obtainedMarks) VALUES ('$reg_num', '$schedule', '$score')";
$res = mysqli_query($link, $ins);
if($res){
//echo "Success";
$Message = urlencode("Marks updated successfully ");
header("Location:marks.php?Message=".$Message);
die;
}
}
}
}
}
}
?>

关于php - 插入前如何查询数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32823026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com