数据作为未定义插入,然后在databasr中作为数字出现

时间:2015-08-13 19:14:52

标签: javascript php count

我在使用以下代码时遇到了一些困难。我正在尝试按下此表单后输出的用户给出一个数字,如-1,2,3,4等

 <form method="POST" name="form">
    <input type="submit" value="Create Draft Order" name="shuffle">
</form>

但是,回显的项目标识为“未定义”。我不确定我的Javascript是否对我想做的事情有误,但其余输出/回声正确。

然后当我通过点击Finalyze草稿订单按钮提交它。所有这些都成功发送到我的数据库,除了drafted_order列(我试图将此计数发送到的列)显示在4.这似乎是计算该数据库表中的记录数并在那里显示它。我没有得到的是在页面上,它显示数字未定义?

如果我没有将此输入添加到Js,脚本将失败..

 '<input type="hidden" name="count[]" value="' + data[i].drafted_order + '">' +

我不明白为什么它会以未定义的形式出现,以及为什么不输入用户的1,2,3,4等等顺序。

我做错了什么?

echo 'Users to be given draft order: <br>';
$array = array();
while ($row = mysqli_fetch_assoc($query)) {
    $array[] = $row;
    echo $row['firstname'] . ' ' . $row['lastname'] . '<br>';
}
?>
<form method="POST" name="form">
    <input type="submit" value="Create Draft Order" name="shuffle">
</form>

    Shuffled results: <br>
    <div class="main-bag">     
    <div class="shuffle_results" id="results"></div>  
     <form method="post">
<?php
$count = 0;
    foreach ($array as $result) :
    $count++;
    $shuffle_count = $count;
        $shuffle_firstname = htmlentities($result['firstname']);
        $shuffle_lastname = htmlentities($result['lastname']);
        $shuffle_id = htmlentities($result['id']);
        $shuffle_username = htmlentities($result['username']);
        $shuffle_email = htmlentities($result['email']);
?>
    <input type="hidden" name="count[]" value="<?php echo $shuffle_count; ?>">
        <input type="hidden" name="firstname[]" value="<?php echo $shuffle_firstname; ?>">
        <input type="hidden" name="lastname[]" value="<?php echo $shuffle_lastname; ?>">
        <input type="hidden" name="id[]" value="<?php echo $shuffle_id; ?>">
        <input type="hidden" name="username[]" value="<?php echo $shuffle_username; ?>">
        <input type="hidden" name="email[]" value="<?php echo $shuffle_email; ?>">

<?php 

    endforeach;
     if ( isset($_POST['shuffle'] ) ) :
        echo '<input type="submit" value="Finalize Draft Order" name="insert">';
    endif;

?>
    </form>
<?php        
if (isset($_POST['insert'])) {
    $con = mysqli_connect("localhost", "", "", "");
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    $draft_stmt1 = $con->prepare("INSERT INTO user_players (user_id, firstname, lastname, username, email) VALUES (?, ?, ?, ?, ?)");
    $draft_stmt2 = $con->prepare("INSERT INTO drafted_players (user_id, drafted_order, firstname, lastname, username, email) VALUES (?, ?, ?, ?, ?, ?)");

  if ( false===$draft_stmt1|| false===$draft_stmt2 ) {
         // Check Errors for prepare
        die('Add to user players prepare() failed: ' . htmlspecialchars($con->error));
    }
     $draft_stmt1->bind_param('issss', $shuffle_id, $shuffle_firstname, $shuffle_lastname, $shuffle_username, $shuffle_email);
     $draft_stmt2->bind_param('iissss', $shuffle_id, $shuffle_count, $shuffle_firstname, $shuffle_lastname, $shuffle_username, $shuffle_email);

    foreach ($_POST['id'] as $i => $shuffle_id) {
        $shuffle_firstname = $_POST['firstname'][$i];
        $shuffle_lastname = $_POST['lastname'][$i];
        $shuffle_username = $_POST['username'][$i];
        $shuffle_email = $_POST['email'][$i];

        $draft_stmt1->execute() or
            die('Add to user players execute() failed: ' . htmlspecialchars($draft_stmt1->error));
        $draft_stmt2->execute() or
            die('Add to user 

的Javascript

var displayResults = function(data){
    var i = 0;
    var lineheight = 24;
    var time = 3000;
    var interval = setInterval(function(){ 
        if( i <= data.length){ 
            console.log( data[i] ); 
            $('#results').append('<div class="result">' +
                        //'<div class="shuffle_results">' + data[i].firstname + ' ' + data[i].lastname + '</div>' + 
                        '<div class="shuffle_results">' + data[i].drafted_order + ' '+ data[i].firstname + ' ' + data[i].lastname + '</div>' + 
                        '<input type="hidden" name="count[]" value="' + data[i].drafted_order + '">' +
                        '<input type="hidden" name="firstname[]" value="' + data[i].firstname + '">' +
                        '<input type="hidden" name="lastname[]" value="' + data[i].lastname + '">' +
                        '<input type="hidden" name="id[]" value="' + data[i].id + '">' +
                        '<input type="hidden" name="username[]" value="' + data[i].username + '">' +
                        '<input type="hidden" name="email[]" value="' + data[i].email + '">' +
                    '</div>');

1 个答案:

答案 0 :(得分:0)

if( i <= data.length){ 

尝试将<=替换为<。数组的索引不包括长度(因为从零开始)。