samedi 25 avril 2015

sending information from a newly created record to a different mysql table


I'm making a form that submits a story into a mysql table called 'work'. I want to later take the id of the newly created record and put the information into a different table. But when I submit the story, it says $workid is undefined. I can't see the problem though because I believe I've defined it?

<?php
            if (!empty($_POST) && !empty($_POST['title']) && !empty($_POST['story']) && !empty($_POST['genre']) && !empty($_POST['rating'])) { 
                $title = strip_tags($_POST['title']);
                $story = strip_tags($_POST['story']);
                $title = mysqli_real_escape_string($db, $title);
                $story = mysqli_real_escape_string($db, $story);
                $genre = $_POST['genre'];
                $rating = $_POST['rating'];

                $query = "SELECT COUNT(*) AS count FROM works WHERE Title = '".$title."'";
                 $result = $db->query($query);
                 $data = $result->fetch_assoc();

                 if ($data['count'] > 0) {
                     echo "<p>Story already exists!</p>";
                     } else {
                         $query = "INSERT INTO works (author_id, login_id, Title, Story, Genre, Rating) VALUES ('".$userid."','".$authorid."','".$title."','".$story."','".$genre."','".$rating."')";

                        $query="SELECT `id` FROM `works` WHERE `Title` = '".$title."'";
                        if ($result = $db->query($query)) {
                        while ($row= $result->fetch_assoc()) 
                        $workid = $row["id"];} //workid is written here but still considered undefined


                        $query = "INSERT INTO `author_work` (`author_id`) VALUES ('".$authorid."')";
                        $result = $db->query($query);

                        $query = "INSERT INTO `author_work` (`work_id`) VALUES ('".$workid."')";
                        $result = $db->query($query);

                        $query = "INSERT INTO `login_work` (`work_id`) VALUES ('".$workid."')";
                        $result = $db->query($query);


                        $query = "INSERT INTO `login_work` (`login_id`) VALUES ('".$userid."')";
                        $result = $db->query($query);                           


            if ($result) {
              echo "<p>Story submitted!</p>";
            } else {
                echo "SQL Error: " . $db->error;
            }
            }
            }
        ?>


Aucun commentaire:

Enregistrer un commentaire