2017-06-02 40 views
1

我一直在试图将一些数据输入到我的SQLI数据库使用PHP ..如何以正确的格式获取此查询?

在插入查询,寻找,当我做到这一点通过GUI上的phpMyAdmin的变量需要被包裹在单引号..

这是我如何建立查询至今:

$fields = array('`appName`' => $_POST[appName], 
        '`appDescription`' => $_POST[appDescription], 
        '`UploadDate`' => date("Y-m-d"), 
        '`appWebsite`' => $_POST[appWebsite]); 
printarray($fields); 

print "<br>"; 
print "<br>"; 

$columns = implode(", ",array_keys($fields)); 
$escaped_values = array_map('mysql_real_escape_string', array_values($fields)); 
$values = implode(", ", $escaped_values); 

$sql = "INSERT INTO `applist`.`apps` ($columns) VALUES ($values)"; 

print $sql; 
print "<br>"; 

if (mysqli_query($conn, $sql)) { 
    echo "New record created successfully"; 
} else { 
    echo "Error"; 
} 

这是给我的,像这样的查询..

INSERT INTO `applist`.`apps` (`appName`, `appDescription`, `UploadDate`, `appWebsite`) 
VALUES (SDD, DDD, 2017-06-02, DDDD) 

怎么办我得到的数组的值包裹在单引号?

任何帮助表示赞赏。

+2

不要建立与字符串连接的查询。使用[** mysqli **](https://secure.php.net/manual/en/mysqli.prepare.php)或[** PDO **](https://secure.php.net/manual/ en/pdo.prepared-statements.php)准备带有绑定参数的语句,如[**这篇文章**]所述(https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection -in-PHP)。 –

+0

在这里混合API,这是行不通的。您的查询不是那么大或很复杂,您需要动态构建它。你当然可以,但至少可以用准备好的语句来构建它。 – Qirel

回答

0

由于您使用破灭,所以你可以用它添加引号和SQL查询添加开始和结束的报价:

$fields = array('`appName`' => $_POST[appName], 
        '`appDescription`' => $_POST[appDescription], 
        '`UploadDate`' => date("Y-m-d"), 
        '`appWebsite`' => $_POST[appWebsite]); 
printarray($fields); 

print "<br>"; 
print "<br>"; 

$columns = implode(", ",array_keys($fields)); 
$escaped_values = array_map('mysql_real_escape_string', array_values($fields)); 
$values = implode("', '", $escaped_values); //add qoutes 

$sql = "INSERT INTO `applist`.`apps` ($columns) VALUES ('$values')"; //add start and end qoutes 

print $sql; 
print "<br>"; 

if (mysqli_query($conn, $sql)) { 
    echo "New record created successfully"; 
} else { 
    echo "Error"; 
} 

但它不是很好的解决方案,也许对于其他查询时,发生错误!使用PDO比它好!

0

您需要将单引号附加到每个数组值。

更换

$values = implode(", ", $escaped_values); 

$values = "'" . implode ("', '", $escaped_values) . "'"; 

你甚至追加双引号。

1

我可以发现至少两个错误。

  • 查询缺乏字串,单引号
  • 混合的API(mysql_mysqli_不混合),你用mysql_real_escape_string()

两个错误是由使用的MySQLi准备语句固定。这不是一个非常复杂的查询,也可能是静态编写的,但是如果你想像这样动态编写它,这不是一个问题 - 如果你使用PHP 5.6,你可以使用array拆包...) 。要生成占位符?,我们创建一个数组,其中count($fields)个元素,全部以?作为值。这是用array_fill()完成的。然后我们implode()它就位,就像我们对列做的那样。

$fields = array('`appName`' => $_POST['appName'], 
       '`appDescription`' => $_POST['appDescription'], 
       '`UploadDate`' => date("Y-m-d"), 
       '`appWebsite`' => $_POST['appWebsite']); 
$columns = implode(", ",array_keys($fields)); 

$sql = "INSERT INTO `applist`.`apps` ($columns) VALUES (".implode(", ", array_fill(0, count($fields), '?')).")"; 

if ($stmt = $conn->prepare($sql)) { 
    $stmt->bind_param(str_repeat("s", count($fields)), ...$fields); 
    if ($stmt->execute()) 
     echo "New record created successfully"; 
    else 
     echo "Insert failed"; 
    $stmt->close(); 
} else { 
    echo "Error"; 
} 

这会照顾引用字符串并防止SQL注入。

要获得您可能遇到的任何错误,请使用mysqli_error($conn)和/或mysqli_stmt_error($stmt)。这会告诉你到底发生了什么问题。

您还应引用POST数组中的索引。 PHP会解决它,并将appName转换为'appName',但如果您记录错误(如您应该),它会生成通知。

0

$列=破灭( “”,array_keys($字段)); $ escaped_values = array_map('mysql_real_escape_string',array_values($ fields));

Ouch。不好。

荣誉试图逃避内容,但如果数据中包含逗号,则会使用此代码取消粘贴。

你已经有了一个很好的泛型方法的开头插入数据,可以考虑:

function insert($db_conn, $table, $data) 
{ 
    $ins_vals=array(); // note we write the transformed data to a new array 
     // as we may be modifying the column names too 
    foreach ($data as $key=>$val) { 
     // we need to protect the column names from injection attacks as 
     // well as the data hence: 
     $quoted_key="`" . str_replace("`", "", $key) . "`"; 

     // next we create an appropriate representation of the data value 
     if (is_null($val)) { 
      $ins_vals[$quoted_key]="NULL"; // in SQL 'NULL' != NULL 
     } else if (is_numeric($val)) { 
      // nothing to change here 
      $ins_vals[$quoted_key]=$val; // no need to quote/escape 
     } else { 
      $ins_vals[$quoted_key]="'" 
       . mysqli_real_escape_string($dbconn, $val) 
       . "'"; 
     } 
    } 
    // then we stick the bits together in an SQL statement 
    $cols=implode(",", array_keys($ins_vals)); 
    $vals=implode(",", $ins_vals); 
    $sql="INSERT INTO $table ($cols) VALUES ($vals)"; 
    return mysqli_query($dbconn, $sql); 
}