Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, I'm facing a problem populating MYSQL database from a form using PHP and I have no idea why is not working.

What I have tried:

MYSQL
<pre>CREATE TABLE `bus_location` (
  `id` int(11) NOT NULL,
  `address` text DEFAULT NULL,
  `city` text DEFAULT NULL,
  `state` text DEFAULT NULL,
  `b_number` text DEFAULT NULL,
  `b_name` text DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

ALTER TABLE `bus_location`
  ADD PRIMARY KEY (`id`);
COMMIT;


FORM
<form action="sendlocation.php" method="POST">
    ADDRESS: <input name="address" type="text" value="<?php echo $row['address']?>" value="" required/><br />
    CITY: <input name="city" type="text" value="<?php echo $row['city']?>" value="" required/><br />
    STATE: <input name="state" value="<?php echo $row['state']?>" type="text" value="" required/><br />
    PHONE NUMBER: <input name="b_number" value="<?php echo $row['b_number']?>" type="text" value="" required/><br />
    BUSINESS NAME: <input name="b_name" value="<?php echo $row['b_name']?>" type="text" value="" required/><br />
    <input type="submit" name="submit" value="Save Data">
</form>

THE PHP FILE
<?php 
include 'dbc.php';

if(isset($_POST['submit'])){
	//print_r($_POST); exit;
	$address = $_POST['address'];
	$city = $_POST['city'];
	$state = $_POST['state'];
	$b_number = $_POST['b_number'];
	$b_name = $_POST['b_name'];
	mysqli_query($con, "DELETE FROM bus_location");
	$query = "INSERT INTO bus_location (address, city, state, b_number, b_name)  VALUES ($address, $city, $state, $b_number, $b_name)";

	header("location:location.php");
	exit();
	
}

?>
Posted
Updated 29-Sep-22 6:17am
v2
Comments
Richard MacCutchan 29-Sep-22 3:47am    
You never execute the query.

Aside from the fact that the only query you're actually executing is DELETE FROM bus_location, you have a more serious problem.

Assuming you were actually intending to execute the query stored in the $query variable, your code would be vulnerable to SQL Injection[^]. NEVER use string concatenation/interpolation to build a SQL query. ALWAYS use a parameterized query.

PHP: SQL Injection - Manual[^]
PHP: Prepared statements and stored procedures - Manual[^]
 
Share this answer
 
Comments
Sphere Wallpapers 29-Sep-22 12:15pm    
Thanks for your response, I was aware of the vulnerable to SQL Injection and corrected my problem, It was an MYSQL database problem, also the posted PHP codes where from an example found on the web. This is not for a live website but intranet device I control remotely via web app.
I fixed my problem by implementing the following codes to PHP and modification to the MYSQL database.
PHP
if(isset($_POST['submit'])){
	$address = $_POST['address'];
	$city = $_POST['city'];
	$state = $_POST['state'];
	$b_number = $_POST['b_number'];
	$b_name = $_POST['b_name'];
	mysqli_query($con, "DELETE FROM bus_location");

	mysqli_query($con, "INSERT INTO bus_location SET
		address='$address',
		city='$city',
		state='$state',
		b_number='$b_number',
		b_name='$b_name'
	");

	header("location:location.php");
	exit();
	
}

MYSQL
CREATE TABLE `bus_location` (
  `id` int(11) NOT NULL,
  `address` text DEFAULT NULL,
  `city` text DEFAULT NULL,
  `state` text DEFAULT NULL,
  `b_number` text DEFAULT NULL,
  `b_name` text DEFAULT NULL,
  `role` text DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


ALTER TABLE `bus_location`
  ADD PRIMARY KEY (`id`);

--
ALTER TABLE `bus_location`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=50;
COMMIT;
 
Share this answer
 
Comments
Richard MacCutchan 29-Sep-22 12:24pm    
And what happens if the INSERT fails for some reason?
Richard Deeming 30-Sep-22 3:33am    
A SQL Injection vulnerability doesn't magically go away just because your code is used to control an intranet device.

What do you think happens when a device on your intranet gets infected with malware, or otherwise allows an attacker through?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900