PHP - 表单数据到localhost数据库

时间:2016-05-14 11:25:13

标签: php html mysql xampp localhost

错误: 提交表单时屏幕变为空白,数据库中没有新记录。有什么想法吗?

我的档案:

Conn.php

    <?php


$connect =mysqli_connect ("localhost", "root", "", "gym");


if(mysqli_connect_errno($connect))
{
    echo "Error Connecting to Database!";
}


?>

form.php的

<html>

<head>

    <title>Gym Form</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">


</head>

<body>

    <form action="Proccess.php" method="post">

    <span>Gym Membership Registration</span><br><br>

    <Span>Title: </Span><input type ="text" Value =" " name ="Title" /><br>

    <Span>First Name: </Span><input type ="text" Value =" " name ="Fname" /><br>

    <Span>Last Name: </Span><input type ="text" Value =" " name ="Lname" /><br><br>

    <Span>Gender: </Span><select name ="Gender">

        <option value ="Junior">Male</option>
        <option value ="Adult">Female</option>
        <option value ="Senior">Private</option>

    </select><br>

    <Span>DOB: </Span><input type ="date" name ="DOB" /><br><br>

    <Span>MembershipExpiry: </Span> <input type ="date" name ="MemX" /><br>

    <Span>MembershipType: </Span><select name = "MemType">

        <option value ="Junior">Junior</option>
        <option value ="Adult">Adult</option>
        <option value ="Senior">Senior</option>

    </select><br><br>

    <Span>Email Address: </Span><input type ="email" name ="Email" /><br><br>

    <input type="Submit" name="submit" value ="Submit Form">

Proccess.php(错误似乎是):

<?php

include 'Connect.php';?>

<?php

//variables

$title = $_POST['Title'];
$fname = $_POST['Fname'];
$lname = $_POST['Lname'];
$gender = $_POST['Gender'];
$dob = $_POST['DOB'];
$memx = $_POST['MemX'];
$memtype = $_POST['MemType'];
$email = $_POST['Email'];

//query

mysqli_query($connect, "INSERT INTO records(Title,Fname,Lname,Gender,DOB,MemX,MemType,Email) 
values ('$title', '$fname', '$lname', '$gender', '$dob', '$memx', '$memtype', '$email')");

错误: 提交表单时屏幕变为空白,数据库中没有新记录。有什么想法吗?

数据库表称为记录并具有列:

Title
First Name
Last Name 
Gender
etc

请帮我连接并将表单数据上传到db。

2 个答案:

答案 0 :(得分:1)

尝试添加&#39;,&#39;在$ connect之后:

mysqli_query($connect, "INSERT INTO records(Title,Fname,Lname,Gender,DOB,MemX,MemType,Email) 
values ('$title', '$fname', '$lname', '$gender', '$dob', '$memx', '$memtype', '$email')");

答案 1 :(得分:0)

connection.php

<?php
$connect = mysqli_connect ("localhost", "root", "", "gym") or die('Error Connecting to Database!');
?>

process.php

   
<?php

//variables

$title = $_POST['Title'];
$fname = $_POST['Fname'];
$lname = $_POST['Lname'];
$gender = $_POST['Gender'];
$dob = $_POST['DOB'];
$memx = $_POST['MemX'];
$memtype = $_POST['MemType'];
$email = $_POST['Email'];

//query

$sql = 'INSERT INTO records (Title, Fname, Lname, Gender, DOB, MemX, MemType, Email) VALUES ( ?,?,?,?,?,?)';

if (! $stmt = mysqli_prepare($connection, $sql)) {
trigger_error('Error: ' . mysqli_error($connection), E_USER_WARNING);
}

mysqli_stmt_bind_param($stmt, 'ssssssss', $title, $fname, $lname, $gender, $dob, $memx, $memtype, $email);

mysqli_stmt_execute($stmt);

mysqli_stmt_close($stmt);

mysqli_close($db);

redirect('Form.php');

查看http://php.net/manual/en/book.mysqli.php