如何从表格数据生成列表

问题描述 投票:0回答:1

我是 php 和 mysql 的新手。我正在尝试创建一个页面,该页面将从表中检索数据并将其显示为列表。

抱歉,我刚刚开始自学php和mysql。如果有人可以帮助加快我的学习速度,我将非常感激。

  1. 这是我制作的表单(form.php)并且它正在工作。
<!DOCTYPE html>
<html lang="en">
<head>
<title>enter data</title>
<style type="text/css">
.db1 {
    display: block;
    margin: 0%;
    padding: 10px;
}
.db1 input {
    width: 100%;
    margin-bottom: 10px;
}
.db1 textarea {
    width: 100%;
    padding-bottom: 10px;
}
.db1 form {
    width: 100%;
    margin: 0px;
}
.wrapper {
    max-width: 600px;
    margin: 0px;
}
</style>
</head>
<body>
<div class="wrapper">
  <h1>Data to Database</h1>
  <form class="db1" action="insert.php" method="post">
    <p>
      <label for="firstName">First Name:</label>
      <input type="text" name="first_name" id="firstName" required>
    </p>
    <p>
      <label for="lastName">Last Name:</label>
      <input type="text" name="last_name" id="lastName" required>
    </p>
    <p>
      <label for="phoneNumber">Phone Number:</label>
      <input type="text" name="phone_number" id="phoneNumber" placeholder="+639012345678" required>
    </p>
    <p>
      <label for="emailAddress">Email Address:</label>
      <input type="text" name="email_address" id="emailAddress" placeholder="[email protected]">
    </p>
    <input type="submit" value="Submit">
  </form>
</div>
</body>
</html>
  1. 这是表单提交(confirm.php)确认:
<!DOCTYPE html>
<html>
 
<head>
    <title>confirm page</title>
</head>
 
<body>
    <center>
        <?php
 
        // servername => localhost
        // username => root
        // password => empty
        // database name => db1
        $conn = mysqli_connect("localhost", "root", "", "db1");
         
        // Check connection
        if($conn === false){
            die("ERROR: Could not connect. "
                . mysqli_connect_error());
        }
         
        // Taking all 5 values from the form data(input)
        $first_name =  $_REQUEST['first_name'];
        $last_name = $_REQUEST['last_name'];
        $phone_number = $_REQUEST['phone_number'];
        $email_address = $_REQUEST['email_address'];
         
        // Performing insert query execution
        // here our table name aem_contacts
        $sql = "INSERT INTO aem_contacts VALUES ('$first_name','$last_name','$phone_number','$email_address')";
         
        if(mysqli_query($conn, $sql)){
            echo "<h3>Submitted successfully</h3>"; 
 
            echo nl2br("\n$first_name $last_name\n  +63 $phone_number\n $email_address");
        } else{
            echo "ERROR: Hush! Sorry $sql. "
                . mysqli_error($conn);
        }
         
        // Close connection
        mysqli_close($conn);
        ?>
    </center>
</body>
 
</html>
  1. 我想创建另一个页面(list.php),每次从 form.php 输入新数据集时都会显示该页面,即

提交数据:

  1. 名字和姓氏 1
  2. 名字和姓氏 2
  3. 名字和姓氏 3
database list listview arraylist linked-list
1个回答
0
投票

对于您的 php,您需要连接到您的数据库,

这是我的 php 和 html,当我在数据中搜索某些内容并找到它后,它会显示它

<?php
$servername = "localhost";
$username = "test";
$password = "1234";
$dbname = "inventaire";

try {
    $connexion = new PDO("mysql:host=$servername;dbname=$dbname;charset=utf8", $username, $password);
    $connexion->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $error) {
    echo "Erreur de connexion: " . $error->getMessage();
    exit;
}

$resultatRecherche = null;

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

    $sql = "SELECT * FROM voitures WHERE modele = :modele";
    $stmt = $connexion->prepare($sql);
    $stmt->bindValue(':modele', $modeleRecherche, PDO::PARAM_STR);
    $stmt->execute();

    if ($stmt->rowCount() > 0) {
        $resultatRecherche = $stmt->fetch(PDO::FETCH_ASSOC);
    } else {
        $resultatRecherche = false;
    }
}

$connexion = null;
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <title>Recherche de voiture</title>
    <meta charset="utf-8" />
    <link rel="stylesheet" href="css/styles.css">
</head>
<body>
<h1>Recherche de voiture</h1>

<form method="post" action="">
    <div>
        <label for="txtModeleRecherche">Modèle:</label>
        <input type="text" name="txtModeleRecherche" id="txtModeleRecherche" required />
        <input type="submit" value="Recherche" />
    </div>
</form>

<?php if ($_SERVER["REQUEST_METHOD"] == "POST" && $resultatRecherche): ?>
    <h2>Détails de la voiture</h2>
    <form method="post" action="">
        <div>
            <label for="txtFabricant">Fabricant:</label>
            <input type="text" name="txtFabricant" id="txtFabricant" value="<?php echo htmlspecialchars($resultatRecherche['Fabricant']); ?>" required />
        </div>
        <div>
            <label for="txtModele">Modèle:</label>
            <input type="text" name="txtModele" id="txtModele" value="<?php echo htmlspecialchars($resultatRecherche['Modele']); ?>" required />
        </div>
        <div>
            <label for="txtCouleur">Couleur:</label>
            <input type="text" name="txtCouleur" id="txtCouleur" value="<?php echo htmlspecialchars($resultatRecherche['Couleur']); ?>" required />
        </div>
        <div>
            <label for="txtPrix">Prix:</label>
            <input type="text" name="txtPrix" id="txtPrix" value="<?php echo htmlspecialchars($resultatRecherche['Prix']); ?>" required />
        </div>
        <div>
            <input type="submit" value="Modifier" />
        </div>
    </form>
<?php elseif ($_SERVER["REQUEST_METHOD"] == "POST"): ?>
    <p>Aucune voiture trouvée !</p>
<?php endif; ?>
[enter image description here][1]
</body>
</html>

我已经给出了它所显示的结果

© www.soinside.com 2019 - 2024. All rights reserved.