简体   繁体   English

sql查询只放入一个记录而不是多个?

[英]sql query only putting in one record not multiple?

Hi I'm just trying to do this php/sql query, I'm attempting to enter more than one record at a time using this code but it's only entering one, not the others... 嗨,我只是想执行此php / sql查询,我尝试使用此代码一次输入多个记录,但只输入一个,而不输入其他...

CODE: 码:

    <?php
        $results=$_POST['results'];
        $day=$_POST['day'];
        $lmID=$_POST['lmID'];
        $cID=$_POST['cID'];
        $count_cID = count($_POST['cID']);

        for($i=0;$i<$count_cID ;$i++){
        $_results = mysql_escape_string($results[$i]);
        $_day = mysql_escape_string($day[$i]);
        $_lmID = mysql_escape_string($lmID[$i]);
        $_cID = mysql_escape_string($cID[$i]);

        $sql = mysql_query("INSERT INTO Club (results, day, lmID, cID) VALUES ('$_results', '$_day', '$_lmID', '$_cID')");
        $result = mysql_query($sql);
    }

    ?>

HTML FORM: HTML格式:

            League Match ID:
            <?php echo $lmID;?> <input type="hidden" name="lmID[]" value="<?php echo $lmID; ?>" >
            Competitor ID:
            <?php echo $cID;?> <input type="hidden" name="cID[]" value="<?php echo $cID; ?>" >
            Result:
            <input type="text" name="results[]">
            Day:
            <input type="text" name="day[]">

            <input type="submit" name="submit">
        </form>
$sql = mysql_query("INSERT INTO Club (results, day, lmID, cID) VALUES ('$_results', '$_day', '$_lmID', '$_cID')");
$result = mysql_query($sql);

Should be: 应该:

$sql = <<<EOF
INSERT INTO Club (results, day, lmID, cID) 
VALUES ('{$_results}', '{$_day}', '{$_lmID}', '{$_cID}')
EOF;
$result = mysql_query($sql);

As mentioned in other comments, please do not use mysql_ database functions - use PDO or mysqli_ functions. 如其他注释中所述,请不要使用mysql_数据库函数-使用PDO或mysqli_函数。 mysql_query is deprecated (for good reason). 不推荐使用mysql_query (有充分的理由)。 With the other libraries, it seems your statement would also be a candidate for PREPARE followed by EXECUTE. 在其他库中,您的语句似乎也可以成为PREPARE的候选对象,然后再执行EXECUTE。 eg using PDO.... 例如使用PDO。

$sql = <<<EOF
INSERT INTO Club (results, day, lmID, cID) 
VALUES (?, ?, ?, ?)
EOF;
$stmt = $db->prepare( $sql);

for($i=0;$i<$count_cID ;$i++){
    // escape your data here - you might not need to with PDO
    $result = $stmt->execute( array( $_results, $day, $_lmID, $_cID));
}

Make sure $count_cID is more than 1 (one). 确保$count_cID大于1(一)。 Which it is not, looks like. 它不是,看起来像。 The reason I'm referring to this variable is you are comparing $i to it, and in your for loop, if it's not greater than 1, then the loop contents will be executed once only, as on the second try the condition will not validate anymore. 我指的是此变量的原因是您将$i与它进行比较,并且在for循环中,如果它不大于1,则循环内容将仅执行一次,因为在第二次尝试时,条件不会验证一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM