注册表单不起作用

时间:2013-07-18 11:32:26

标签: php mysql database forms

我是php的新手,我试图使用php和mysql创建一个简单的注册表单。 我已经按照教程,但它似乎没有工作。
这是我的代码:

con.php

<?php
    $host = "******";
    $user = "******";
    $pass = "******";
    $db   = '******';

    $con = mysql_connect($host,$user,$pass) or die('could not connect to database.');

    mysql_select_db($db,$con) or die('could not find database');
?>

register.php

<?php
    include "con.php";
    $username = $_POST['user_name'];
    $password = md5($_POST['password']);
    $first    = $_POST['first'];
    $last     = $_POST['last'];
    $insert   = "insert into users (user_name,password,first,last) values(".$username.",".$password.",".$first.",".$last.")";
    mysql_query($insert) or die("Sorry could not complete signup");
    echo 'Signup succsessfull';
?>

HTML

    <form action='register.php' method='post'>
        <div style='color:orange;'>First name:</div>
        <input type='text' name='first' value='<?php if(isset($_POST["first"])){echo $_POST["first"];}?>'>
        <div style='color:orange;'>Last name:</div>
        <input type='text' name='last' value='<?php if(isset($_POST["last"])){echo $_POST["last"];}?>'>
        <div style='color:orange;'>User name:</div>
        <input type='text' name='user_name' value='<?php if(isset($_POST["user_name"])){echo $_POST["user_name"];}?>'>
        <div style='color:orange;'>Password:</div>
        <input type='password' name='password'>
        <input type='reset' value='Reset'>
        <input type='submit' value='Submit'>
    </form>

如果有人可以帮我解决这个问题会很棒,因为我做不到。

2 个答案:

答案 0 :(得分:2)

$insert = "insert into users (user_name,password,first,last) values(".$username.",".$password.",".$first.",".$last.")";

必须引用SQL中的字符串值。您没有引用您的数据。

按照in this answer所述,转到使用预准备语句和绑定变量,数据库API将为您添加引号。

答案 1 :(得分:1)

将您的查询更改为

$insert = "insert into users (user_name,password,first,last) values('".$username."','".$password."','".$first."','".$last."')";

我认为您选择了字段的varchar数据类型,是否必须为每个变量添加单引号。

相关问题