0

正如标题所示,我正在使用 PHP 和 SQL 创建一个注册表单。每当我单击注册按钮时,表格中的所有变量都会出现未定义的变量错误,数据库中也没有输入任何内容。请如果你能提出任何建议,我正在拔头发。

这是表单提交到的页面:

<?php
include('dbconfig.php');
?>

<?php

 echo $_GET["FirstName"]; 
?>

<?php

$db = new PDO('mysql:host=localhost;dbname=mydatabase;', 'root', '');

$firstname = $_POST['FirstName'];
$lastname  = $_POST['LastName'];
$username  = $_POST['username' ];
$password  = $_POST['Password'];
$email     = $_POST['Email'];
$startdate = $_POST['StartDate'];
$year      = $_POST['Year'];



$password = md5($password);



$sql = "INSERT into supervisor (FirstName, LastName, UserName, Password, Email,                      
Company) VALUES (:FirstName, :LastName, :UserName, :Password, :Email)";

$statement = $db->prepare($sql);

$params = array(
':FirstName' => $firstname,
':LastName'  => $lastname, 
':Password'  => $password,
':Email'     => $email,
':StartDate' => $startdate,
':Year'      => $year,
':Company'   => $company
);

dbconfig 文件:

<?php

$config['db'] = array(
'host'      => 'localhost',
'username'  => 'root',
'password'  =>  '',
'dbname'    =>  'mydatabase',

);

$db = new PDO('mysql:host='. $config['db']['host'] .';dbname='. $config['db']
['dbname'], $config['db']['username'], $config['db']['password']);


?>

最后是注册php文件(只是表格):

<div class="container">   
  <form id="regform" class="form-signin" action='staffRegister.php' method='Post'    
 onsubmit="return validateForm()"/>
     <img src="logo.png" width="160" height="50"> <h2 class="form-signin-heading">Staff       

Registration</h2>


        <div>
            <label for="name">First Name: </label>
            <input id="fname" name="name" type="text" class="input-                             

block-level" maxlength="35" onFocus="if(this.value=='name')this.value='';" onblur =   
"checkField(this)"/>
            <span id="Alpha"  style="display:none;">Please use letters  

only.</span>
        </div>
        <div>
            <label for="name">Last Name: </label>
            <input id="lname" name="name" type="text" class="input- 

block-level" maxlength="35" onFocus="if(this.value=='name')this.value='';" onblur = 

"checkField1(this)"/>
            <span id="Alpha2"  style="display:none;">Please use letters 

only.</span>
        </div>
        <div>
            <label for="name">UserName: </label>
            <input id="username" name="name" type="text" class="input-

block-level" maxlength="12"  />

        </div>
        <div>
            <label for="email">E-mail Ad:</label>
            <input id="email" placeholder="@kent.ac.uk"name="Email" 
type="text" class="input-block-level" onchange="return validateEmail();"/></br>
            <span id="spanEmail"  style="display:none;">Please use your    
Kent Email.</span>

        </div>
        <div>
            <label for="pass1">Password:</label>
            <input id="pass1" name="pass1" type="password"   
class="input-block-level" maxlength="12" />
            <span id="pass1Info">At least 6 characters: letters,  
numbers and '_'</span>
        </div>
        <div>





    <center><input type='Submit' name='Submit' value="Register" class="btn btn-   
 primary"/ >    </input></center>
  </form>

任何帮助都非常感谢,谢谢。

4

2 回答 2

0

你有3个问题。

1.混合$_GET[]$_POST[]。作为form method = post使用$_POST[]

2.form和php之间变量的命名。即fname在形式和$firstnamePHP。文本输入的名称和 ID 也应该相同。

3.当您的变量设置不正确时,会出现您收到的警告。要解决此问题,请使用isset()。下面的代码说明了问题。(另存为post.php)

<?php 
echo "Test for $_POST & isset()<br>";
$test1 = isset($_POST['test1']) ? $_POST['test1'] : ''; 
$test2 = $_POST['test2'];
echo $test1;
echo "<br>";
echo $test2;
?> 
<form action="post.php" method="post"> 
<input name="test1" type="checkbox" value="1"> 
<input name="test2" type="checkbox" value="2"> 
<input type="submit"> 
</form>

上面的演示使用 isset() 和三元运算符,我已经说明了如何在您的代码中使用它。

$fname = isset($_POST['$fname']) ? $_POST['$f'] : ''; 
$lname = isset($_POST['$lname']) ? $_POST['$lname'] : ''; 
etc
于 2013-02-24T01:13:15.697 回答
0

您的 HTMl 中的名称与您在$_POST. 您同时发布 Firstname 和 Surname 的名称name并尝试$_POST['FirstName'] $_POST['LastName']分别获取它们。

于 2013-02-23T18:21:32.170 回答