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 :)