使用PHP在数据库不能增加ID

问题描述 投票:0回答:1

我是一个学生,我已经打了一堵墙...

我得到了什么:

function new_id($table, $id_column_name)
{
require_once ("config.php");
$db=mysqli_connect($host,$usernamesql,$passwordsql,$db_name)
           or die ("could not connect");

$query="SELECT MAX($id_column_name) 
            AS max FROM $table";

$result=mysqli_query($db,$query);
$result2=mysqli_fetch_array($result);
$idnew=$result2['max']+1;
return $idnew;
}
//this function is located inside libs/lib.php

下面的代码位于内/signup.php

require ("libs/config.php");
//DB connection stuff

require ("libs/lib.php");
//function above

id_user = 'id'; 
id_student = 'id_student';
//these vars represent column names in users/students tables
//tried `id`, 'id', "id" quote variations, nothing seems to work 

$db=mysqli_connect($host,$usernamesql,$passwordsql,$db_name) 
           or die ("could not connect");
$idnew = new_id($tbl_users, $id_user);
$query="INSERT INTO $tbl_users (id, username, passwd, sex, married, lasttname, firstname, email, datereg, extension, phone) 
        VALUES ('$idnew', '$username', '$passwd', '$sex', '$married', '$lastname', '$first', '$email','$currentDate', '$image_type', '$phone')";
$result=mysqli_query($db,$query);

$idnew = new_id($tbl_student, $id_student);
$query="INSERT INTO $tbl_student (id_student, lastname, firstname)
        VALUES ('$idnew', '$lastname', '$firstname')";
$result=mysqli_query($db,$query);

所以你可以看到我尝试添加到用户表和学生表递增ID为两个表行额外的奖励。

它增加了tbl_users得很好,但它不会增加的ID。我没有把它设置为唯一的,这样我结束了与ID = 1一对夫妇行。它不添加任何东西,虽然tbl_student,我想不通为什么...

值得一提的是,我手动添加一个行远见两个表,函数将采取1 + 1 MAX ID的ID递增到2。

最后,我想有什么毛病我的功能有,但我不能为我的生活弄清楚什么...

php mariadb
1个回答
0
投票

require ("config.php");代替require_once ("config.php");

它没有一个功能我看到里面喜欢require_once。出于某种原因,这为我工作。它甚至增加它没有前可言,请不要介意增加任何标识的tbl_student。

我的猜测是(没有研究),该函数内部,你可以有循环(如果,同时),它迫使你require,这样就可以使用所需的文件,这些循环中。

© www.soinside.com 2019 - 2024. All rights reserved.