我使用php创建了注册表单。我有index.php,submit.php,functions.php和script。
现在它工作正常,我需要从数据库中检索数据并在前端显示。
我是php新手,我正在学习。请帮我这样做。
这是我的data.php:
<?php
include ('condb.php');
$query = mysql_query("select * from test where id='".$_GET['id']."'");
$row = mysql_fetch_array($query);
?>
<body>
<h3>Employee Detail</h3>
<p>Emp Name:</p> <?php echo $row['fname'];?>
<p>LastName:</p> <?php echo $row['lname'];?>
</body>
<?php ?>
这是action.php:
<?php
include('condb.php');
extract($_POST);
$que=mysql_query("INSERT INTO test (id, fname, lname) VALUES ('$id', '$fname', '$lname')") or die("Error Msg!");
if($que)
{
header('Location: data.php?id='.$id.'');
}
?>
的index.html:
<body bgcolor="#2E2E2E">
<table align="center" cellpadding="5" style="background:#A4A4A4; border-radius:4px; margin-top:40px; padding:15px;">
<h1 style="text-align:center; color:white; margin-top:50px">Employee Registration Form</h1>
<form method="post" action="action.php">
<tr><td>First Name:</td><td> <input type="text" id="fname" name="fname" required /></td></tr>
<tr><td>Last Name:</td><td> <input type="text" id="lname" name="lname"/></td></tr>
<tr><td></td><td><input type="submit" value="Register Me!"/></td></tr>
</form>
</table>
</body>
和condb.php:
<?php
$con=mysql_connect("localhost","root","")or die("Error!");
mysql_select_db("test") or die(mysql_error());
?>
答案 0 :(得分:2)
这里我显示了3字段的代码,您可以对其他字段执行类似的操作。您可以使用 newfile 并粘贴该代码。
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT fname, lname, email FROM crop';
mysql_select_db('crop');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "First Name :{$row['fname']} <br> ".
"Last Name : {$row['lname']} <br> ".
"Email Address : {$row['email']} <br> ".
"--------------------------------<br>";
}
echo "Fetched data successfully\n";
mysql_close($conn);
?>
答案 1 :(得分:1)
您必须使用select语句从数据库中检索数据。 从数据库中选择数据后,您必须获取数据以使其有用。
例如:
$queryResult = mysql_query("SELECT * FROM crop");
$result = mysql_fetch_assoc($queryResult);
当您打算打印结果时,您将看到数据库中的所有选定数据
print_r($result);
答案 2 :(得分:1)
<?php
include 'config.php';
error_reporting(E_ERROR);
session_start();
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$email=$_POST['email'];
$pass=$_POST['pass'];
$phone=$_POST['phone'];
$sex_select=$_POST['sex_select'];
$month=$_POST['month'];
$day=$_POST['day'];
$year=$_POST['year'];
$result = mysql_query("INSERT INTO crop(fname, lname, email, pass, phone,`sex_select`, month,day,year)
VALUES ('$fname', '$lname', '$email', '$pass','$phone','$sex_select', '$month','$day','$year')");
if(mysql_affected_rows()>1)
{
//it will redeirect you to. your any file.
//you can give any file name here.
$_SESSION['fname']=$fname;//you are adding value to the session
$_SESSION['lanme']=$lname; //you are adding value to the session
//Do this for rest of your value
header('Location:Your_File_name.php');
}
if (!$result) {
die(msg(0,"wrong query"));
}
?>
在您重定向的文件中。你没有重定向。 如果你不想重定向不重定向。但要理解这个概念 你可以在start_start()之后的任何地方访问这些$ _SESSION;在任何文件
session_start();
//now you have access to all those value. which you stored in $_SESSION
echo $_SESSION['fname'];
echo $_SESSION['lanme'];
//In short which ever value you store in $_SESSSION you can echo them
//out on any page.
在该文件中
<?php
//lets say there is one more file you want to do
//that
//do the same thing
session_start();
echo $_SESSION['fname'];
echo $_SESSION['lanme'];
答案 3 :(得分:-1)
Markkh回答你,但也要确保你从$ _POST中逃脱输入,否则你会遇到SQL注入问题。 php.net/mysql_real_escape_string