首页 > 解决方案 > 将不同文件中的 php 索引传递到 html 表单输入字段

问题描述

当我在输入字段中调用它的值时,我无法正确显示我的变量。我没有包括表单中的其他输入字段,因为它们都有效。我只需要在页面加载时将用户昵称插入昵称字段。我已经看到了很多旧的方法,这些方法不再起作用,也无法治愈目前如何进行。

我尝试使用 $_POST、$_GET 传递 $nickname 会话,但仍然无法使其正常工作。

硬币提交.html

<form autocomplete="off" action="AdminCoinSub_Code.php" method="POST">
    <input type="text" name="Nickname" id="Nickname" placeholder="Nickname">
</form>

AdminCoinSub_Code.php

<?php
if (isset($_POST['Next'])) {
    $servername = "localhost";
    $username = "root";
    $password = "password";
    $dbname = "administrator_logins";

    try {
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        // set the PDO error mode to exception
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        // prepare sql and bind parameters
        $stmt = $conn->prepare("INSERT INTO coins (Store, Position, 
            Nickame, ContactNumber, MachineCount, CutOffDate, Coins, location) 
            VALUES 
            ('$_POST[Store]','$_POST[Position]',
            '$_POST[Nickame]','$_POST[ContactNumber]','$_POST[MachineCount]','$_POST[CutOffDate]',
            '$_POST[Coins]','$_POST[location]')");

        $stmt->bindParam(':Store', $Store);
        $stmt->bindParam(':Position', $Position);
        $stmt->bindParam(':Nickname', $Nickname);
        $stmt->bindParam(':ContactNumber', $ContactNumber);
        $stmt->bindParam(':MachineCount', $MachineCount);
        $stmt->bindParam(':CutOffDate', $CutOffDate);
        $stmt->bindParam(':Coins', $Coins);
        $stmt->bindParam(':location', $location);

        $stmt->execute();

        echo "Success: Go back";
    } catch (PDOException $e) {
        echo "Error: " . $e->getMessage();
    }
    $conn = null;
}

当共同提交页面加载用户的昵称时,应自动将其插入“昵称”字段。我在下面包含了数据库结构。

硬币

硬币

管理员登录

管理员登录

标签: phpforms

解决方案


您的准备调用无效。您应该使用参数,然后绑定您的 POST 数据。

$stmt = $conn->prepare("INSERT INTO coins (Store, Position, 
    Nickame, ContactNumber, MachineCount, CutOffDate, Coins, location) 
    VALUES (:Store,:Position, :Nickname,:ContactNumber,:MachineCount,:CutOffDate, :Coins,:location)");

$stmt->bindParam(':Store', $_POST['Store']);
$stmt->bindParam(':Position', $_POST['Position']);
$stmt->bindParam(':Nickname', $_POST['Nickname']);
$stmt->bindParam(':ContactNumber', $_POST['ContactNumber']);
$stmt->bindParam(':MachineCount', $_POST['MachineCount']);
$stmt->bindParam(':CutOffDate', $_POST['CutOffDate']);
$stmt->bindParam(':Coins', $_POST['Coins']);
$stmt->bindParam(':location', $_POST['location']);

$stmt->execute();


推荐阅读