如何根据数据库中是否存在值来插入或更新

时间:2017-10-27 18:23:31

标签: php mysql

我试图检查Multiple_Choices表中是否存在值学校和课程。如果它们存在则更新查询为星标,如果不存在则为查询。这个php文件的目的是插入mysql数据库问题以进行多项选择测验或更新已有的测验。它发布了id,问题,5个可能的答案,用户帮助,学校名称和课程名称(学校课程)。

这是我的PHP代码(我也尝试过get方法):

<?php
 require('connect.php');

 $id = $_POST['id'];
 $erwtisi = $_POST['erwtisi'];
 $apantisi1 = $_POST['apantisi1'];
 $apantisi2 = $_POST['apantisi2'];
 $apantisi3 = $_POST['apantisi3'];
 $apantisi4 = $_POST['apantisi4'];
 $swstiapantisi = $_POST['swstiapantisi'];
 $voithia = $_POST['voithia'];
 $school = $_POST['school'];
 $lesson = $_POST['lesson'];


 function slugify($text){
    // replace non letter or digits by -
    $text = preg_replace('~[^\pL\d]+~u', '-', $text);
    return $text;
}
 $school=slugify($school);
 $lesson=slugify($lesson);


 $sql = "SELECT * FROM Multiple_Choices WHERE school = '$school' AND lesson = '$lesson'";
 $result = mysqli_query($conexao, $sql) or die ("Error: ") . mysqli_error($sql);
 $count = mysqli_num_rows($result);

 if ($count == 0) 
 {
    echo "insert";
    $sql3 = "INSERT INTO Multiple_Choices (id, Answer1, Answer2, Answer3,Answer4, Answer5, school, lesson) VALUES ('$id', '$apantisi1', '$apantisi2', '$apantisi3', '$apantisi4', '$swstiapantisi', '$school', '$lesson')";
    $sql4 = "INSERT INTO Questions (id, erwtiseis, apantiseis, help, school, lesson) VALUES ('$id', '$erwtisi', '$swstiapantisi', '$voithia', '$school', '$lesson')";
    $result3 = mysqli_query($conexao, $sql3) or die ("Error3: ") . mysqli_error($sql3);
    $result4 = mysqli_query($conexao, $sql4) or die ("Error4: ") . mysqli_error($sql4);      
    $count3 = mysqli_num_rows($result3);
    $count4 = mysqli_num_rows($result4);

    if (($count3 == 0) AND ($count4 == 0))
    {
        echo "error1";
    }
    else
    {
        echo "ok1";
    }
 }
 else
 {
    echo "update";
    $sql1 = "UPDATE Multiple_Choices SET Answer1 = '$apantisi1', Answer2 = '$apantisi2', Answer3 = '$apantisi3', Answer4 = '$apantisi4', Answer5 = '$swstiapantisi' WHERE school = '$school' AND id = '$id' AND lesson = '$lesson'";
    $sql2 = "UPDATE Questions SET erwtiseis = '$erwtisi', apantiseis = '$swstiapantisi', help = '$voithia' WHERE school = '$school' AND id = '$id' AND lesson = '$lesson'";
    $result1 = mysqli_query($conexao, $sql1) or die ("Error1: ") . mysqli_error($sql1);
    $result2 = mysqli_query($conexao, $sql2) or die ("Error2: ") . mysqli_error($sql2);
    $count1 = mysqli_num_rows($result1);
    $count2 = mysqli_num_rows($result2);

    if (($count1 == 0) AND ($count2 == 0))
    {
        echo "error2";
    } 
    else
    {
        echo "ok2";
    }
 }

?>

1 个答案:

答案 0 :(得分:0)

好的,我发现了自己的错误。我将代码更改为此,它的工作原理!

<?php
 require('connect.php');

 $id = $_POST['id'];
 $erwtisi = $_POST['erwtisi'];
 $apantisi1 = $_POST['apantisi1'];
 $apantisi2 = $_POST['apantisi2'];
 $apantisi3 = $_POST['apantisi3'];
 $apantisi4 = $_POST['apantisi4'];
 $swstiapantisi = $_POST['swstiapantisi'];
 $voithia = $_POST['voithia'];
 $school = $_POST['school'];
 $lesson = $_POST['lesson'];

 function slugify($text){
    // replace non letter or digits by -
    $text = preg_replace('/\s+/u', '_', $text);
    return $text;
 }
 $school=slugify($school);
 $lesson=slugify($lesson);

 $sql1 = "UPDATE Multiple_Choices SET Answer1 = '$apantisi1', Answer2 = '$apantisi2', Answer3 = '$apantisi3', Answer4 = '$apantisi4', Answer5 = '$swstiapantisi' WHERE school = '$school' AND id = '$id' AND lesson = '$lesson'";
 $sql2 = "UPDATE Questions SET erwtiseis = '$erwtisi', apantiseis = '$swstiapantisi', help = '$voithia' WHERE school = '$school' AND id = '$id' AND lesson = '$lesson'";
 $result1 = mysqli_query($conexao, $sql1) or die ("Error1: ") . mysqli_error($sql1);
 $result2 = mysqli_query($conexao, $sql2) or die ("Error2: ") . mysqli_error($sql2);

    if (mysqli_affected_rows($conexao)==0)
    {
        echo "error1";
        $sql3 = "INSERT INTO Multiple_Choices (id, Answer1, Answer2, Answer3, Answer4, Answer5, school, lesson) VALUES ('$id', '$apantisi1', '$apantisi2', '$apantisi3', '$apantisi4', '$swstiapantisi', '$school', '$lesson')";
        $sql4 = "INSERT INTO Questions (id, erwtiseis, apantiseis, help, school, lesson) VALUES ('$id', '$erwtisi', '$swstiapantisi', '$voithia', '$school', '$lesson')";
        $result3 = mysqli_query($conexao, $sql3) or die ("Error3: ") . mysqli_error($sql3);
        $result4 = mysqli_query($conexao, $sql4) or die ("Error4: ") . mysqli_error($sql4);

        if (mysqli_affected_rows($conexao)==0)
        {
            echo "error2";
        }
        else
        {
            echo "ok2";
        }
    }
    else
    {
        echo "ok1";
    }



?>