【问题标题】:Unable to upload data to database in wamp localhost无法将数据上传到 wamp localhost 中的数据库
【发布时间】: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了吗???

标签: php html mysql wamp


【解决方案1】:

注意:未定义的索引信息,调用时应将 createNewBio() 函数包装为 if 条件,如:

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 :)

【讨论】:

    【解决方案2】:

    首先你为什么每次都创建函数。使用这个编码:

    获取数据的 HTML 表单:

    <form  action="bio.php" style="display:none" method="post">
    
    
    
    <table>
    <tr>
    <td>First Name</td>
    <td>
        <input type="text" name="firstname">
        </td>
        </tr>
    
        <tr>
        <td>Last name:</td>
        <td>
        <input type="text" name="lastname">
        </td>
        </tr>
        <tr>
        <td> Mother's Name:</td>
        <td> <input type="text" name="mothername"></td>
        </tr>
        <tr>
        <td>Father's name:</td>
        <td><input type="text" name="fathername"></td>
        </tr>
        <tr>
        <td>Date of Birth:</td>
        <td>Day</td>
        <td><input type="number" name="day" min="1" max="31"></td>
        <td>Month</td>
        <td><input type="number" name="month" min="1" max="12"></td>
        <td>Year</td>
        <td><input type="number" name="year" min="1990" max="2050"></td>
        </tr>
    
       <tr>
        <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>
       </tr>
    
        </table>
         <input type="submit" value="save" name="save" >
        </form>
    

    bio.php

        <?php
        error_reporting( error_reporting() & ~E_NOTICE );
        session_start();
        include 'DbConnect.php';
    
       $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"];
    
     $Name = $FirtsName.$LastName;
        $DOB = strval($Day).strval($Month).strval($Year);
    
        $result=mysql_query("insert into biodata values('$Name','$MothersName','$FathersName','$DOB','$Gender')");
    
    
    
        if($result)
        {   
        $response["error"] = false;
            $response["message"] = "Registered Successfully!!";
            $response["ID"] = $id;
        ?>
    
        <?php
        }
        else
        {
    
         $response["error"] = true;
            $response["message"] = "Registration unsuccessfull!!";
    
        }
    echo json_encode($response);
    
        ?>
    

    DbConnect.php

    <?php
    
    mysql_connect("localhost", "root", "")or die("cannot connect to server"); 
    mysql_select_db("assignment")or die("cannot select DB");
    
    
    ?>
    

    【讨论】:

    • 仍然无法将数据上传到数据库。
    • 没有错误,只是在按下提交时页面被定向到 bio.php 但没有输入数据。同时,如果只是去 localhost/bio.php 它会显示响应消息,因为在这种情况下没有数据可以输入到数据库中,只会发生自动增量。
    猜你喜欢
    • 1970-01-01
    • 2014-12-06
    • 2019-04-23
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    • 1970-01-01
    • 1970-01-01
    • 2020-08-08
    相关资源
    最近更新 更多