Php 试图在本地主机上用我的代码修复SQL注入,但无法使其正常工作。(菲律宾)

Php 试图在本地主机上用我的代码修复SQL注入,但无法使其正常工作。(菲律宾),php,mysql,mysqli,Php,Mysql,Mysqli,我对PHP非常陌生,有人告诉我,我以前的代码可以被SQL注入,所以我现在正在尝试解决这个问题。这就是我到目前为止所想到的。当我使用下面的代码提交到我的表单时,我收到以下错误: 注意:未定义的变量:第49行的/Applications/XAMPP/xamppfiles/htdocs/index.php中的mysqli 致命错误:在第49行的/Applications/XAMPP/xamppfiles/htdocs/index.php中调用null上的成员函数prepare() 我已经在第49行发表

我对PHP非常陌生,有人告诉我,我以前的代码可以被SQL注入,所以我现在正在尝试解决这个问题。这就是我到目前为止所想到的。当我使用下面的代码提交到我的表单时,我收到以下错误:

注意:未定义的变量:第49行的/Applications/XAMPP/xamppfiles/htdocs/index.php中的mysqli

致命错误:在第49行的/Applications/XAMPP/xamppfiles/htdocs/index.php中调用null上的成员函数prepare()

我已经在第49行发表了评论

<?php 

$mysql_pekare= new mysqli ("localhost", "username","pass", "database");

if(!empty($_GET['namn'])) {
$unsafe_variable = "Welcome ". $_GET["namn"]. ". You are ".$_GET["age"]. " years old." ; 

$stmt = $mysqli->prepare("INSERT INTO Personinfo(`Personname`, `Personage`) VALUES('$_GET[namn]', '$_GET[age]')");` //this is line 49

$stmt->bind_param("s", $unsafe_variable);

$stmt->execute();

$stmt->close();

$mysqli->close();
}
?>

您首先创建
$mysql\u pekare
,然后尝试使用
$msqli
。这是您的问题

改变你的变量以匹配,你应该是好的

$mysql_pekare = new mysqli(...);

$mysql_pekare->prepare(...);

您首先创建
$mysql\u pekare
,然后尝试使用
$msqli
。这是您的问题

改变你的变量以匹配,你应该是好的

$mysql_pekare = new mysqli(...);

$mysql_pekare->prepare(...);

您必须使用您命名的连接:

$mysql_pekare= new mysqli ("localhost", "username","pass", "database");

if(!empty($_GET['namn'])) {
    $unsafe_variable = "Welcome ". $_GET["namn"]. " You are ".$_GET["age"]. " years old." ; 

    $stmt = $mysql_pekare->prepare("INSERT INTO Personinfo(`Personname`, `Personage`) VALUES(?,?))";
    $stmt->bind_param("ss", $_GET['namn'], $_GET['age']);
    $stmt->execute();
    $mysql_pekare->close();
}

一旦这样做,您必须为每个不安全变量使用占位符(
),然后绑定到这些变量。

您必须使用您命名的连接:

$mysql_pekare= new mysqli ("localhost", "username","pass", "database");

if(!empty($_GET['namn'])) {
    $unsafe_variable = "Welcome ". $_GET["namn"]. " You are ".$_GET["age"]. " years old." ; 

    $stmt = $mysql_pekare->prepare("INSERT INTO Personinfo(`Personname`, `Personage`) VALUES(?,?))";
    $stmt->bind_param("ss", $_GET['namn'], $_GET['age']);
    $stmt->execute();
    $mysql_pekare->close();
}

一旦您这样做,您就必须为每个不安全变量使用占位符(
),然后绑定到这些变量。

您在代码的其余部分缺少一些关键问题。您在代码的其余部分缺少一些关键问题。