Reputation: 11
Here's the quick story, i am a beginner at MYSQL(i) coding, so please excuse noobish errors, except this system worked until i had 4-5 friends attempt to input data at one time, in the end it ended with the database not accept further inputs BUT it allows the usage of my removal code, where it will remove certain data from a table.
<?php
require ("../config/mysql.php");
if (isset($_POST['unit']) && is_numeric($_POST['unit']))
{
$unit = $_POST['unit'];
$name = $_POST['name'];
$vehicle = $_POST['vehicle'];
$status = $_POST['status'];
$query = "INSERT INTO `users` (`unit`, `name`, `vehicle`,`status`) VALUES ('$unit', '$name', '$vehicle', '$status');";
mysqli_query($conn, $query);
header("Location: ../index.php");
}
else
{
echo "<h2>ERROR: YOU GINGER'D IT</h2>";
echo "<h2>YOU PUT A LETTER IN THE UNIT SLOT, GOOD JOB!</h2>";
}
?>
Ignore the last two echo's as it's a joke that we have between our friends.
Please note, that it works fine with exactly ONE person using it.
Encase you're wondering, here's my removal code:
<?php
require ("../config/mysql.php");
if (isset($_POST['unit']) && is_numeric($_POST['unit']))
{
$unit = $_POST['unit'];
$query = "DELETE from `users` WHERE unit = $unit";
mysqli_query($conn, $query);
header("Location: ../index.php");
}
else
{
echo "<h2>ERROR: YOU GINGER'D IT</h2>";
echo "<h2>YOU PUT A LETTER IN THE UNIT SLOT, GOOD JOB!</h2>";
}
?>
This code works fine, no errors. I attempted to use someone's code to show any errors, except it didn't work for me, so i am asking StackOverFlow for some assistance.
I have no idea what could be causing this error, i have searched up and down the ends of the internet for answers.
Thank You.
EDIT
Here is my table including current data.
-- phpMyAdmin SQL Dump
-- version 4.5.1
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Mar 21, 2016 at 05:51 PM
-- Server version: 10.1.10-MariaDB
-- PHP Version: 7.0.4
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `cad`
--
-- --------------------------------------------------------
--
-- Table structure for table `users`
--
CREATE TABLE `users` (
`id` int(100) NOT NULL DEFAULT '0',
`name` text NOT NULL,
`unit` text NOT NULL,
`vehicle` text NOT NULL,
`status` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `users`
--
INSERT INTO `users` (`id`, `name`, `unit`, `vehicle`, `status`) VALUES
(0, 'Ginger', '2', 'GMC Ambulance', 'ON DUTY'),
(1, 'Mac Mcfly', '1', 'Chevy Tahoe', 'OFF DUTY');
--
-- Indexes for dumped tables
--
--
-- Indexes for table `users`
--
ALTER TABLE `users`
ADD PRIMARY KEY (`id`);
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
Upvotes: 0
Views: 127
Reputation: 21
Your problem is not the code, it's the database. You obviously corrupted the database by violating the primary key. Try doing a select * on the table where your primary key is having count greater then 1. This happens when you don't have validation checks before inserting into the database.
Upvotes: 1