简体   繁体   English

使用PHP存储在MySQL中并返回输入值的表单输入

[英]Form Entry Using PHP to Store in MySQL and Return Entered Value

This is my first post, so I apologize if it's a repeat. 这是我的第一篇文章,如果再次重复,我深表歉意。 I'm at a bit of a loss here and can't figure out why the information I've entered isn't showing when I submit it to the form. 我在这里有些茫然,无法弄清楚为什么我将输入的信息提交到表单后却没有显示。 It's only my second week using PHP, but I'm trying to have the info that is entered into the form stored in MySQL and show up on the user's screen. 这只是我第二周使用PHP,但是我试图将输入到表单中的信息存储在MySQL中并显示在用户屏幕上。

    <?php

$user = "root";
$pass = "root";
$dbh = new PDO('mysql:host=localhost; dbname=ssl; port=8889', $user, $pass);

if ($_SERVER['REQUEST_METHOD'] == 'POST'){
    $fruitname = $_POST['fruitname'];
    $fruitcolor = $_POST['fruitcolor'];
    $stmt = $dbh->prepare("INSERT INTO fruits (fruitname, fruitcolor) VALUES (:fruitname, :fruitcolor);");

    $stmt->bindParam(':fruitname', $fruitname);
    $stmt->bindParam(':fruitcolor', $fruitcolor);
    $stmt->execute();
}
?>

The following is the HTML for the table I've set up to display the information 以下是我为显示信息而设置的表的HTML

<!-----HTML FORM ENTRY----->
    
    
<!DOCTYPE html>
    

<html lang="en">
    

<head>
    
    
    <meta charset="UTF-8">
    
    <title>Fruit Database Application</title>
    
    <link rel="stylesheet" type="text/css" href="css/styles.css">
    
    
</head>

    
    <body>

    
    <h1>Database Test</h1>

    
    <section class="formStyle">
    
    
    <form action="fruits.php" method="post">
    
        <label>
    
            <b>Fruit Name:</b><input type="text" name="fruitname" value="" required>
    
        </label><br/><br/>
    
        <label><b>Fruit Color:</b><input type="text" name="fruitcolor" value="" required>
    
        </label><br/><br/>
    
        <input type="submit" name="submit" value="Submit">
    
    </form>
    
    </section>
    
    <br/>
    
    <section class="tableStyle">
    
    
    <table>
    
        <thead>
    
        <tr>
    
            <th>Fruit ID</th>
    
            <th>Fruit Name</th>
    
            <th>Fruit Color</th>
    
            <th>Action</th>
    
        </tr>
    
        </thead>
    
    
        <tbody>
 

And this is the last bit of PHP entered into the table 这是表中最后输入的PHP

<?php
    $stmt = $dbh->prepare('SELECT * FROM fruits order by fruitId ASC;');

    $stmt->execute();
    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
    foreach ($result as $row){
        echo '<tr><td>' . $row['fruitId'] . '</td><td>' . $row['fruitName'] . '</td><td>' . $row['fruitColor'] . '</td><td><a href="deletefruit.php?id=' . $row['fruitId'] . '">Delete</a></td>';
    }
    ?>


    </tbody>
    </table>
</section>
</body>
</html>

What I can't seem to get to show up is the info entered into the form. 我似乎无法露面的是在表单中输入的信息。 If I submit the name and color, and refresh my SQL database, the info shows up, so it's entering it correctly. 如果我提交名称和颜色,并刷新我的SQL数据库,则会显示该信息,因此它输入正确。 Any assistance or pointers would be greatly appreciated! 任何帮助或指针将不胜感激!

If you are using os different then windows , than mysql identifiers are case sensitive :) , since your addition to table went well, they're lowercase 如果您使用的操作系统不同于Windows,则mysql标识符区分大小写:),因为您对表的添加顺利进行,因此它们是小写的

Here is code for retrieving info corrected : 这是用于检索已更正信息的代码:

<?php
    $stmt = $dbh->prepare('SELECT * FROM fruits order by fruitId ASC;');

    $stmt->execute();
    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
    foreach ($result as $row){
        echo '<tr><td>' . $row['fruitid'] . '</td><td>' . $row['fruitname'] . '</td><td>' . $row['fruitcolor'] . '</td><td><a href="deletefruit.php?id=' . $row['fruitid'] . '">Delete</a></td></tr>';
    }
    ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM