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

时间:2016-04-30 04:47:09

标签: php html mysql wamp

我正在尝试创建一个带名称,dob等的简单数据库。我使用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中的firstname在线   5

     

注意:未定义的索引:第6行的C:\ xampp \ htdocs \ bio.php中的姓氏

     

注意:未定义的索引:C:\ xampp \ htdocs \ bio.php中的mothername在线   7

     

注意:未定义的索引:C:\ xampp \ htdocs \ bio.php中的fathersname   第8行

     

注意:未定义的索引:第9行的C:\ xampp \ htdocs \ bio.php中的性别

     

注意:未定义的索引:第10行的C:\ xampp \ htdocs \ bio.php中的日期

     

注意:未定义的索引:第11行的C:\ xampp \ htdocs \ bio.php中的月份

     

注意:未定义的索引:第12行的C:\ xampp \ htdocs \ bio.php中的年份

     

注意:未定义的变量:C:\ xampp \ htdocs \ bio.php中的FirtsName   第15行

     

未选择数据库

我不明白有什么不对?请帮忙。

2 个答案:

答案 0 :(得分:0)

注意:未定义的索引信息,你应该在调用时用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:)

答案 1 :(得分:0)

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

用于获取数据的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");


?>