在 Stackoverflow 上发布脚本的新手,所以请注意我的上传技巧。我正在登录并在 php 中注册代码以连接我的数据库。当我运行时stmt->execute()
,什么也没有发生。我想if else
,error()
等
<?php
// begin our session
session_start();
//first check that both the username, password and form token have been sent
if(!isset( $_POST['username'], $_POST['password'], $_POST['mailid'], $_POST['firstname'], $_POST['lastname'], $_POST['form_token']))
{
$message = 'Username and password not valid';
}
// check the form token is valid
elseif( $_POST['form_token'] != $_SESSION['form_token'])
{
$message = 'Invalid form submission';
}
elseif (strlen( $_POST['username']) > 20 || strlen($_POST['username']) < 4)
{
$message = 'Incorrect Length for Username';
}
// check the password is the correct length
elseif (strlen( $_POST['password']) > 20 || strlen($_POST['password']) < 4)
{
$message = 'Incorrect Length for Password';
}
// check the username has only alpha numeric characters
elseif (ctype_alnum($_POST['username']) != true)
{
// if there is no match ***/
$message = "Username must be alpha numeric";
}
// check the password has only alpha numeric characters
elseif (ctype_alnum($_POST['password']) != true)
{
// if there is no match
$message = "Password must be alpha numeric";
}
else
{
// if we are here the data is valid and we can insert it into database
$phpro_username = filter_var($_POST['username'], FILTER_SANITIZE_STRING);
$phpro_password = filter_var($_POST['password'], FILTER_SANITIZE_STRING);
$phpro_mailid = filter_var($_POST['mailid'], FILTER_SANITIZE_STRING);
$phpro_firstname = filter_var($_POST['firstname'], FILTER_SANITIZE_STRING);
$phpro_lastname = filter_var($_POST['lastname'], FILTER_SANITIZE_STRING);
// now we can encrypt the password
$phpro_password = sha1( $phpro_password );
// connect to database
// mysql hostname
$mysql_hostname = 'localhost';
// mysql username
$mysql_username = 'root';
// mysql password
$mysql_password = '';
// database name
$mysql_dbname = 'login';
try
{
$dbh = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password);
// $message = a message saying we have connected
// set the error mode to excptions
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// prepare the insert
$stmt = $dbh->prepare("INSERT INTO userdetails (username, mailid, password, firstname, lastname ) VALUES (:phpro_username, :phpro_mailid, :phpro_password, :phpro_firstname, :phpro_lastname)");
//$stmt = $dbh->prepare("select * from userdetail");
// bind the parameters
$stmt->bindParam(':phpro_username' , $phpro_username );
$stmt->bindParam(':phpro_password' , $phpro_password );
$stmt->bindParam(':phpro_mailid' , $phpro_mailid );
$stmt->bindParam(':phpro_firstname', $phpro_firstname);
$stmt->bindParam(':phpro_lastname' , $phpro_lastname );
echo "Before execution";
// execute the prepared statement
$stmt->execute();
//echo "After execution";
// unset the form token session variable
unset( $_SESSION['form_token'] );
// if all is done, say thanks
$message = 'New user added';
}
catch(Exception $e)
{
// check if the username already exists
if( $e->getCode() == 23000)
{
$message = 'Username already exists';
}
else
{
// if we are here, something has gone wrong with the database
$message = 'We are unable to process your request. Please try again later"';
}
}
}
?>
尝试添加数据类型作为第三个参数bindParam
:
改变:
$stmt->bindParam(':phpro_username' , $phpro_username );
至
$stmt->bindParam(':phpro_username' , $phpro_username, PDO::PARAM_STR);
其他bindParams
调用同理。可以在此处找到更多帮助
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句