gpt4 book ai didi

php - sql 只存储每个用户 1 个请求,忽略其他请求

转载 作者:可可西里 更新时间:2023-11-01 00:58:15 25 4
gpt4 key购买 nike

不知何故,我的 mysql 数据库只为每个用户存储一次请求。

我打算为我的网站创建一个票务系统,我已经创建了表格并创建了一个表单和一个 php 类,如下所示。

在这种情况下,我想创建 2 个不同的票据,它只会存储第一张票而不是第二张票。

Screenshot of the submitted form

SQL代码:

CREATE TABLE IF NOT EXISTS `Comments` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`comment` varchar(255) NOT NULL,
`comment_creation_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `Conversation` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`ticket_id` varchar(30) NOT NULL,
`comment_id` int(11) NOT NULL,
`conversation_creation_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `ticket_id` (`ticket_id`,`comment_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `Tickets` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(20) NOT NULL,
`email` varchar(50) NOT NULL,
`ticket_id` varchar(30) NOT NULL,
`ticket_question` varchar(255) NOT NULL,
`ticket_status` tinyint(1) NOT NULL DEFAULT '1',
`ticket_subject` varchar(50) NOT NULL,
`ticket_creation_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`ticket_id`),
UNIQUE KEY `id` (`id`),
UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=10 ;

PHP 类:

<?php
class ticket_create extends database {

// Ticket class constructor storing the class variables
function __construct($username, $email, $ticket_id, $ticket_subject, $ticket_question){
$this->username = $username;
$this->email = $email;
$this->ticket_id = $ticket_id;
$this->ticket_subject = $ticket_subject;
$this->ticket_question = $ticket_question;
}

// Function to add the ticket to the database
function create_ticket(){
$this->connect();
$this->execute_query("INSERT INTO Tickets (username, email, ticket_id, ticket_subject, ticket_question) VALUES ('" . $this->username . "', '" . $this->email . "', '" . $this->ticket_id . "', '" . $this->ticket_subject . "', '" . $this->ticket_question . "')");
}

// Function to handle the class and execute their functions
function class_handler(){
$this->create_ticket();
return 'The ticket: ' . $this->ticket_id . ' was successfull created.';
}
}
?>

调用 php 类:

<?php
if(!empty($_POST)){
require_once('../handling/ticket_create.php');

$ticket_question = $_POST['ticket_question'];
$create_ticket = new ticket_create($user->username, $user->email, md5(uniqid($user->username, true)), $ticket_question);
$ticket_response = $create_ticket->class_handler();
echo $ticket_response;
}
?>

我如何让它工作,每张票都会被存储?

最佳答案

问题是这一行:

UNIQUE KEY `username` (`username`)

用户名是唯一的,这意味着如果您将banana作为用户名保存一次,您将无法再次保存banana。曾经。

从表中的用户名中删除唯一键,一切都会起作用。

ALTER TABLE Tickets DROP INDEX username;

CREATE TABLE IF NOT EXISTS `Tickets` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(20) NOT NULL,
`email` varchar(50) NOT NULL,
`ticket_id` varchar(30) NOT NULL,
`ticket_question` varchar(255) NOT NULL,
`ticket_status` tinyint(1) NOT NULL DEFAULT '1',
`ticket_subject` varchar(50) NOT NULL,
`ticket_creation_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`ticket_id`),
UNIQUE KEY `id` (`id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=10 ;

关于php - sql 只存储每个用户 1 个请求,忽略其他请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35084626/

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