gpt4 book ai didi

PHP 和 MySQL : Insert select result into table with id auto_increment

转载 作者:行者123 更新时间:2023-11-29 16:34:42 24 4
gpt4 key购买 nike

我正在尝试为从 userInformation 创建的每个组分配一个 ID 号,并将其保存到 groupInformation 中。我不断收到错误:

Column count doesn't match value count at row 1

我不确定是否应该增加 groupId。如果通过php或mysql。有人建议我创建一个数组来增加 groupId,但我不知道如何开始。

我的 table 是:

GroupInformation = GroupId(主键,自动增量),RegistrationId(外键)

用户信息 = RegistrationId(主键, auto_increment), 姓名, 电子邮件, 地址

$GroupSize = $_POST['groupsize'];

$con = mysqli_connect($host, $user, $password, $dbName) or die("Nope");

if (isset($_POST['create'])) {

//assign group id to groups created
//insert RegistrationId into groupinformation from records randomly selected from userInformation, group Id must increment by every group created in groupinformation

//$query ="Select RegistrationId from userInformation order by RAND() LIMIT ".$GroupSize;
$query = "INSERT INTO groupInformation SELECT RegistrationId FROM (SELECT RegistrationId FROM userInformation ORDER BY RAND() LIMIT ".$GroupSize.") as res";

$result = mysqli_query($con, $query) or die ("query failed " . mysqli_error($con));


//unsure how to use this array
while (($row = mysqli_fetch_row($result)) == true) {
echo $row[0].'<br>';

}

最佳答案

尝试这个查询:

$query = "INSERT INTO groupInformation (RegistrationId) SELECT RegistrationId FROM (SELECT RegistrationId FROM userInformation ORDER BY RAND() LIMIT ".$GroupSize.") as res";

关于PHP 和 MySQL : Insert select result into table with id auto_increment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53688769/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com