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

ctehm74n  于 2021-06-21  发布在  Mysql
关注(0)|答案(3)|浏览(354)

我尝试将一些由for循环生成的许可证插入到数据库中,但得到了错误消息:您的sql语法有错误;请查看与您的mysql服务器版本对应的手册,以了解使用第1行的near'keys)values('46f2-sh73-2qdd-z4vh-hv')的正确语法
我已经有一段时间了,现在试图fiure它,但它给出了相同的错误,每次我运行它。
这是我的密码:

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

为什么我会犯这样的错误?

1sbrub3j

1sbrub3j1#

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

cbwuti442#

运行该代码并检查您的密钥字段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>
umuewwlo

umuewwlo3#

请正确读取sql错误。
mysql抛出了一个sql语法错误,因此insert查询的语法不正确。

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

在许可证密钥(keys)和“values”之间留出一个空格
同时在“keys”列周围加上反勾号。

相关问题