2013-11-15 70 views
0

我想用PHP插入到从网页上的MySQL数据库,但试图使用变量,当它不工作(它工作得很好,如果我使用something同时使用$something不)插入不与PHP变量的工作

下面是代码:

mysqli_query($con,"INSERT INTO Atendido (idPaciente,idDoctor,fecha,costo,tipoAtencion) values ('".$_GET['iddoctor']."', '".$_GET['idpacient']."', '".$_GET['date']."', '".$_GET['amount']."', '".$_GET['description']."')"); 

和数据来自一个其它页面与这种形式:

<form action="thanks/index.php" method="get"> 
    <span class="largetext">ID. doctor</span><br/> 
    <input type="password" name="iddoctor"><br/> 
    <span class="largetext">ID. patient</span><br/> 
    <input type="password" name="idpatient"><br/> 
    <span class="largetext">Date</span><br/> 
    <input type="date" name="date"><br/> 
    <span class="largetext">Amount</span><br/> 
    <input type="number" name="amount"><br/> 
    <span class="largetext">Description</span><br/> 
    <input type="text" name="description"><br/><br/> 
    <input type="submit" value="Accept" style="background-color:#FF5F00; color:#FFFFFF; opacity: 0.77;"> 
</form> 

谢谢!给所有注意到SQL injection问题的人,我也会看看这个。

我现在的作品,这里是更正后的代码:

mysqli_query($con,"INSERT INTO Atendido (idPaciente,idDoctor,fecha,costo,tipoAtencion) VALUES ('".$_GET['idpatient']."', '".$_GET['iddoctor']."','".$_GET['date']."', '".$_GET['amount']."', '".$_GET['description']."')"); 
+8

圣SQL注入蝙蝠侠! –

+1

我等待第一个评论:你是SQL注入漏洞 – aldanux

+0

arturojain你是如何破解stackoverflow比成为aldanux :) – ismail

回答

1

正如与OP讨论的那样,$_GET['idpacient']name="idpatient"所以没有匹配。

我相信你想使用$_GET['idpatient']name="idpacient"

采取的纠正哪一个你挑。

1

的字段顺序错误:

Atendido (idPaciente, idDoctor 
VALUES ('".$_GET['iddoctor']."', '".$_GET['idpacient']."' 

变化:

"INSERT INTO Atendido (idPaciente,idDoctor,fecha,costo,tipoAtencion) 
VALUES ('".$_GET['idpacient']."', '".$_GET['iddoctor']."', 
'".$_GET['date']."', '".$_GET['amount']."', '".$_GET['description']."')") 
+1

这些字段的顺序可能错误,但这不应该阻止发生'INSERT'。 – 2013-11-15 01:53:55

+0

可以像外键violetion – rray

+0

感谢您也发现错误。 – arturojain

0

INSERT语法使得小尽管如此:

mysqli_query($con,"INSERT INTO Atendido (idPaciente,idDoctor,fecha,costo,tipoAtencion) values ('".$_GET['iddoctor']."', '".$_GET['idpacient']."', '".$_GET['date']."', '".$_GET['amount']."', '".$_GET['description']."')"); 

我会建议你做以下,并使用sprintf -to使格式化简单:

$insert_query = sprintf("INSERT INTO Atendido (idPaciente,idDoctor,fecha,costo,tipoAtencion) values ('%s','%s','%s','%s','%s')", $_GET['iddoctor'], $_GET['idpacient'], $_GET['date'], $_GET['amount'], $_GET['description']); 
mysqli_query($con,$insert_query); 

什么是好的约sprintf是它允许从数据本身,你可以轻松地单独格式化逻辑。把它想象成一个小型的模板系统。

而且,我甚至会建议一步把它做这个:

$data_keys = array('idPaciente','idDoctor','fecha','costo','tipoAtencion'); 
$data_values = array(); 
foreach($data_keys as $key) { 
    $value = array_key_exists($key, $_GET) && !empty($_GET[$key]) ? $_GET[$key] : null; 
    if (!empty($value)) { 
    $data_values[$key] = $value; 
    } 
} 
if (!empty($data_values)) {  
    $insert_query = sprintf("INSERT INTO Atendido (%s) values ('%s')", implode(',', array_keys($data_values)), implode("','", $data_values)); 
    echo $insert_query; 
    mysqli_query($con,$insert_query); 
} 

你有一个过程来过滤$_GET值,使的INSERT更容易创造,了解如何irregardless这样你有很多价值。