PHP数据库不会保存数据 [英] PHP database won't save data

查看:97
本文介绍了PHP数据库不会保存数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视频教程系列中的以下代码。 (我听说下面的代码的一些部分被认为是不良做法...)。我一直在努力让它保存我的注册信息,但它只是不工作。它检测填写表单中的错误很好,它还显示请登录开始!当我正确提交一切,但它只是不会保存任何数据到数据库:



PHP

 <?php include(./inc/connect.inc.php); ?> 
<?php
$ reg = @ $ _ POST ['reg'];
//声明变量以防止错误
$ fn =; //名字
$ ln =; //姓氏
$ un =; //用户名
$ em =; // Email
$ em2 =; // Email 2
$ pswd =; // Password
$ pswd2 =; // Password 2
$ d =; //注册日期
$ u_check =; //检查用户名是否存在
//注册表单
$ fn = strip_tags(@ $ _ POST ['fname']);
$ ln = strip_tags(@ $ _ POST ['lname']);
$ un = strip_tags(@ $ _ POST ['username']);
$ em = strip_tags(@ $ _ POST ['email']);
$ em2 = strip_tags(@ $ _ POST ['email2']);
$ pswd = strip_tags(@ $ _ POST ['password']);
$ pswd2 = strip_tags(@ $ _ POST ['password2']);
$ d = date(Y-m-d); //年 - 月 - 日

if($ reg){
if($ em == $ em2){
//检查用户是否已存在
$ u_check = mysqli_query($ link,SELECT username FROM users WHERE username ='$ un');
//计算用户名= $ un
$ check = mysqli_num_rows($ u_check)的行数;
if($ check == 0){
//检查所有字段已填充
if($ fn&& $ ln& $& $ un&& $ em& ;& $ em2&& $ pswd&& $ pswd2){
//检查密码是否匹配
if($ pswd == $ pswd2){
//检查最大用户名,第一和姓氏的长度不超过25个字符
if(strlen($ un)> 25 || strlen($ fn)> 25 || strlen($ ln)> 25){
echo用户名,名字和姓氏的最大限制为25个字符!
}
else
{
//检查密码长度不超过30个字符,不少于5个字符
if(strlen($ pswd)> ; 30 || strlen($ pswd)< 5){
echo您的密码必须介于5到30个字符长!
}
else
{
//在发送到数据库之前使用md5加密密码和密码
$ pswd = md5($ pswd);
$ pswd2 = md5($ pswd2);
$ query = mysqli_query($ link,INSERT INTO users VALUES('','$ un','$ fn','$ ln','$ em','$ pswd','$ d' ,'0'));
die(< h2> Welcome to findFriends< / h2>请先登录以开始使用!
}
}
}
else {
echo您的密码不匹配!
}
}
else {
echo请填写所有必填字段。
}
}
else {
echo抱歉,用户名不可用。
}
}
else {
echo您的电子邮件不匹配!
}
}
?>

HTML

 < div style =width:200px; margin-left:550px; margin-top:20px;> 
< table>
< tr>
< td width =90%valign =top>
< h2>今天加入findfriends!< / h2>
< / td>
< td width =40%valign =top>< / td>
< h2>注册下面!< / h2>
< form action =index.phpmethod =POST>
< input type =textname =fnamesize =25placeholder =First Name>< br />< br /
< input type =textname =lnamesize =25placeholder =Last Name>< br />< br /
< input type =textname =usernamesize =25placeholder =用户名>< br />< br /
< input type =textname =emailsize =25placeholder =Email>< br />< br /
< input type =textname =email2size =25placeholder =Email>< br />< br /
< input type =textname =passwordsize =25placeholder =Password>< br />< br /
< input type =textname =password2size =25placeholder =密码(再次)>< br />< br /
< input type =submitname =regvalue =注册!>
< / form>
< / tr>
< / table>
< / div>
< / body>
< / html>

MYSQL (已添加到phpmyadmin)



CREATE TABLE IF NOT EXISTS`users`(
`id` int(11)NOT NULL AUTO_INCREMENT,/ * AUTO_INCREMENT = 1st user,id 1,第二个用户,id 2 .... * /
`username` varchar(255)NOT NULL,
`first_name` varchar(255)NOT NULL,
`last_name` varchar NOT NULL,
`email` varchar(255)NOT NULL,
`password` varchar(32)NOT NULL,
`sign_up_date`日期NOT NULL,
` ('0','1')NOT NULL,
PRIMARY KEY(`id`)
)ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT = 1;

connect.inc.php

 <?php 
$ link = mysqli_connect('localhost','user','pswd','socialnetwork')或die 无法连接到SQL服务器);
?>


解决方案

最好的建议是看http日志文件错误和访问日志)以找到解决方案。


I have the following code that I got from a video tutorial series. (I've heard that some parts of the code below are considered bad practice...). I've been trying to get it to save my Registration info but it just won't work. It detects errors in filling the form just fine, it also displays the "please login to get started"! When I submit everything correctly, but it just won't save any data to the database:

PHP

<?php include ("./inc/connect.inc.php"); ?>
    <?php
    $reg = @$_POST['reg'];
    //declaring variables to prevent errors
    $fn = ""; //First Name
    $ln = ""; //Last Name
    $un = ""; //Username
    $em = ""; //Email
    $em2 = ""; //Email 2
    $pswd = ""; //Password
    $pswd2 = ""; //Password 2
    $d = ""; //Sign up date
    $u_check = ""; //Check if username exists
    //registration form
    $fn = strip_tags(@$_POST['fname']);
    $ln = strip_tags(@$_POST['lname']);
    $un = strip_tags(@$_POST['username']);
    $em = strip_tags(@$_POST['email']);
    $em2 = strip_tags(@$_POST['email2']);
    $pswd = strip_tags(@$_POST['password']);
    $pswd2 = strip_tags(@$_POST['password2']);
    $d = date("Y-m-d"); //Year - Month - Day

    if ($reg) {
    if ($em==$em2) {
    // check if user already exists
    $u_check = mysqli_query($link,"SELECT username FROM users WHERE username='$un'");
    // count the amount of rows where username = $un
    $check = mysqli_num_rows($u_check);
    if ($check == 0) {
    // check all of the fields have been filled
    if ($fn&&$ln&&$un&&$em&&$em2&&$pswd&&$pswd2) {
    // check that passwords match
    if ($pswd==$pswd2) {
    // check the maximum length of username, first, and last name does not exceed 25 characters
    if (strlen($un)>25||strlen($fn)>25||strlen($ln)>25) {
    echo "The maximum limit for username, first, and last name is 25 characters!";
    }
    else
    {
    // check the length of password does not exceed 30 characters and is no less than 5 characters
    if (strlen($pswd)>30||strlen($pswd)<5) {
    echo "Your password must be between 5 and 30 charcters long!";
    }
    else
    {
    // encrypt password and password 2 using md5 before sending to database
    $pswd = md5($pswd);
    $pswd2 = md5($pswd2);
    $query = mysqli_query($link,"INSERT INTO users VALUES ('','$un','$fn','$ln','$em','$pswd','$d','0')");
    die("<h2>Welcome to findFriends</h2> Please login to get started!");
    }
    }
    }
    else {
    echo "Your passwords do not match!!";
    }
    }
    else {
    echo "Please Fill in all required fields.";
    }
    }
    else {
    echo "Sorry, that username is not available.";
    }
    }
    else {
    echo "Your email does not match!";
    }
    }
    ?>

HTML

<div style="width: 200px; margin-left: 550px; margin-top: 20px;">
        <table>
            <tr>
                <td width="90%" valign="top">
                    <h2>Join findfriends Today!</h2>
                </td>
                <td width="40%" valign="top"></td>
                    <h2>Sign Up Below!</h2>
                    <form action="index.php" method="POST">
                        <input type="text" name="fname" size="25" placeholder="First Name"><br /><br />
                        <input type="text" name="lname" size="25" placeholder="Last Name"><br /><br />
                        <input type="text" name="username" size="25" placeholder="Username"><br /><br />        
                        <input type="text" name="email" size="25" placeholder="Email"><br /><br />
                        <input type="text" name="email2" size="25" placeholder="Email"><br /><br />
                        <input type="text" name="password" size="25" placeholder="Password"><br /><br />
                        <input type="text" name="password2" size="25" placeholder="Password (again)"><br /><br />
                        <input type="submit" name="reg" value="Sign Up!">           
                    </form>
            </tr>
        </table>
</div>
</body>
</html>

MYSQL (added to phpmyadmin)

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT, /* AUTO_INCREMENT = 1st user, id 1, 2nd user, id 2....*/
  `username` varchar(255) NOT NULL,
  `first_name` varchar(255) NOT NULL,
  `last_name` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `password` varchar(32) NOT NULL,
  `sign_up_date` date NOT NULL,
  `activated` enum('0','1') NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

And the connect.inc.php:

  <?php
    $link = mysqli_connect('localhost', 'user', 'pswd', 'socialnetwork') or die ("Couldn't connect to SQL server");
    ?>

解决方案

The best suggestion is you shoud see http log files(error and access logs) to find solution.

这篇关于PHP数据库不会保存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆