我有两个表,我正在尝试加入一个新表。我有一个“用户”表,一个“任务”表,我有一个空白的“user_tasks”表。我正在尝试将它们加入“user_tasks”表。
这是我的表格。
users = user_id, first_name, last_name
tasks = task_id, task_description
user_tasks = ut_id, ut_user_id, ut_first_name, ut_last_name, ut_task_id, ut_task_description.
这是我的PHP& SQL查询。我不确定是什么问题。我得到一个未定义的索引。我是否需要在HTML表单上添加隐藏的输入?任何帮助将不胜感激。
<?php
if(isset($_POST['submit'])){
$user_id = $_POST['user_id'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$task_id = $_POST['task_id'];
$task_description = $_POST['task_description'];
$ut_id = $_POST['ut_id'];
$ut_user_id = $_POST['ut_user_id'];
$ut_first_name = $_POST['ut_first_name'];
$ut_last_name = $_POST['ut_last_name'];
$ut_task_id = $_POST['ut_task_id'];
$ut_task_description = $_POST['ut_task_description'];
$join_query = "SELECT users.user_id, users.first_name, users.last_name, tasks.task_id, tasks.task_description FROM users, tasks INNER JOIN user_tasks ON users.user_id = user_tasks.ut_id INNER JOIN user_tasks ON tasks.task_id = user_tasks.ut_id INSERT INTO user_tasks (ut_user_id, ut_first_name, ut_last_name, ut_task_id, ut_task_description) VALUES ($user_id, $first_name, '$last_name', '$task_id', $task_description) WHERE ('$first_name' = '$ut_first_name', '$last_name' = '$ut_last_name', $task_id = $ut_task_id, '$task_description' = '$ut_task_description') ";
$result = mysqli_query($connection, $join_query);
}
&GT;
答案 0 :(得分:0)
我认为你的选择中的表('用户,任务')是错误的,也许表是'user_tasks'
答案 1 :(得分:0)
您有两个连接的查询,请选择+插入
试试这个:
1- insert + select separate:
$join_query = "SELECT users.user_id, users.first_name,
users.last_name, tasks.task_id,
tasks.task_description FROM users
INNER JOIN user_tasks ON users.user_id = user_tasks.ut_user_id
/*users.user_id = user_tasks.ut_id*/
INNER JOIN tasks ON task.task_id=user_task.ut_task_id";
$result = mysqli_query($connection, $join_query);
AND Insert After,Insert不使用where:
$insert_query="INSERT INTO user_tasks (ut_user_id, ut_first_name, ut_last_name,
ut_task_id, ut_task_description)
VALUES ($user_id, '$first_name', '$last_name',
'$task_id', '$task_description')";
mysqli_query($connection,$insert_query) or die(mysqli_error($connection));
2- INSERT with SELECT:
$query="INSERT INTO user_tasks (ut_user_id, ut_first_name, ut_last_name,
ut_task_id, ut_task_description)
SELECT users.user_id, users.first_name,
users.last_name, tasks.task_id,
tasks.task_description FROM users
INNER JOIN user_tasks ON users.user_id = user_tasks.ut_user_id /*users.user_id = user_tasks.ut_id*/
INNER JOIN tasks ON task.task_id=user_task.ut_task_id";
mysqli_query($connection,$query) or die(mysqli_error($connection));