MySQL / PHP显示错误的id

时间:2017-06-29 20:59:23

标签: php mysql

我目前正在开展一个学校项目,我需要一些帮助。我正在为一个页面编写PHP / SQL代码,当用户提交表单时,运行的查询将循环显示用户文本输入以及与<select>下拉列表相关联的值。

(关于我的意思,请访问http://themanaclub.com/themarketplace2/themarketplace2.php

这是我的代码:

<?php
include_once ('connection2.php');

if (($_SERVER["REQUEST_METHOD"] == "POST") && (isset($_POST['card_catalog_form']))) {

    $card_name = mysqli_real_escape_string($conn, $_POST['card_name']);
    $card_label = mysqli_real_escape_string($conn, $_POST['card_genre']);
    $insert_card_genre_query = sprintf("INSERT into card_catalog (card_name, label_id) VALUES ('%s', %u)",
        $card_name,
        $card_label);
    $insert_card_genre = mysqli_query($conn, $insert_card_genre_query) or die (mysqli_error($conn));

    $last_record = mysqli_insert_id($conn);
}

$card_genre_query = "SELECT card_genre.genre_id, card_label from `card_genre` order by genre_id desc";
$card_genre = mysqli_query($conn, $card_genre_query) or die(mysqli_error($conn));

$get_card_genre_query = "SELECT card_catalog.id, card_name, label_id, card_label from card_catalog left join card_genre on label_id = genre_id";
$get_card_genre = mysqli_query($conn, $get_card_genre_query) or die(mysqli_error($conn));

?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>The Marketplace By The Mana Club</title>
    <link rel="stylesheet" type="text/css" href="stylesheets2/tmp2.css">
</head>
<body>

    <?php include('templatestuff2/top_of_tmp2.php'); ?>

    <main>
        <h1>Card Input Form:</h1>

        <form id="card_name_entry" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" enctype="multipart/form-data">
            <h4>What Card Are You Looking For?</h4>
            <textarea name="card_label" rows="5" cols="30" placeholder="Write the name of the card here"></textarea>
            <p>
                <select name="card_genre">
                    <?php while ($row_card_genre = mysqli_fetch_assoc($card_genre)) { ?>
                    <option value="<?php echo $row_card_genre['genre_id'];?>"><?php echo $row_card_genre['card_label'];?><?php echo $row_card_genre['card_name'];?></option>
                    <?php } ?>
                </select>
            </p>

            <?php $query = "SELECT label_id from `card_catalog` right join `card_genre` on genre_id"; ?>

            <p id="textareasubmit"><input type="submit"></p>
            <input type="hidden" name="card_catalog_form">
        </form>

        <section id="all_questions">
            <ul>
                <?php while ($row_card_genre = mysqli_fetch_assoc($get_card_genre)) { ?>
                <li><a href="otherphppages/card_detail.php?id=<?php echo $row_card_genre['id'];?>"><?php echo $row_card_genre['card_label'];?></a></li>
                <?php }
                    $row_card_genre = mysqli_data_seek($get_card_genre, 0);
                ?>
            </ul>
        </section>

        <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" enctype="multipart/form-data">

            <p id="deletethissubmit"><input type="submit" value="Delete This"></p>
            <input type="hidden" name="card_catalog_delete">
            <input type="hidden" name="genre_id" value="<?php echo $row_card_catalog['label_id'];?>"

        </form>

        <p>You're asking this at
            <?php
                date_default_timezone_set('America/New_York');
                echo date('g:i a T \o\n l, F j, Y');
            ?>
        </p>

        <p id="backtothemarketplace"><a href="themarketplace2.php">Back To The Marketplace</a></p>

        <?php

        /*if (isset($_POST['card_genre'])) {
            $query = "SELECT card_catalog.card_name, card_catalog.label_id, card_genre.genre_id, card_genre.card_label FROM card_catalog, card_genre WHERE card_genre.genre_id = ?";
            $stmt = mysqli_prepare($conn, $query);
            $stmt->bind_param('s', $_POST['card_genre']);
            $stmt->execute();
            $result = $stmt->get_result();

            while ($row = $result->fetch_assoc()) {
                echo $row['card_name']." - ".$row['label_id'];
                echo "<br />";
            }
        }*/

        ?>

    </main>

    <?php include('templatestuff2/bottom_of_tmp2.php'); ?>

</body>
</html>

如果有人有任何帮助或建设性的批评,我将不胜感激(我是一个PHP / SQL新手,所以我将获得所有可以获得的帮助)。

由于

1 个答案:

答案 0 :(得分:0)

用代码搞砸了,我修复了它。现在正在显示id,它将链接到正确的页面。

我的代码的所有错误都是我混淆了一些变量和ID。

谢谢大家的帮助。