【发布时间】:2016-04-30 04:47:09
【问题描述】:
我正在尝试创建一个包含姓名、出生日期等的简单数据库。我使用 HTML 表单从用户那里获取数据,如下所示:
<!DOCTYPE html>
<head>
<title>BIODATA</title>
</head>
<style>
form{
width:500px;
margin: 0 auto;
}
</style>
<body>
<h1><b><center>ASSIGNMENT Operating Systems Lab</center></b></h1>
<h2><b><center><u>BIODATA</u></center></b></h2>
<h3><b><center>Roll no : EPANECS042</center></b></h3>
<br>
<br>
<br>
<form action="bio.php" method="post" >
First name:<br>
<input type="text" name="firstname">
<br>
Last name:<br>
<input type="text" name="lastname">
<br>
Mother's Name:<br>
<input type="text" name="mothername">
<br>
Father's name:<br>
<input type="text" name="fathername">
<br><br>
Date of Birth:<br>
Day<input type="number" name="day" min="1" max="31">
Month<input type="number" name="month" min="1" max="12">
Year<input type="number" name="year" min="1990" max="2050">
<br><br>
<input type="radio" name="gender" value="male" checked> Male<br>
<input type="radio" name="gender" value="female"> Female<br>
<input type="radio" name="gender" value="other"> Other<br>
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
我使用 php 向 MySQL 数据库输入数据。为此,我使用文件 bio.php 插入数据并使用 DbConnect.php 连接到数据库。
bio.php
<?php
include_once './DbConnect.php';
function createNewBio() {
$response = array();
$FirstName = $_POST["firstname"];
$LastName = $_POST["lastname"];
$MothersName = $_POST["mothername"];
$FathersName = $_POST["fathersname"];
$Gender = $_POST["gender"];
$Day = $_POST["day"];
$Month = $_POST["month"];
$Year = $_POST["year"];
//combining variables
$Name = $FirtsName.$LastName;
$DOB = strval($Day).strval($Month).strval($Year);
$Query = "INSERT INTO biodata VALUES ({$Name},{$MothersName},{$FathersName}, {$DOB},{$Gender})";
$Result = mysql_query($Query) or die(mysql_error());
if ($result) {
$response["error"] = false;
$response["message"] = "Registered Successfully!!";
$response["ID"] = $id;
} else {
$response["error"] = true;
$response["message"] = "Registration unsuccessfull!!";
}
echo json_encode($response);
}
createNewBio();
?>
DbConnect.php
<?php
class DbConnect {
private $conn;
function __construct() {
//connecting to database
$this->connect();
}
function __destruct() {
$this->close();
}
function connect() {
$this->conn = mysql_connect('127.0.0.1', 'root','') or die(mysql_error());
mysql_select_db('assignment') or die(mysql_error());
// returing connection resource
echo $this->conn;
return $this->conn;
}
// Close function
function close() {
// close db connection
mysql_close($this->conn);
}
}
?>
问题是我无法上传数据。我正在使用wamp。当我在浏览器中打开 bio.php 时,我得到以下信息:
注意:未定义索引:在线 C:\xampp\htdocs\bio.php 中的名字 5
注意:未定义索引:第 6 行 C:\xampp\htdocs\bio.php 中的姓氏
注意:未定义索引:在线 C:\xampp\htdocs\bio.php 中的母名 7
注意:未定义的索引:C:\xampp\htdocs\bio.php 中的fathersname 第 8 行
注意:未定义索引:C:\xampp\htdocs\bio.php 第 9 行中的性别
注意:未定义索引:第 10 行 C:\xampp\htdocs\bio.php 中的日期
注意:未定义索引:C:\xampp\htdocs\bio.php 第 11 行中的月份
注意:未定义索引:C:\xampp\htdocs\bio.php 第 12 行中的年份
注意:未定义变量:C:\xampp\htdocs\bio.php 中的 FirtsName 第 15 行
没有选择数据库
我不明白这是怎么回事?请帮忙。
【问题讨论】:
-
没有提交表单就打开
bio.php了吗???