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

时间:2018-01-07 05:41:21

标签: php mysql mysqli

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

我已经有一段时间了,现在试图确定它,但每次运行它都会产生同样的错误。

这是我的代码:

<?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>

为什么我会收到这样的错误?

3 个答案:

答案 0 :(得分:1)

许可证密钥中可能有一些特殊的字符。尝试使用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();

答案 1 :(得分:0)

运行该代码并检查您的密钥字段varchar limit

   <?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>

答案 2 :(得分:-1)

请正确阅读SQL错误。

MySQL正在抛出SQL语法错误,因此您的插入查询语法不正确。

insert into license_keys(`keys`) values(?)

在license_keys(键)和'values'

之间加一个空格

还在“键”列周围添加反引号。