2016-09-02 94 views
-7

我想在我的SQL插入由GET数据,但我不能插入数据插入数据通过GET

<?php 

include("config.php"); 

$f=$_GET["first_name"]; 
$l=$_GET["last_name"]; 
$e=$_GET["email"]; 
$m=$_GET["mobile"]; 
$b=$_GET["birthday"]; 
$g=$_GET["gender"]; 



$insert="INSERT INTO user (`first_name`, `last_name`, `email`, `mobile`, `birthday`, `gender`) 
VALUES ('$f', '$l', '$e', '$m', '$b', '$g')"; 
mysqli_query($insert); 



?> 

我通过这个链接尝试插入数据:

http://localhost:8888/restfull/insert.php?f=hayoo

+2

你正在使用错误的GET数组。 '?f'?这将无法正常工作。你也需要'&'运算符。 –

+3

您也没有将db连接传递给查询。阅读手册http://php.net/manual/en/mysqli.query.php –

+5

您可以[** SQL注入**](https://www.owasp.org/index.php/SQL_Injection )。至于你的问题,考虑一下:你正在''_GET'中使用不存在的值。这并不奇怪,不起作用。 –

回答

1

我已经使用mysqli很长一段时间了,下面的代码应该很可能运行。正如其他人所提到的,从不绑定未经过处理的数据(即使您认为您信任数据,仍然可以安全地使用准备好的数据)。

<?php 
//Create you db connection 
$conn = new mysqli('server', 'user', 'password', 'databasename'); 
//Create insert statement. Never concat un-sanitized data in statements 
$insert="INSERT INTO user (`first_name`, `last_name`, `email`, `mobile`, `birthday`, `gender`) 
VALUES (?, ?, ?, ?, ?, ?)"; 


$stmt = $conn->prepare($sql); 

//Values corespond to ? except the first param which represents format of expected data. "s" stands for string 
$stmt->bind_param(
    'ssssss', 
    $_GET["first_name"], 
    $_GET["last_name"], 
    $_GET["email"], 
    $_GET["mobile"], 
    $_GET["birthday"], 
    $_GET["gender"] 
); 
$stmt->execute(); 

您的网址应该是这样的: http://localhost:8888/restfull/insert.php?first_name=john&last_name=Doe&[email protected]test.com&mobile=0&birthday=May&gender=male

确保,如果你是把上述网址在某些类型的表格你正确的URL编码值(我注意到很多你收集意志值像要求它斜杠等)。