php - 需要帮助将 ID 值从一个表插入到 PHP 中的下一个表中

标签 php html mysql

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

我有 table 艺术家 流派 轨道 跟踪评级

在轨道中我有 trackID(设置为 autoIncrement)、genreID、artistID、title

我可以插入一首轨道并且工作正常问题是当我插入一首轨道时 我还想插入与该轨道关联的评级

在 track_rating 我有 rating_id(AutoInc), track_id, rating_number, total_points

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

这是我的代码

add_track_form

<?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');
}
?>

最佳答案

$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();

我从你的代码中添加了 $latestID = $statement->inser_id(); 这就是获取最新 ID 的代码,如果你使用准备好的 stament 然后我们使用变量 $latestID 在您的 track_rating 表中插入 track_ID..

关于php - 需要帮助将 ID 值从一个表插入到 PHP 中的下一个表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40850162/

相关文章:

php - 从php和mysql如何在html文件中加载和插入数百万条记录

javascript - 服务器在 ajax 提交时给出 403 错误

MySql 仅从父键有多个值的子表中获取记录

java - 在java中关闭MySQL数据库连接

php - 如何在 Laravel 5 中更新一对多(hasMany)关系

PHP 无法正常运行

html - 将占位符居中并在文本字段中输入文本

php - 如何使用内连接更新 codeigniter 中的表?

php - 如何从 PHP 中的子类别获取父节点?

html - 将背景图像应用于引导列网格布局