测验表字段的索引基数是' 2',但它应该是' 0' 0

时间:2018-01-15 11:40:39

标签: php mysql mysqli cardinality

我有一个简单的测验系统,其中有一个测验表和PHP代码,用于向数据库添加测验,包括每个测验的唯一ID。在数据库&工作的程序,我注意到索引基数是0,并且在似乎不起作用的数据库中,我注意到索引基数是2.

这是否导致缺乏独特的quizid生成,或者是否与代码有关?

例如,我创建了一个名为testquiz的测验,它应该生成一个1,2,3等的id。

相反,它产生了这个:

enter image description here

在可行的版本中,它会(正确地)生成一个唯一的id和测验ID,存储起来:

enter image description here 不工作基数如下所示:(此表的基数为2,运行代码时不会生成唯一的测验ID)

Action  Keyname Type    Unique  Packed  Column  Cardinality Collation   Null    Comment
Edit Edit   Drop Drop   PRIMARY BTREE   Yes No  id  2   A   No

然而,这可行:

Action  Keyname Type    Unique  Packed  Column  Cardinality Collation   Null    Comment
Edit Edit   Drop Drop   PRIMARY BTREE   Yes No  id  0   A   No  

MySQL会自动生成基数,所以我假设我无法更改它?如果这是问题,可以做些什么?

否则,如果代码存在问题,有人可以指出问题所在:

添加新测验的代码

<?php

    include('scripts/connect_db.php');

        if(isset($_POST['quizName']) && $_POST['quizName'] != ""
        && isset($_POST['quizTime']) && $_POST['quizTime'] != ""
        && isset($_POST['numQues']) && $_POST['numQues'] != ""){

            $qName=mysqli_real_escape_string($con,$_POST['quizName']);
            $qTime=mysqli_real_escape_string($con,$_POST['quizTime']);
            $nQues=mysqli_real_escape_string($con,$_POST['numQues']);

            $qTime = preg_replace('/[^0-9]/', "", $qTime);
            $nQues = preg_replace('/[^0-9]/', "", $nQues);

            $fetch=mysqli_query($con,"SELECT id FROM quizes 
                                WHERE quiz_name='$qName'")or die(mysqli_error());
            $count=mysqli_num_rows($fetch);
            if($count!="")
            {
                $user_msg = 'Sorry, but \ '.$qName.' \ already exists!';
                header('location: admin.php?msg='.$user_msg.'');
            }else{
                mysqli_query($con,"INSERT INTO quizes (quiz_name, display_questions, time_allotted) 
                    VALUES ('$qName','$nQues','$qTime')")or die(mysqli_error());

                $lastId = mysqli_insert_id();
                mysqli_query($con,"UPDATE quizes SET quiz_id='$lastId' 
                                WHERE id='$lastId' LIMIT 1")or die(mysqli_error());

                $user_msg = 'Quiz, \ '.$qName.' \ has been created!';
                header('location: admin.php?msg='.$user_msg.'');
            }
        }else{
            $user_msg = 'Sorry, but Something went wrong';
            header('location: admin.php?msg='.$user_msg.'');
        }
?>

总而言之,主要问题是当运行代码以添加新测验时,不会生成和/或未添加到数据库中的唯一测验ID,因此测验ID始终为0.

在我的程序版本中,生成并正确添加了测验ID,但该版本包含已失效的MySQL函数。

以前的原始代码正常工作(但使用已解散的MySQL):

<?php

    include('scripts/connect_db.php');

        if(isset($_POST['quizName']) && $_POST['quizName'] != ""
        && isset($_POST['quizTime']) && $_POST['quizTime'] != ""
        && isset($_POST['numQues']) && $_POST['numQues'] != ""){

            $qName=mysql_real_escape_string($_POST['quizName']);
            $qTime=mysql_real_escape_string($_POST['quizTime']);
            $nQues=mysql_real_escape_string($_POST['numQues']);

            $qTime = preg_replace('/[^0-9]/', "", $qTime);
            $nQues = preg_replace('/[^0-9]/', "", $nQues);

            $fetch=mysql_query("SELECT id FROM quizes 
                                WHERE quiz_name='$qName'")or die(mysql_error());
            $count=mysql_num_rows($fetch);
            if($count!="")
            {
                $user_msg = 'Sorry, but \ '.$qName.' \ already exists!';
                header('location: admin.php?msg='.$user_msg.'');
            }else{
                mysql_query("INSERT INTO quizes (quiz_name, display_questions, time_allotted) 
                    VALUES ('$qName','$nQues','$qTime')")or die(mysql_error());

                $lastId = mysql_insert_id();
                mysql_query("UPDATE quizes SET quiz_id='$lastId' 
                                WHERE id='$lastId' LIMIT 1")or die(mysql_error());

                $user_msg = 'Quiz, \ '.$qName.' \ has been created!';
                header('location: admin.php?msg='.$user_msg.'');
            }
        }else{
            $user_msg = 'Sorry, but Something went wrong';
            header('location: admin.php?msg='.$user_msg.'');
        }
?>

我无法发现错误,或者判断错误是在php代码中,还是在数据库设置中。

思考?解决方案?谢谢。

2 个答案:

答案 0 :(得分:1)

您必须激活“自动增量”选项 当您激活它时,每次插入新行时都会生成uniqueId。

答案 1 :(得分:0)

找到答案。代码有错误,缺少参数:

$lastId = mysqli_insert_id($con);

使用上述内容,修复它。