I'm attempting to develop a user login system using PHP, where each user is assigned their own table within a database named 'crop'. However, despite each user having their own table, all users are seeing the same 'last crop'. How can I fix this issue?
here is the code:
CREATE TABLE `users` (
`id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
`username` VARCHAR(50) NOT NULL UNIQUE,
`password` VARCHAR(255) NOT NULL,
`created_at` DATETIME DEFAULT CURRENT_TIMESTAMP);
CREATE TABLE `crop` (
`id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
`qty` INT NOT NULL,
`pot_size` INT NOT NULL,
`name` VARCHAR(50) NOT NULL,
`thc` INT NOT NULL,
`yield` INT NOT NULL,
`ready` INT NOT NULL,
`genetics` VARCHAR(50) NOT NULL,
`soil` VARCHAR(50) NOT NULL,
`type` VARCHAR(20) NOT NULL,
`startdate` DATE NOT NULL,
`enddate` DATE NOT NULL,
`total_harvested` INT NOT NULL,
`strain_rating` INT DEFAULT NULL);
CREATE TABLE `user_crop` (
`user_id` INT NOT NULL,
`crop_id` INT NOT NULL,
PRIMARY KEY (`user_id`, `crop_id`),
FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE,
FOREIGN KEY (`crop_id`) REFERENCES `crop` (`id`) ON DELETE CASCADE);
CREATE TABLE `watering` (
`id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
`crop_id` INT NOT NULL,
`ph` FLOAT NOT NULL,
`root_juice` FLOAT NOT NULL,
`bio_grow` FLOAT NOT NULL,
`bio_bloom` FLOAT NOT NULL,
`top_max` FLOAT NOT NULL,
`bio_heaven` FLOAT NOT NULL,
`acti_vera` FLOAT NOT NULL,
`wdate` DATETIME(6) NOT NULL,
FOREIGN KEY (`crop_id`) REFERENCES `crop` (`id`) ON DELETE CASCADE);
CREATE TABLE `weather` (
`id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
`temperature` FLOAT NOT NULL,
`humidity` FLOAT NOT NULL,
`date` DATETIME(6) NOT NULL);
And the PHP to create new crop:
<?php include 'include/dbconnect.php';?>
<?php
if(isset($_POST['datepicker']) && isset($_POST['submit']) && $_POST['submit'] =='Submit' ){
$originalDate = $_POST['datepicker'];
$newDate = date("Y-m-d", strtotime($originalDate));
$qty = $_POST['qty'];
$pot_size = $_POST['pot_size'];
$name = $_POST['name'];
$soil = $_POST['soil'];
$type = $_POST['type'];
$thc = $_POST['thc'];
$yield = $_POST['yield'];
$ready = $_POST['ready'];
$genetics = $_POST['genetics'];
$datepicker = $newDate;
// SQL query to insert a new crop
$sql = "INSERT INTO `crop` (`qty`,`pot_size`,`name`,`soil`,`type`,`thc`,`yield`,`ready`,`genetics`,`startdate`) VALUES ('$qty','$pot_size','$name','$soil','$type','$thc','$yield','$ready','$genetics','$datepicker')";
if (mysqli_query($conn, $sql)) {
echo "New crop record created successfully";
$crop_id = mysqli_insert_id($conn);
// SQL query to associate the crop with the current user
$user_id = $_SESSION['user_id'];
$sql = "INSERT INTO `user_crop` (`user_id`, `crop_id`) VALUES ('$user_id', '$crop_id')";
if (mysqli_query($conn, $sql)) {
echo "Crop associated with user successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
?>
" . mysqli_error($conn); }` is also a bad design. It's verbose, makes your code harder to read, and would also be insecure to show errors on-screen in an internet-facing site (they should always be logged to a file). See https://stackoverflow.com/questions/15318368/mysqli-or-die-does-it-have-to-die for an up-to-date, quicker, cleaner and more secure alternative way to trace mysqli errors. – ADyson May 01 '23 at 00:01