首页 > 解决方案 > 修复“错误 500:页面不工作”错误 PHP

问题描述

这是我正在学习的 PHP 课程的作业。我们在 mySQL 中创建了一个数据库,我们正在制作一个网站来查看/插入/更新/删除数据库中的信息。现在,我在运行 if 语句以查看数据库中是否已存在 ID # 时收到“页面不工作”错误。

我已经尝试注释掉我的部分代码以确定问题出在哪里,我很确定这是我的“数据库连接”注释下的代码的问题。这是初学者的课程,我正在按照教授的演练视频进行作业,但我无法弄清楚我做错了什么。

<?php 
session_start(); //starts the session

//Set all variables
$uniqueid = $_POST["uniqueid"];
$breed = $_POST["breed"];
$color = $_POST["color"];
$furlength = $_POST["furlength"];
$dogweight = $_POST["dogweight"];

//test if variables come in correctly
//echo "variables: " . $uniqueid . $breed . $color . $furlength . $dogweight;

//test if all fields filled
if (($uniqueid == "") || ($breed == "") || ($color == "") || ($furlength == "") || ($dogweight == "")) {
    $_SESSION["errormessage"] = "You must complete all fields!"; //error message if any field is empty
    header("Location:insert.php");
    exit;
}
else { //if all fields filled
    $_SESSION["errormessage"] = ""; //no error message
}

//database connections -- THIS IS PROBABLY WHERE THE ISSUE IS
include("includs/openDBConn.php"); //include the database
//check that # adding isn't already part of database
$sql="SELECT UniqueID FROM Dogs WHERE UniqueID=".$uniqueid;
$result=$conn->query($sql);

if($result->$num_rows > 0) { //make sure database loads the rows
    echo("num rows= ".$result->$num_rows); //echo the number of rows
}
else { //if there are no rows
    echo("No data found"); //give error message
}
?>

在另一个页面上,有一些字段供我输入 UniqueID、品种、颜色等。该页面应该检查所有字段是否都已填写(该部分有效),然后检查是否已经有一行带有我输入的 UniqueID。如果有,它应该回显使用该 UniqueID(应该是 1)找到的行数。

我对 PHP 很陌生,所以如果我遗漏了任何重要信息,请告诉我。我很感激任何建议!

标签: phpmysqlsql

解决方案


你的代码充满了漏洞。容易发生sql注入、xss攻击、csrf、html注入。

我已经重新编写了它以避免大多数此类问题。

1.)现在使用准备查询缓解Sql 注入

2.)使用整数变量的 intval 和字符串的 strip_tags 来减轻Html 注入。您可以阅读更多关于 php 中的数据验证和清理的信息,以查看更多可用选项

3.) xss 攻击已通过htmlentities()得到缓解。你也可以使用htmlspecialchars()。阅读有关所有这些内容的更多信息

请参阅下面更好的安全代码

请尽可能放置您的数据库凭据 我不知道您的唯一 ID 是字符串还是整数(数字)

// if UniqueID is integer or number use i parameter
$stmt->bind_param("i", $UniqueID);

// if UniqueID is a string use s parameter
$stmt->bind_param("s", $UniqueID);

这是代码

    <?php

    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "ur dbname";

    // Create connection
    $connect = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if ($connect->connect_error) {
        die("Connection to db failed");
    }

session_start(); //starts the session

    // ensure that the Id is integer using intval
    //$uniqueid = intval($_POST["uniqueid"]);


    // sanitize your data against xss and html injection
    $uniqueid = strip_tags($_POST["uniqueid"]);
    $breed = strip_tags($_POST["breed"]);
    $color = strip_tags($_POST["color"]);
    $furlength = strip_tags($_POST["furlength"]);
    $dogweight = strip_tags($_POST["dogweight"]);

    //test if all fields filled
    if (($uniqueid == "") || ($breed == "") || ($color == "") || ($furlength == "") || ($dogweight == "")) {
        $_SESSION["errormessage"] = "You must complete all fields!"; //error message if any field is empty
        header("Location:insert.php");
        exit();
    }

    //Avoid sql injection using prepared statement

    $stmt = $connect->prepare("SELECT UniqueID FROM Dogs WHERE UniqueID = ?");

    // UniqueID is integer or number use i parameter
    $stmt->bind_param("i", $UniqueID);

    // if UniqueID is a string use s parameter
    //$stmt->bind_param("s", $UniqueID);

    $stmt->execute();
    $stmt -> store_result(); 
    $stmt -> bind_result($UniqueID); 

    if ($stmt->num_rows >= "1") {
    while ($stmt -> fetch()) { 

    // ensure that xss attack is not possible using htmlentities
    // fetch UniqueID

        echo "your UniqueID: .htmlentities($UniqueID). <br>"; 


    }
    }else{

       echo "No data found";
    }

    $stmt->close();
    $connect->close();
    ?>

推荐阅读