需要帮助在PHP中将ID值从一个表插入下一个表吗

需要帮助在PHP中将ID值从一个表插入下一个表吗,php,html,mysql,Php,Html,Mysql,好的,我有一个数据库,可以存储歌曲信息和每首歌曲的星级 我有桌子 艺术家 体裁 轨道 轨道额定值 在曲目中,我有trackID(设置为自动递增)、genreID、artistID和title 我可以插入一个音轨,效果很好。问题是当我插入音轨时 我还想插入一个与该曲目相关的评分 在赛道等级中,我有等级id(AutoInc)、赛道id、等级编号、总分 我遇到的问题是track_id。我无法从tracks表中获取值以将其插入track_rating表 这是我的密码 添加曲目表单 <?php re

好的,我有一个数据库,可以存储歌曲信息和每首歌曲的星级

我有桌子 艺术家 体裁 轨道 轨道额定值

在曲目中,我有trackID(设置为自动递增)、genreID、artistID和title

我可以插入一个音轨,效果很好。问题是当我插入音轨时 我还想插入一个与该曲目相关的评分

在赛道等级中,我有等级id(AutoInc)、赛道id、等级编号、总分

我遇到的问题是track_id。我无法从tracks表中获取值以将其插入track_rating表

这是我的密码

添加曲目表单

<?php
require('includes/database.php');
$query = 'SELECT * FROM genres ORDER BY genreID';
$statement = $db->prepare($query);
$statement->execute();
$genres = $statement->fetchAll();
$statement->closeCursor();

$queryArtist = 'SELECT * FROM artists ORDER BY artistID';
$statementArtist = $db->prepare($queryArtist);
$statementArtist->execute();
$artists = $statementArtist->fetchAll();
$statementArtist->closeCursor();
?>
<!DOCTYPE html>
<html>

<!-- the head section -->
<head>
    <title>MixMatcher</title>
    <link rel="stylesheet" type="text/css" href="css/main.css">
</head>

<!-- the body section -->
<body>
    <header><h1>Tracks</h1></header>

    <main>
        <h1>Add Track</h1>
        <form action="add_track.php" method="post" id="add_track_form">

            <label>genre:</label>
            <select name="genre_id">
            <?php foreach ($genres as $genres) : ?>
                <option value="<?php echo $genres['genreID']; ?>">
                   <?php echo $genres['genreName']; ?>
                </option>
            <?php endforeach; ?>
            </select><br>

            <label>Artist:</label>
            <select name="artist_id">
            <?php foreach ($artists as $artists) : ?>
                <option value="<?php echo $artists['artistID']; ?>">
                   <?php echo $artists['artistName']; ?>
                </option>
            <?php endforeach; ?>
            </select><br>

            <label>Title:</label>
            <input type="text" name="title"><br>

            <label>Add Track</label>
            <input type="submit" value="Add track"><br>
        </form>
        <p><a href="index.php">View track List</a></p>
    </main>

    <footer>
        <p>&copy; <?php echo date("Y"); ?> MixMatcher, Inc.</p>
    </footer>
</body>
</html>

混音器
轨道
添加曲目
体裁:

艺术家:
标题:
添加曲目

&抄袭;混音器公司

添加曲目

    <?php
// Get the track data
$genre_id = filter_input(INPUT_POST, 'genre_id', FILTER_VALIDATE_INT);
$artist_id = filter_input(INPUT_POST, 'artist_id', FILTER_VALIDATE_INT);
$title = filter_input(INPUT_POST, 'title', FILTER_SANITIZE_STRING);

// Validate inputs
if ($genre_id == null || $genre_id == false ||
        $artist_id == null || $title == null ) {
    $error = "Invalid track data. Check all fields and try again.";
    include('database_error.php');
} else {
    require_once('includes/database.php');

    // Add the track to the database  
    $query = 'INSERT INTO tracks (genreID, artistID, title)
              VALUES (:genre_id, :artist_id, :title)';
    $statement = $db->prepare($query);
    $statement->bindValue(':genre_id', $genre_id);
    $statement->bindValue(':artist_id', $artist_id);
    $statement->bindValue(':title', $title);
    $statement->execute();
    $statement->closeCursor();



    $query1 = 'SELECT * FROM tracks ORDER BY trackID';
    $statement1 = $db->prepare($query1);
    $statement1->execute();
    $tracks = $statement->fetchAll();
    $statement->closeCursor();

    //Problem Here
    $trackID =  $tracks['trackID'];
    $queryRating = 'INSERT INTO track_rating (track_id, rating_number, total_points)
              VALUES (:track_id, :rating_number, :total_points)';
    $statementRating = $db->prepare($queryRating);
    $statementRating->bindValue(':track_id', $trackID);
    $statementRating->bindValue(':rating_number', 0);
    $statementRating->bindValue(':total_points', 0);
    $statementRating->execute();
    $statementRating->closeCursor();

    // Display the track List page
    include('index.php');
}
?>

从您的代码中,我添加了
$lastestID=$statement->inser_id()
这是获取最新id的代码,如果您使用准备好的stament,则在将曲目id插入您的曲目评分表时使用变量
$lastestID


从您的代码中,我添加了
$lastestID=$statement->inser_id()这是获取最新id的代码,如果您使用准备好的stament,则在将曲目id插入您的曲目评分表时使用变量
$lastestID

如果我错了,请纠正我。你想从曲目表中输入最新的trackID并插入到曲目等级吗?是的,如果我错了,那就是正确的。您想从曲目表中输入最新的trackID并插入到曲目等级吗?是的,就是这样
$query = 'INSERT INTO tracks (genreID, artistID, title)
              VALUES (:genre_id, :artist_id, :title)';
    $statement = $db->prepare($query);
    $statement->bindValue(':genre_id', $genre_id);
    $statement->bindValue(':artist_id', $artist_id);
    $statement->bindValue(':title', $title);
    $statement->execute();

    $lastestID = $statement->inser_id();

    $statement->closeCursor();