gpt4 book ai didi

php - 从for循环向数据库插入错误

转载 作者:行者123 更新时间:2023-11-30 21:48:59 25 4
gpt4 key购买 nike

我尝试从 for 循环中插入一些许可证生成的 kes 到数据库中,但我收到错误消息:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本对应的手册,了解在第 1 行的 'keys)values('46F2-SH73-2QDD-Z4VH-HV')' 附近使用的正确语法

我已经使用它一段时间了,现在试图解决它,但每次运行它都会出现同样的错误。

这是我的代码:

<?php
//ob_start();
//session_start();
//error_reporting(0);
//ini_set('display_errors', '0');
date_default_timezone_set('Africa/Lagos');
#$db = parse_ini_file("../config/db.ini");
$dbhost = 'localhost'; //$db['host'];
$dbuser = 'root';//$db['user'];
$dbpass = '';//$db['pass'];
$dbname = 'infonetsch_mgmt';//$db['dbname'];

//Connect
$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if (mysqli_connect_errno()) {
printf("MySQLi connection failed: ", mysqli_connect_error());
exit();
}
// Change character set to utf8
if (!$mysqli->set_charset('utf8')) {
printf('Error loading character set utf8: %s\n', $mysqli->error);
}

?>
<html>
<head>
<title>License Key Generator</title>
</head>
<body style="background-color:#F0F0F0">
<h1>License Key generation</h1>
<form method="POST" action="">
<table>
<tr>
<td>Keys to generate</td>
<td><select name="numkeys">
<option value="1">1</option>
<option value="5" selected>5</option>
<option value="10">10</option>
<option value="20">20</option>
<option value="50">50</option>
<option value="100">100</option>
<option value="500">500</option>
<option value="1000">1000</option>
<option value="5000">5000</option>
<option value="10000">10000</option>
<option value="20000">20000</option>
<option value="50000">50000</option>
</select></td>
</tr>
<tr>
<td>Length of Key</td>
<td><select name="keylen">
<option value="8">8</option>
<option value="10">10</option>
<option value="12">12</option>
<option value="14">14</option>
<option value="16">16</option>
<option value="18">18</option>
<option value="20">20</option>
</select></td>
</tr>

</table>
<input name="validate" type="submit" value="Generate!"/>
</table>
</form>

<?php
if(isset($_POST['validate'])){
$name= 'a';//$_POST['client'];
$software= 'sis';//$_POST['software'];
$numkeys=$_POST['numkeys']; if($numkeys<1)$numkeys=1; if($numkeys>50000)$numkeys=50000;
$keylen=$_POST['keylen'];if($keylen<1)$keylen=1; if($keylen>20)$keylen=20;

include("license_key.class.php");

$pass=new license_key();
echo "<h3>Generating $numkeys Random License Keys </h3>
KeyLenght: $keylen</a><hr/>";

for($i=0;$i<$numkeys;$i++){
$pass->keylen=$keylen;
$key= $pass->codeGenerate($name.$software);
$get = mysqli_query($mysqli, "insert into license_keys(keys)values('".$key."')");//Insert query

$j=$i+1;

echo "$j- $key <br/>";


}
if($get){
echo "Done";
}else{ printf("Errormessage: %s\n", $mysqli->error);; }
echo "<br/><br/><a href=\"test.php\">Generate again</a><br/><br/>";

}
?>
</body>
</html>

为什么会出现这样的错误?

最佳答案

您的许可证 key 中可能有一些特殊字符。尝试使用 prepare 语句并引用 https://www.w3schools.com/php/php_mysql_prepared_statements.asp作为

$stmt = $mysqli->prepare("insert into license_keys (`keys`) values(?)");
$stmt->bind_param("s", $key);
$stmt->execute();

关于php - 从for循环向数据库插入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48134653/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com