好吧,所以我试图在index.php 上将用户的名字显示为问候消息,例如“你好,(用户的名字)”,但我对所有这些 php 内容都很陌生,所以我真的无法弄清楚这个怎么做。我尝试遵循一个简短的教程,但是每当我尝试使用 'print $_POST['first_name'] 打印名字时,我都会收到 undefined index 错误......可能是一个愚蠢的问题,但我只是无法弄清楚。
这是我的 db_connect.php(如果它很重要):
<?php
$host = "localhost";
$user = "root";
$pass = "";
$db = "Byt til Nyt";
$ms = mysql_pconnect($host, $user, $pass);
if ( !$ms )
{
echo "Could not connect to database.";
}
mysql_select_db("$db");
?>
这是我的 register.php 的简短版本:
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Register</title>
<?php
include('includes/db_connect.php');
if (@$_GET["op"] == "reg") {
$bInputFlag = false;
foreach ($_POST as $field) {
if ($field == "") {
$bInputFlag = false;
} else {
$bInputFlag = true;
}
}
if ($bInputFlag == false) {
die("Der opstod et problem med dine oplysninger. Gå tilbage og prøv igen.");
}
$q = "INSERT INTO 'users' ('first_name','last_name','email','username','password')" . "VALUES ('" . $_POST["username"] . "'," . "PASSWORD ('" . $_POST["password"] . "')," . "'" . $_POST["email"] . "')";
$r = mysql_query($q);
if (!mysql_insert_id()) {
die("Der opstod en fejl. Du er ikke blevet tilføjet til databasen.");
} else {
Header("Location: register.php?op=thanks");
}
} elseif (@$_GET["op"] == "thanks") {
echo "<h2>Tak fordi du registrerede!</h2>";
}
?>
</head>
<body>
<form method="post" action="includes/verify.php" name="register">
//recaptcha script here
<label>First name</label>
<input type="text" name="first_name" placeholder="Your first name" required><br>
<label>Last name</label>
<input type="text" name="last_name" placeholder="Your last name" required><br>
//some more input fields and stuff
这是我的 index.php 的简短版本:
<?php
session_start();
include("includes/functions.php");
include("includes/db_connect.php");
?>
<!DOCTYPE html>
<html lang="dk">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<head>
<title>
Byt til Brugt
</title>
<link rel="stylesheet" href="index.css">
</head>
<body background="http://i.imgur.com/urLzQr5.jpg" bgcolor="navy">
<div class="menu">
<p>Velkommen, <?php if (isset($username)) {
print $_POST['first_name'];
}
?>
</p>
所以,问题出在哪里? :/任何帮助将非常感激,我已经尝试解决这个问题两天了......只要说你是否需要我的login.php或其他任何东西(:非常感谢!对我糟糕的英语感到抱歉。 .
最佳答案
$_POST['first_name']
可能没有设置,请尝试
<?php if (isset($_POST['first_name'])) {
print $_POST['first_name'];
}
?>
关于php - 在屏幕上显示用户的用户名。出现 'Undefined Index' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24325852/