数据不是通过MySQL中的表单插入的(编码是在PHP中)

时间:2014-09-15 16:22:56

标签: php mysql forms

我正在尝试通过使用php脚本的表单将数据插入到mysql数据库中。我没有收到任何错误,bt表单提交的信息没有插入数据库..而不是数据(由提交者提交) form),在数据库中插入的值是(1,1,1,1,1)..这也是一种SQL注入攻击吗?(因为我在网上搜索得到了这个攻击名称) 这是我用于将数据插入数据库的PHP代码..

<html>
<head>
<link rel="stylesheet" type="text/css" href="clubcs.css">
<div class="text_field">
<div class="segment_header" align="center">
<?php
$con=mysqli_connect("localhost","root","");

if (!$con)
{
    die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"test");
$first=mysqli_real_escape_string($con,isset($_POST['first']));
$last=mysqli_real_escape_string($con,isset($_POST['last']));
$addr=mysqli_real_escape_string($con,isset($_POST['addr']));
$city=mysqli_real_escape_string($con,isset($_POST['city']));
$contact=mysqli_real_escape_string($con,isset($_POST['contact']));

$sql1=mysqli_prepare($con,"INSERT INTO clubdata (FirstName, LastName, Address, City,Contact_no)
VALUES (?,?,?,?,?)");
$sql1->bind_param("ssssi", $first, $last, $addr, $city, $contact);
$sql1->execute();

if(isset( $_POST["first"] ) &&  isset($_POST["last"]) && isset($_POST["addr"] ) && isset($_POST["city"]) &&  isset($_POST["contact"])  )
{
    echo "Thank You" ." " .$_POST["first"];
    echo " You have Successfully registered with us!";
    exit();
}
mysqli_close($con);
?> 

    有什么东西我没有得到或失踪? Plz建议我任何解决方案!

3 个答案:

答案 0 :(得分:2)

  

在数据库中插入的值为(1,1,1,1,1)

isset()只返回01,它解释了返回的所有1,因为isset()假设您想要全部设置,因此返回TRUE1

  

如果var存在,则返回 TRUE ,其值不是 NULL,否则为FALSE

您还需要使用ternary operator

$first=mysqli_real_escape_string($con,isset($_POST['first']) ? $_POST['first'] : '');
$last=mysqli_real_escape_string($con,isset($_POST['last']) ? $_POST['last'] : '');
$addr=mysqli_real_escape_string($con,isset($_POST['addr']) ? $_POST['addr'] : '');
$city=mysqli_real_escape_string($con,isset($_POST['city']) ? $_POST['city'] : '');
$contact=mysqli_real_escape_string($con,isset($_POST['contact']) ? $_POST['contact'] : '');

<强> 旁注:

如果“contact”是一个字符串(代表),您将需要使用所有s作为绑定。即:"sssss"

$sql1->bind_param("sssss", $first, $last, $addr, $city, $contact);

洞察

答案 1 :(得分:0)

这是因为isset()函数返回1或0,具体取决于值是否设置。您需要事先进行检查,然后将值放入:

if(isset($_POST['first']) && isset($_POST['last']) && isset($_POST['addr']) && isset($_POST['city']) && isset($_POST['contact']))
{
  $first = mysqli_real_escape_string($con, $_POST['first']);
  $last = mysqli_real_escape_string($con, $_POST['last']);
  $addr = mysqli_real_escape_string($con, $_POST['addr']);
  $city = mysqli_real_escape_string($con, $_POST['city']);
  $contact = mysqli_real_escape_string($con, $_POST['contact']);
}

答案 2 :(得分:0)

就像@fred和@Boren指出的那样,你传递的是isset返回的布尔整数值。

如果移动if(isset())条件以包装整个代码,则无需检查是否再次设置该值。

事实是你甚至不需要逃避任何事情,准备声明会为你做。

<?php
if(isset($_POST["first"],$_POST["last"], $_POST["addr"],$_POST["city"],$_POST["contact"]))
{
    $con=mysqli_connect("localhost","root","");

    if (!$con)
    {
        die('Could not connect: ' . mysqli_error($con));
    }
    mysqli_select_db($con,"test");

    $sql = "INSERT INTO clubdata (FirstName, LastName, Address, City,Contact_no) VALUES (?,?,?,?,?)";
    $stmt=mysqli_prepare($con, $sql);

    $stmt->bind_param("ssssi", $_POST['first'], $_POST['last'], $_POST['addr'], $_POST['city'], $_POST['contact']);
    $stmt->execute();

    if(mysqli_num_rows($stmt) > 0){
        echo "Thank You" ." " .$_POST["first"];
        echo " You have Successfully registered with us!";  
    }else{
        echo 'INSERT did not affect any rows';
    }

    mysqli_close($con);
    }else{
        echo "Missing required fields";
    }

}

哦,关闭你的连接之前不要exit,你会把它打开。