简体   繁体   中英

simple registration page in php

I have the following code for the registration page:

    <?php
session_start();
require("mysqli.php"); 
?>
<html>

<head>

<title>Registration Page</title>
</head>

<body>
<?php

if ( $_POST['registerbtn']){
 function add_member(){
 global $db_obj;
 $uid=$db_obj->escape_string($_REQUEST['uid']);      // (A)
 $last=$db_obj->escape_string($_REQUEST['last']);
 $first=$db_obj->escape_string($_REQUEST['first']);
 $email=$db_obj->escape_string($_REQUEST['email']);
 $pass=$db_obj->escape_string($_REQUEST['passwd']);  // (B)
 $query="INSERT INTO member VALUES ('$uid','$last','$first', '$email', PASSWORD('$pass'))"; // (D)
 return ($db_obj->query($query));                    // (E)
 echo "Your acount has been created!";

}}


else
{
$form = "<form action='./register.php' method='post'>
<table>

<tr>
<td>uid:</td>
<td><input type='text' name='uid' value=''/></td>
</tr>

<tr>
<td>last:</td>
<td><input type='text' name='last' value=''/></td>
</tr>

<td>first:</td>
<td><input type='text' name='first' value=''/></td>
</tr>

<tr>
<td>Email:</td>
<td><input type='text' name='email' value=''/></td>
</tr>

<tr>
<td>Password:</td>
<td><input type='password' name='pass' value=''/></td>
</tr>


<tr>
<td></td>
<td><input type='submit' name='registerbtn' value='Register'/></td>
</tr>


</table>
</form>";
echo $form;
}
?>
</body>
</html>

and it is not working with me, what is my problem? I have created the member table in my local host as the following:

SQL

CREATE TABLE member

( uid varchar(10),
last varchar(25) NOT NULL,
first varchar(15) NOT NULL,
email varchar(35) NOT NULL UNIQUE,
passwd varbinary(48) DEFAULT NULL,
PRIMARY KEY (uid)
);

and when I press the register button nothing shows with me. any hints or advice please?

edited:

-- phpMyAdmin SQL Dump
-- version 3.5.8.2
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Apr 01, 2014 at 12:36 PM
-- Server version: 5.1.73-log
-- PHP Version: 5.3.3

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `###`
--

-- --------------------------------------------------------

--
-- Table structure for table `member`
--

CREATE TABLE IF NOT EXISTS `member` (
  `uid` varchar(10) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL,
  `last` varchar(10) NOT NULL,
  `first` varchar(15) NOT NULL,
  `email` varchar(40) NOT NULL,
  `passwd` varbinary(48) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

login.php

<html>
<?php require("mysqli.php"); ?>
<head>

<title>Login Page</title>
</head>

<body>
<?php

if ( $_POST['loginbtn']){
function authenticate($uid, $pass)
{global $db_obj;
$userid=$db_obj->escape_string($uid);
$pass=$db_obj->escape_string($pass);
$query="SELECT * FROM member WHERE uid =’$uid’"
. " AND passwd = PASSWORD(’$pass’)";
if ( ($result = $db_obj->query($query)) && $result->num_rows == 1 )
{ return $uid; }
else
{ return ""; }
}
}

else

$form = "<form action='./login.php' method='post'>
<table>

<tr>
<td>uid:</td>
<td><input type='text' name='uid' value=''/></td>
</tr>


<tr>
<td>Password:</td>
<td><input type='password' name='pass' value=''/></td>
</tr>


<tr>
<td></td>
<td><input type='submit' name='loginbtn' value='Login'/></td>
</tr>


</table>
</form>";
echo $form;

?>
</body>
</html>

Try the following which worked for me, minus the PASSWORD function which I don't know how/where you're using. It is commented out in a query line of code.

Instead of using your require("mysqli.php"); just replace the xxx below with your login credentials for the time being.

Affected lines have been commented out.

I also changed <form action='./register.php' method='post'> to <form action='' method='post'>

The entire code works from the same page.

There are comments inside the code for you to read.

<?php
// session_start();
// require("mysqli.php"); 

$DB_HOST = "xxx";
$DB_NAME = "xxx";
$DB_PASS = "xxx";
$DB_USER = "xxx";


$db_obj = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($db_obj->connect_errno > 0) {
  die('Connection failed [' . $db_obj->connect_error . ']');
}

if($db_obj->connect_errno > 0){
    die('Unable to connect [' . $db_obj->connect_errno . ']');
}


?>
<html>

<head>

<title>Registration Page</title>
</head>

<body>
<?php

if ($_POST['registerbtn']){
// function add_member(){ // commented out
 global $db_obj; // fill in credential on top instead.
 $uid=$db_obj->escape_string($_REQUEST['uid']);      // (A)
 $last=$db_obj->escape_string($_REQUEST['last']);
 $first=$db_obj->escape_string($_REQUEST['first']);
 $email=$db_obj->escape_string($_REQUEST['email']);
 $pass=$db_obj->escape_string($_REQUEST['pass']);  // (B)

 // Test line of code without the PASSWORD function
 $query="INSERT INTO member (uid,last,first, email, passwd) VALUES ('$uid','$last','$first', '$email', '$pass')"; // (D)

 // Comment out once the above test worked, then comment out the line above
 // $query="INSERT INTO member (uid,last,first, email, passwd) VALUES ('$uid','$last','$first', '$email', PASSWORD('$pass'))"; // (D)

 // return ($db_obj->query($query)); // (E)
 echo "Your account has been created!";

}
// } commented out. Used for add_member() function


else
{
$form = "<form action='' method='post'>
<table>

<tr>
<td>uid:</td>
<td><input type='text' name='uid' value=''/></td>
</tr>

<tr>
<td>last:</td>
<td><input type='text' name='last' value=''/></td>
</tr>

<td>first:</td>
<td><input type='text' name='first' value=''/></td>
</tr>

<tr>
<td>Email:</td>
<td><input type='text' name='email' value=''/></td>
</tr>

<tr>
<td>Password:</td>
<td><input type='password' name='pass' value=''/></td>
</tr>


<tr>
<td></td>
<td><input type='submit' name='registerbtn' value='Register'/></td>
</tr>


</table>
</form>";
echo $form;
}
?>
</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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