dongtan9465 2015-11-20 14:42
浏览 144
已采纳

或变量(T_VARIABLE)或数字(T_NUM_STRING)

After read many and many posts i still cannot fix the error! I think my problem is that i need to say what is the user id, but well created it as auto_increment, lets show my database:

CREATE TABLE USER(
    id TINYINT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(20) NOT NULL,
    lastname VARCHAR(20) NOT NULL,
    username VARCHAR(20) NOT NULL UNIQUE,
    nif VARCHAR(9) NOT NULL,
    email VARCHAR(30) NOT NULL,
    password VARCHAR(20) NOT NULL,
    country VARCHAR(20),
    age TINYINT
);

CREATE TABLE BOOK(
id TINYINT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(50) NOT NULL,
author VARCHAR(30) NOT NULL,
language VARCHAR(20) NOT NULL,
thematic VARCHAR(30) NOT NULL,
done BOOLEAN DEFAULT FALSE,
userid TINYINT default 1,
    FOREIGN KEY (userid) REFERENCES USER (id)
    ON UPDATE CASCADE);

Watching it, its really easy to do an insert by Mysql-Workbench like:

insert into USER (name, lastname, username, NIF, email, password) values ('victor', 'ribero', 'tureey', '47880680q', 'victor.ribero3@gmail.com', '47880680q');

The problem is, when i do the "sign up" to create a user by an html form interface it crashs and i dont know why:

<div id="form-wrapper">
            <form action="php/insert_user.php" id="sign-up-form">
                <form action="insert_user.php" method="POST">
                        <input required class="shared" type="text" placeholder="Name (*)" name="name" maxlength="20">
                        <input required class="shared" type="text" placeholder="Last name (*)" name="lastname" maxlength="20"><br/>
                        <input required class="shared" type="text" placeholder="nif (*)" name="nif" maxlength="20">
                        <input required class="shared" type="text" placeholder="Email (*)" name="email" maxlength="30"><br/>
                        <input required class="shared" type="text" placeholder="User name (*)" name="username" maxlength="20">
                        <input required class="shared" type="text" placeholder="Password (*)" name="password" maxlength="20"><br/>
                        <input class="shared" type="text" placeholder="Country" name="country" maxlength="20">
                        <input class="shared" type="text" placeholder="Age" name="age"><br/>
                        <input type="text" hidden name="userid" value="1">

                        <p><small>(*) Means its required.</small></p>
                        <input onclick="close_signUp" id="signup-up-confirm" type="submit">
                    </form>
                </div>
        </form>

And i try to create it by this insert_user.php

require_once 'login.php';
    //////////////////////
    //Connect to the server
    //////////////////////
    $link = mysqli_connect($db_hostname, $db_username, $db_password);
    if (!$link) die("Unable to conect to MYSQL" . mysqli_error($link));
    //////////////////////
    //Select the database
    //////////////////////
    $db = mysqli_select_db($link, $db_database);
        if(!$db)die ("Unable to conect to mysqli: " . mysqli_error($link));

    //Query to add a new user into our database (sign up)

        /******************************************************/
        /******************************************************/
        /******************************************************/


                    //cannot ad a user yet

        /******************************************************/
        /******************************************************/
        /******************************************************/
    $query = "insert into USER (id, name, lastname, username, nif, email, password, country, age) VALUES ('null', $_POST['name'],
                                                                                                    $_POST['lastname'],
                                                                                                    $_POST['username'],
                                                                                                    $_POST['NIF'],
                                                                                                    $_POST['email'],
                                                                                                    $_POST['password'],
                                                                                                    $_POST['country],
                                                                                                    $_POST['age'])";
    //We apply the query to our database
    $result = mysqli_query($link, $query);

    if(!$result) die ("Database access failed: ". mysqli_error($link));
    else{
        ?>
        <!-- Redirection to the index after sign up -->
        <script type="text/javascript">
            alert('Used created successfully!');
            window.location = 'index.php';
        </script>
        <?php
    }
?>

Like i said, i hope the problem is the user id, but i dont know how i should fix it.

Any suggest? Thanks a lot :)

  • 写回答

1条回答 默认 最新

  • douke8473 2015-11-20 15:05
    关注

    You shuold not insert a value for id not even null try removing this way (and like suggested by @steve reformat the sql instruction avoiding $_POST inside the string )

      $query = "insert into USER ( name, lastname, username, nif, email, 
         password, country, age) VALUES ( " . $_POST['name'] .", " .    
                                          $_POST['lastname'] .", ".
                                          $_POST['username'] . ", ".
                                          $_POST['NIF']. ", " .
                                          $_POST['email'] .", ".
                                          $_POST['password'] . ", ".
                                          $_POST['country] .", " .
                                          $_POST['age'] .")";
    

    and just a minor you don't need

    <input type="text" hidden name="userid" value="1">
    

    in the create user

    The user id should be created by autoicrement

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单