php - 无法将数据上传到 wamp localhost 中的数据库

标签 php html mysql wamp

我正在尝试创建一个简单的数据库,它需要姓名、出生日期等。我使用 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 时,我得到以下信息:

Notice: Undefined index: firstname in C:\xampp\htdocs\bio.php on line 5

Notice: Undefined index: lastname in C:\xampp\htdocs\bio.php on line 6

Notice: Undefined index: mothername in C:\xampp\htdocs\bio.php on line 7

Notice: Undefined index: fathersname in C:\xampp\htdocs\bio.php on line 8

Notice: Undefined index: gender in C:\xampp\htdocs\bio.php on line 9

Notice: Undefined index: day in C:\xampp\htdocs\bio.php on line 10

Notice: Undefined index: month in C:\xampp\htdocs\bio.php on line 11

Notice: Undefined index: year in C:\xampp\htdocs\bio.php on line 12

Notice: Undefined variable: FirtsName in C:\xampp\htdocs\bio.php on line 15

No database selected

我没明白哪里出了问题?请帮忙。

最佳答案

注意: undefined index 信息,您应该在调用时使用 if 条件包装 createNewBio() 函数,例如:

if (isset($_POST['submit'])) {
    //Run the function inside this 
    createNewBio(); 
}

未选择数据库,请尝试:

$con = mysql_connect('xxx', 'xxx', 'xxx') or die (mysql_error());
$db  = mysql_select_db("database", $con);

另外,尽量避免使用 mysql,因为它已被弃用,请使用 mysqli 代替:)

关于php - 无法将数据上传到 wamp localhost 中的数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36950863/

相关文章:

php - 使用元标记和 PHP 重定向页面

php - 在 Ubuntu 中运行 PHP 程序 - fopen ("filename", "w") : Failed to open stream: Permission denied

javascript - 增加 SVG 边框的可点击区域

PHP if 语句在登录页面中使用

mysql - PHP;原产地保护组织;数据库; INSERT 查询不接受输入

php - $stmt -> execute() 返回 false

php - 在表中显示数据以进行更新

javascript - 下划线在鼠标悬停时展开

javascript - 单击元素时导航栏下拉列表的标题不会更改

php - MySQL导入Excel工作表的奇怪问题