使用php在mysql中插入复选框数据

时间:2018-09-13 15:48:24

标签: php mysql

我正在创建一个表单,其中用户在复选框上打勾,然后1将存储在MySQL表的该列中。如果用户未勾选,则数据库中的该字段将存储0。一栏对应一个复选框。我的HTML代码是:

 Type ;<label class="checkbox-inline"><input type="checkbox" name="mentor" value="1" >Mentor</label>

  <label class="checkbox-inline"><input type="checkbox" name="core" value="1" >Core</label>

我的PHP代码是

 $name = mysqli_real_escape_string($DBcon, $_POST['name']);
    $mentor;
    if (isset ($_POST['mentor']) == "1")
    {   
        $mentor = 1;

    } 
    else 
    {  
         $mentor = 0;
    }
    $core;
    if (isset ($_POST['core']) == "1")
    {   
        $core =1;

    } 
    else 
    {  
         $core =0;
    }
    $insert = $DBcon->query("INSERT into contributor(name,mentor,core) VALUES('$name','$mentor','$core')");

但是我收到“您的SQL语法有误;请查看与您的MySQL服务器版本相对应的手册,以在'???? \” Exif \ 0 \ 0MM \ 0 * \附近使用正确的语法。在第1行的0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0?\ 0 \ 0 \ 0 \ 0 \ 0 \ 0?\ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \'在第1行“ 当我按下“提交”按钮

时出现此错误

3 个答案:

答案 0 :(得分:0)

PHP使用单引号表示文字。也就是说,$ varname不会被解释为0,而是$ varname。删除单引号,它应该可以工作。

"INSERT into contributor(name,mentor,core) VALUES($name,$mentor,$core)"

'INSERT into contributor(name,mentor,core) VALUES('.$name.','.$mentor.','.$core.')'

如果这是可行的,请阅读PHP PDO及其增加的安全性。

答案 1 :(得分:0)

您似乎在代码中混合了过程面向对象 mysqli_*语句。您应该选择一个。在此处更改行:

mysqli_real_escape_string($DBcon, $_POST['name']);

对此:

$DBCon->real_escape_string($_POST['name']);

此外,您还将希望在查询语句中删除嵌套的单引号:

$insert = $DBcon->query("INSERT into contributor(name,mentor,core) VALUES($name,$mentor,$core)");

安全隐患:

我不得不说(并且也没有回应上面的评论),您正在使自己对使用此方法的 SQL注入攻击持开放态度。为了确保您受到保护,应该考虑使用mysqli_*PDO_*扩展名提供的准备好的语句

考虑使用以下更安全的替代方法,而不是您上面使用的代码:

$DBCon = new \PDO('{dsn}', '{user}', '{pass}', [
    \PDO::ATTR_ERRMODE          => \PDO::ERRMODE_EXCEPTION,
    \PDO::ATTR_EMULATE_PREPARES => FALSE
]);

$mentor = (isset($_POST['mentor']) AND intval($_POST['mentor']) === 1) ? 1 : 0;
$core = (isset($_POST['core']) AND intval($_POST['core']) === 1) ? 1 : 0;

try {
    $stmt = $DBCon->prepare("INSERT INTO contributor(name,mentor,core) VALUES (:name,:mentor,:core)");

    $stmt->bindParam(':name', $name, PDO::PARAM_STR);
    $stmt->bindParam(':mentor', $mentor, PDO::PARAM_INT);
    $stmt->bindParam(':core', $core, PDO::PARAM_INT);

    $stmt->execute();

    /* Cleanup (if you are finished interacting with the database) */
    $stmt = NULL;
    $DBCon = NULL;

} catch (\PDOException $e) {
    /* Handle Error Here */
}

答案 2 :(得分:0)

# as @War10ck mentioned, you're mixing Procedural-style with object oriented
$name = $DBCon->real_escape_string($_POST['name']);

# You were comparing a boolean (isset) with "1". Since it's a checkbox, you an do this (or $_POST['mentor'] == "1" since that's your value on the form..)
$mentor = isset($_POST['mentor']) ? 1 : 0;
$core = isset($_POST['core']) ? 1 : 0;

# remove single quotes from your $paramaters
$insert = $DBcon->query("INSERT into contributor(name,mentor,core) VALUES($name, $mentor, $core)");

请注意,您应该使用PDO准备好的语句,就像其他人提到的那样

$stmt = $DBcon->prepare("INSERT INTO contributor(name, mentor, core) VALUES(?,?,?)");
$stmt->bind_param('sssd', $name, $mentor, $core);

$insert = $stmt->execute();