需要帮助在PHP

时间:2016-11-28 17:29:16

标签: php html mysql

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

我有桌子 艺术家 流派 轨道 track_rating

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

我可以插入一个曲目,这很好用,插入曲目的麻烦就是 我还想插入与该曲目相关联的评级

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

我遇到的问题是track_id。我无法从track表中获取值以将其插入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>

add_track

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

1 个答案:

答案 0 :(得分:0)

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

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