Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何检查复选框是否选中,并执行必要的查询以更新相应的表?_Php_Mysql - Fatal编程技术网

Php 如何检查复选框是否选中,并执行必要的查询以更新相应的表?

Php 如何检查复选框是否选中,并执行必要的查询以更新相应的表?,php,mysql,Php,Mysql,HTML 我的数据库中有categories表,其中存储了以下数据: //obtains last insert id $wallId = mysql_insert_id(); echo $wallId; 我还有另一个名为wall\u categories的表格,其中: category_id category_name 1 Wallpaper 2 Photography 在从最后一个插入id中获取墙id之后,我想检查这两个i

HTML

我的数据库中有
categories
表,其中存储了以下数据:

//obtains last insert id
    $wallId = mysql_insert_id();
    echo $wallId;
我还有另一个名为
wall\u categories
的表格,其中:

category_id  category_name
1             Wallpaper
2             Photography
在从最后一个插入id中获取墙id之后,我想检查这两个id中的哪个类别被选中。例如,选中了
Wallpaper
复选框,因为
category\u id
1
,我想将该
category\u id
wall\u id
将要存储到我的
wall\u categories
表中的任何内容一起存储。所以我的表将变成(比如说
wall\u id
16

最后要添加的一件事是,因为类别是复选框,当两者都选中时,我想将两者都添加到我的
wall\u类别
表中,这样我的
wall\u类别
表将变成:

wall_id (P, F)    category_id (P, F)
 16                 1
我尝试了一些变通方法,但我还是PHP新手。我想请你帮助我如何做到这一点。谢谢(脚本将仅在内部使用。)

HTML

wall_id (P, F)    category_id (P, F)
     16                 1
     16                 2
如果您的
wall\u id
来自另一个表,请跳过
get last
步骤。并通过从另一个表中获取的
wall\u id
变量删除/更改
$next\u wall\u id

如果你想做的话,就这些了。有问题吗?发现了一些错误?评论如下:D

HTML

wall_id (P, F)    category_id (P, F)
     16                 1
     16                 2
如果您的
wall\u id
来自另一个表,请跳过
get last
步骤。并通过从另一个表中获取的
wall\u id
变量删除/更改
$next\u wall\u id


如果你想做的话,就这些了。有问题吗?发现了一些错误?下面的注释:D

对我来说非常有效的代码,使用了last_insert_id,因为我确定我需要将最新的id插入到我的wall_categories表中。非常感谢@Oki Erie Rinaldi

<?php
$con = mysqli_connect("host","username","password","database_name");
//First get the category from input
$checked = $_REQUEST['category']; // it's an array

//--------get last
    //Then you have to get the last wall_id from your wall_categories table
    //and add it 1 (+1)
    $q1 = mysqli_query($con,"SELECT MAX(wall_id) as last_wall_id from wall_categories"); // get last wall_id
    $last = mysqli_fetch_array($q1);
    $next_wall_id = $last['last_wall_id']+1;//+1 last wall_id for next wall_id
//-------end of get last

foreach($checked as $cat){
mysqli_query($con,"INSERT INTO wall_categories (wall_id, category_id) values ('$next_wall_id','$cat') ");
}
?>

对我来说效果很好的代码使用了last_insert_id,因为我确信我需要将最新的id插入到我的wall_categories表中。非常感谢@Oki Erie Rinaldi

<?php
$con = mysqli_connect("host","username","password","database_name");
//First get the category from input
$checked = $_REQUEST['category']; // it's an array

//--------get last
    //Then you have to get the last wall_id from your wall_categories table
    //and add it 1 (+1)
    $q1 = mysqli_query($con,"SELECT MAX(wall_id) as last_wall_id from wall_categories"); // get last wall_id
    $last = mysqli_fetch_array($q1);
    $next_wall_id = $last['last_wall_id']+1;//+1 last wall_id for next wall_id
//-------end of get last

foreach($checked as $cat){
mysqli_query($con,"INSERT INTO wall_categories (wall_id, category_id) values ('$next_wall_id','$cat') ");
}
?>

您想要的流程是什么?您的意思是流程?是否要将类别插入数据库?或者怎样做?我的数据库中已经有了
类别
表。我想同时插入类别id和wall_id以插入我的
wall_categories
表中。我认为您应该将输入名称更改为相同的名称,并使其具有不同的值。您想要的过程是什么?您的意思是“过程”吗?是否要将类别插入数据库?或者怎样做?我的数据库中已经有了
类别
表。我想同时插入类别id和墙id以插入我的
wall\u categories
表。我认为您应该将输入名称更改为相同的名称并使其具有不同的值。我想我也需要您的帮助。哈哈。我想我也需要你的帮助。哈哈。
<?php
$con = mysqli_connect("host","username","password","database_name");
//First get the category from input
$checked = $_REQUEST['category']; // it's an array

//--------get last
    //Then you have to get the last wall_id from your wall_categories table
    //and add it 1 (+1)
    $q1 = mysqli_query($con,"SELECT MAX(wall_id) as last_wall_id from wall_categories"); // get last wall_id
    $last = mysqli_fetch_array($q1);
    $next_wall_id = $last['last_wall_id']+1;//+1 last wall_id for next wall_id
//-------end of get last

foreach($checked as $cat){
mysqli_query($con,"INSERT INTO wall_categories (wall_id, category_id) values ('$next_wall_id','$cat') ");
}
?>
<?php
$con = mysqli_connect("host","username","password","database_name");
//First get the category from input
$checked = $_REQUEST['category']; // it's an array

$wallId = mysqli_insert_id($con); //retrieves the last ID.

foreach($checked as $cat){
mysqli_query($con,"INSERT INTO wall_categories (wall_id, category_id) values ('$wallId','$cat') ");
}
?>