我建议调查 PHP 手册页中描述的 mysqli 类的准备功能:
http://www.php.net/manual/en/mysqli-stmt.prepare.php
还有 bind_params 功能:
http://www.php.net/manual/en/mysqli-stmt.bind-param.php
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* create a prepared statement */
$stmt = mysqli_stmt_init($link);
$query='INSERT INTO activadosmil (cod, stock, precio, subcategoria, ".
"descripcion, ean, canon, fabricante) VALUES (?, ?, ?, ?, ?, ?, ?,?)';
if (mysqli_stmt_prepare($stmt, $query));{
/* bind parameters for markers */
// NB: the first parameter is a string of single char type indicators
// - One for each of the actual parameters s=string, d=double, I=integer, b=blob
// - In this case all 8 appear to be string - so 'ssssssss'
mysqli_stmt_bind_param('ssssssss', $cod, $stock, $precio, $subcategoria, $descripcion,
$ean, $canon, $fabricante);
/* execute query */
mysqli_stmt_execute($stmt);
/* close statement */
mysqli_stmt_close($stmt);
}
/* close connection */
mysqli_close($link);
?>
使用 bind_param 应该可以防止引号出现任何问题。