简体   繁体   English

PHP SQL注册表单问题

[英]Problems with PHP SQL Registration form

as the title suggests i'm creating a registration form using PHP and SQL. 如标题所示,我正在使用PHP和SQL创建注册表。 Whenever I click the register button, I get an undefined variable error on ALL of the variables in the form, also nothing is entered in the database. 每当我单击注册按钮时,都会在表格中的所有变量上得到未定义的变量错误,数据库中也不会输入任何内容。 Please if youcan suggest anything, i'm pulling my hair out. 请提出任何建议,我正在拔头发。

Here is the page that the form submits to: 这是表单提交到的页面:

<?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
);

The dbconfig file: 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']);


?>

Finally the registration php file (just the form): 最后注册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>

Any help really appreciated, thank you. 任何帮助都非常感谢,谢谢。

The names in your HTMl do not correspond to the keys you're using in $_POST . 您的HTM1中的名称与$_POST使用的键不对应。 You're posting both Firstname and Surname with a name of name and trying to fetch them with $_POST['FirstName'] $_POST['LastName'] respectively. 您发布两个名字和姓氏用的名称name ,并试图与来接他们$_POST['FirstName'] $_POST['LastName']分别。

You have 3 problems. 您有3个问题。

1.Mixing $_GET[] and $_POST[] . 1.混合$_GET[]$_POST[] as form method = post use $_POST[] 作为form method = post使用$_POST[]

2.Naming of variable between form and php. 2.form和php之间变量的命名。 ie fname in form and $firstname in php. 即形式的fname和php中的$firstname Also the name and id of the text inputs should be the same. 文本输入的名称和ID也应该相同。

3.The warnings you are getting appears when your variable are not properly set,. 3.如果未正确设置变量,则会出现警告。 To resolve this use isset() . 要解决此问题,请使用isset() The code below illustrates the problem.(save as post.php) 下面的代码说明了该问题。(另存为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>

The above demo uses isset() and the ternary operator and I have illustrated how to utilise it in your code. 上面的演示使用了isset()和三元运算符 ,我已经说明了如何在您的代码中使用它。

$fname = isset($_POST['$fname']) ? $_POST['$f'] : ''; 
$lname = isset($_POST['$lname']) ? $_POST['$lname'] : ''; 
etc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM