gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:52:24 25 4
gpt4 key购买 nike

读了很多很多帖子后我仍然无法修复错误!我认为我的问题是我需要说出用户 ID 是什么,但很好地将其创建为 auto_increment,让我们显示我的数据库:

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

看着它,通过 Mysql-Workbench 进行插入真的很容易,如下所示:

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

问题是,当我通过 html 表单界面“注册”创建用户时,它崩溃了,我不知道为什么:

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

我尝试通过这个 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
}
?>

就像我说的,我希望问题出在用户 ID 上,但我不知道应该如何解决它。

有什么建议吗?非常感谢:)

最佳答案

您不应该插入 id 的值,甚至不能为 null 尝试以这种方式删除(就像 @steve 所建议的那样,重新格式化 sql 指令,避免在字符串中使用 $_POST )

  $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'] .")";

还有一个你不需要的未成年人

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

在创建用户

用户 ID 应由 autoicrement 创建

关于php - 或变量 (T_VARIABLE) 或数字 (T_NUM_STRING),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33829409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com