PHP Update查询没有错误,但无法继续下一页

时间:2019-03-07 05:36:43

标签: php

我需要有关查询的帮助,没有错误等信息,但是它无法进入下一页,请参见下面的查询:

<?php
$connect=mysqli_connect('localhost','root','','lawadmission');

session_start();

            $reference_number = $_SESSION['reference_number'];
            $citizenship = $_POST['citizenship'];
            $region = $_POST['region'];
            $spouse_name = $_POST['spouse_name'];
            $place_of_birth = $_POST['place_of_birth'];
            $civil_status = $_POST['civil_status'];
            $no_of_children = $_POST['no_of_children'];
            $weight = $_POST['weight'];
            $height = $_POST['height'];
            $degree = $_POST['degree'];
            $school = $_POST['school'];
            $yearGraduated = $_POST['yearGraduated'];
            $elementary = $_POST['elementary'];
            $elementaryDegreeObtained = $_POST['elementaryDegreeObtained'];
            $elementaryPeriodOfAttendance = $_POST['elementaryPeriodOfAttendance'];
            $highschool = $_POST['highschool'];
            $highschoolDegreeObtained = $_POST['highschoolDegreeObtained'];
            $highschoolPeriodOfAttendance = $_POST['highschoolPeriodOfAttendance'];
            $college = $_POST['college'];
            $collegeDegreeObtained = $_POST['collegeDegreeObtained'];
            $collegePeriodOfAttendance = $_POST['collegePeriodOfAttendance'];
            $postCollege = $_POST['postCollege'];
            $postcollegeDegreeObtained = $_POST['postcollegeDegreeObtained'];
            $postcollegePeriodOfAttendance = $_POST['postcollegePeriodOfAttendance'];
            $other = $_POST['other'];
            $otherDegreeObtained = $_POST['otherDegreeObtained'];
            $otherPeriodOfAttendance = $_POST['otherPeriodOfAttendance'];

            $query = "UPDATE applicants SET 
            citizenship = '$citizenship', 
            region = '$region',
            spouseName = '$spouse_name',
            placeOfBirth = '$place_of_birth',
            civilStatus = '$civil_status',
            childNo = '$no_of_children',
            weight = '$weight',
            height = '$height',
            degree = '$degree',
            school = '$school',
            yearGraduated = '$yearGraduated',
            elementary = '$elementary',
            elementaryDegreeObtained = '$elementaryDegreeObtained',
            elementaryPeriodOfAttendance = '$elementaryPeriodOfAttendance',
            highschool = '$highschool',
            highschoolDegreeObtained = '$highschoolDegreeObtained',
            highschoolPeriodOfAttendance = '$highschoolPeriodOfAttendance',
            college = '$college',
            collegeDegreeObtained = '$collegeDegreeObtained',
            collegePeriodOfAttendance = '$collegePeriodOfAttendance',
            postCollege = '$postCollege',
            postcollegeDegreeObtained = '$postcollegeDegreeObtained',
            postcollegePeriodOfAttendance = '$postcollegePeriodOfAttendance',
            other = '$other',
            otherDegreeObtained = '$otherDegreeObtained' and
            otherPeriodOfAttendance = '$otherPeriodOfAttendance' 
            WHERE referenceNo = '$reference_number'";

                if(mysqli_query($connect, $query)){
                    header( "Location: registered.php" ); die;
                    echo "<script>window.open('registered.php','_self')</script>";
                }
                if(mysqli_connect_errno($connect))
                {
                    echo 'Failed to connect';
                }


?>

3 个答案:

答案 0 :(得分:1)

您的错误就在这里:

  UPDATE applicants SET
   ...
  other = '".$other."',
  otherDegreeObtained = '".$otherDegreeObtained."' and  <--- and
  otherPeriodOfAttendance = '$otherPeriodOfAttendance' 

and应该是,

SQLInjection 除此之外,您还可以进行SQL注入。就像我在评论中说的那样,在任何输入中的单个'都会破坏您的查询-通过SQLInjection。

将举这个小例子

//$citizenship = $_POST['citizenship'];
UPDATE applicants SET citizenship = '{$_POST['citizenship']}'

现在,如果$_POST['citizenship']it's或其中包含'的任何东西,这就是您的查询:

UPDATE applicants SET citizenship = 'it's'

现在s'将不匹配,因此这将成为SQL中的语法错误,并且您将回到原来的位置。那是最好的情况。可以做的一件事是(不要尝试此操作)

//don't do this
$_POST['otherPeriodOfAttendance'] = "' WHERE 1 --";
UPDATE applicants SET ... , otherPeriodOfAttendance='' WHERE 1 --WHERE referenceNo = ''
//OR
 UPDATE applicants SET ... , otherPeriodOfAttendance='' WHERE 1

--是SQL中的注释,因此此后将忽略其余查询。因此,这将更新数据库中的每一行,而不仅仅是更新1,因为1总是正确。实际上,您可能会一起忽略WHERE。因此,只需输入:

  //don't do this either
  $_POST['otherPeriodOfAttendance'] = "'--";
  UPDATE applicants SET ... , otherPeriodOfAttendance=''--WHERE referenceNo = ''
  //OR
  UPDATE applicants SET ... , otherPeriodOfAttendance=''

我基本上可以消灭整个桌子,这显然不是我们想要做的事情。

我建议查找如何在PHP中准备查询。关于该主题的资源很多,因此,在此我将不作详细介绍,除了说显而易见的安全原因外,它还会处理引号。

答案 1 :(得分:0)

如果可以在查询中使用数组,则可以编辑查询,以清楚地读取参数值。另外,请考虑使用var_dumpprint_r进行检查。

$query = "UPDATE applicants SET 
        citizenship = '".$citizenship."', 
        region = '".$region."',
        spouseName = '".$spouse_name."',
        placeOfBirth = '".$place_of_birth."',
        civilStatus = '".$civil_status."',
        childNo = '".$no_of_children."',
        weight = '".$weight."',
        height = '".$height."',
        degree = '".$degree."',
        school = '".$school."',
        yearGraduated = '".$yearGraduated."',
        elementary = '".$elementary."',
        elementaryDegreeObtained = '".$elementaryDegreeObtained."',
        elementaryPeriodOfAttendance = '".$elementaryPeriodOfAttendance."',
        highschool = '".$highschool."',
        highschoolDegreeObtained = '".$highschoolDegreeObtained."',
        highschoolPeriodOfAttendance = '".$highschoolPeriodOfAttendance."',
        college = '".$college."',
        collegeDegreeObtained = '".$collegeDegreeObtained."',
        collegePeriodOfAttendance = '".$collegePeriodOfAttendance."',
        postCollege = '".$postCollege."',
        postcollegeDegreeObtained = '".$postcollegeDegreeObtained."',
        postcollegePeriodOfAttendance = '".$postcollegePeriodOfAttendance."',
        other = '".$other."',
        otherDegreeObtained = '".$otherDegreeObtained."',
        otherPeriodOfAttendance = '".$otherPeriodOfAttendance."' 
        WHERE referenceNo = '".$reference_number."'";

答案 2 :(得分:0)

我的问题是模具的功能。删除模具功能

 if(mysqli_query($connect, $query))
 {
            header( "Location: registered.php" );
            echo "<script>window.open('registered.php','_self')</script>";
 }