I can’t for the life of me figure out why my coding is not working in order to update my sql database from some php code. I’m hoping there are some kind peeps out there that could easily spot my mistake.
Here’s the code:
<?php
require_once('connectvars.php');
if (isset($_GET['id'])) {
// Grab the company data from the GET
$id = $_GET['id'];
$company = $_GET['company'];
$main_address = $_GET['main_address'];
$state = $_GET['state'];
}
else if (isset($_POST['id'])) {
// Grab the company data from the POST
$id = $_POST['id'];
$company = $_POST['company'];
$main_address = $_POST['main_address'];
$state = $_POST['state'];
}
else {
echo '<p class="error">Sorry, no company was specified for editing.</p>';
}
if (isset($_POST['submit'])) {
if ($_POST['confirm'] == 'Yes') {
// Connect to the database
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// update the company data from the database
$query="UPDATE company SET company='$company',
main_address='$main_address',
state='$state'
WHERE id = '$id' ";
mysqli_query($dbc, $query);
mysqli_close($dbc);
echo '<p>The company '.$company.' was successfully edited.';
}
else {
echo '<p class="error">The company was not edited.</p>';
}
}
else if (isset($id)) {
echo '<p>Please edit as required.</p>';
echo '<strong>id: </strong>' . $id . '<br />';
echo '<form method="post" action="/ims/editcompany.php">';
echo '<strong>Company:</strong>'.' '.'<input type="text" name="company" value="'.$company.'" />'."<br><br>";
echo '<strong>Main Address:</strong>'.' '.'<input type="text" name="main_address" value="'.$main_address.'" />'."<br><br>";
echo '<strong>State:</strong>'.' '.'<input type="text" name="state" value="'.$state.'" />'."<br><br>";
echo '<input type="submit" value="Submit" name="submit" />';
echo '</form>';
}
?>