简体   繁体   中英

PHP MYSQL form won't update the database

I've got a PHP form that displays current data on one side of the page and on the other side are fields that can be filled in to update the data. I have two files "modify.php" and "modify.config.php" to process the update. When hitting the "save" button on modify.php I get a success message (through the config page); the record id (site_id) passes through to the success message in the url but the data does not update in the mysql database. Any help is greatly appreciated!

Modify.php:

<?php
if(isset($_GET['id'])) {

require_once 'includes/connection.php';

$id = mysqli_real_escape_string($conn, $_GET['id']);

$sql = "SELECT * FROM sites WHERE site_id='$id' ";
$result = mysqli_query($conn, $sql) or die ("Bad Query: $sql");
$row = mysqli_fetch_array($result);

}

?>

<form action='modify.config.php?id=<?php echo $_GET['id']; ?>' method='POST'>        

<table width='100%'>
<thead>
<th colspan='2'><strong>Current Details:</strong></th>
<th colspan='2' style='background-color:#2c9cd4; color:#ffffff;'><strong>New Details:</strong></th>
</thead>  
<tr class='hover'>
<td width='15%'><strong>Site Code: </strong></td>
<td width='35%'><?php echo $row['site_code'] ?></td>
<td colspan='2' bgcolor='#f2f2f2'><input type='text' name='site_code' class='input2' placeholder='Site Code (UCCE)' maxlength='4' size='20' id='site_code' value='<?php if (isset($trimmed['site_code'])) echo $trimmed['site_code']; ?>' autofocus /></td>
</tr>
</tr>
<tr class='hover'>
<td width='15%'><strong>Name: </strong></td>
<td width='35%'><?php echo $row['site_name'] ?></td>
<td colspan='2' bgcolor='#f2f2f2'><input type='text' name='site_name' class='input2' placeholder='Site Name' maxlength='100' size='50' id='site_name' value='<?php if (isset($trimmed['site_name'])) echo $trimmed['site_name']; ?>' autofocus /></td>
</tr>
<tr class='hover'>
<td width='15%'><strong>Description: </strong></td>
<td width='35%'><?php echo $row['description'] ?></td>
<td colspan='2' bgcolor='#f2f2f2'><input type='text' name='description' class='input2' placeholder='Description' maxlength='100' size='50' id='description' value='<?php if (isset($trimmed['description'])) echo $trimmed['description']; ?>' autofocus /></td>
</tr>
<tr class='hover'>
<td width='15%'><strong>Street Address: </strong></td>
<td width='35%'><?php echo $row['address_street'] ?></td>
<td colspan='2' bgcolor='#f2f2f2'><input type='text' name='address_street' class='input2' placeholder='Street Address' maxlength='100' size='50' id='address_street' value='<?php if (isset($trimmed['address_street'])) echo $trimmed['address_street']; ?>' autofocus /></td>
</tr>
<tr class='hover'>
<td width='15%'><strong>City: </strong></td>
<td width='35%'><?php echo $row['address_city'] ?></td>
<td colspan='2' bgcolor='#f2f2f2'><input type='text' name='address_city' class='input2' placeholder='City' maxlength='100' size='50' id='address_city' value='<?php if (isset($trimmed['address_city'])) echo $trimmed['address_city']; ?>' autofocus /></td>
</tr>
</table>
</div>
<span style='float:right;'>
<button type='submit' class='save' id='submit' name='submit'>
<img src='images/save.png' height='13px' width='13px' style='vertical-align: sub;' />  Save </button>
<input type='hidden' name='submit' id='submit' value='TRUE' />
</span>
</form>

And Modify.Config.php:

<?php

if(isset($_GET['id'])) {

require_once 'includes/connection.php';

$id = mysqli_real_escape_string($conn, $_GET['id']);

$sql = "SELECT * FROM sites WHERE site_id='$id'";
$result = mysqli_query($conn, $sql) or die ("Bad Query: $sql");
$row = mysqli_fetch_array($result);

}

include 'includes/connection.php';

if (isset($_POST['submit'])) {

$site_code = mysqli_real_escape_string($conn, trim($_POST['site_code']));
$site_name = mysqli_real_escape_string($conn, trim($_POST['site_name']));
$description = mysqli_real_escape_string($conn, trim($_POST['description']));
$address_street=mysqli_real_escape_string($conn, trim($_POST['address_street']));
$address_city = mysqli_real_escape_string($conn, trim($_POST['address_city']));

$error = false;

if (!$error) {
if (!empty($site_code) && !empty($site_name) && !empty($description) && !empty($address_street) && !empty($address_city))

{

$sql = "UPDATE sites SET site_code='$site_code', site_name='$site_name', description='$description', address_street='$address_street', address_city='$address_city' WHERE site_id='$id'";

mysqli_query($conn, $sql);

}

$page_title = "Modify Success";
$page_content = "Site <strong><a href='site.php?id={$row['site_id']}'> {$row['site_code']} {$row['description']}</a></strong> has been successfully modified.";
include "header.php";
include "includes/box_success.php"; 
echo "<p>&nbsp;<p>";
include "footer.php";

mysqli_close($conn);

exit();

} else {

$page_title="Modify - ERROR";
$page_content = "<p><strong>The site {$row['site_code']} could not be updated.</strong></p><p>{$sql}<br>{mysqli_error($conn)}</p>";
include "header.php";
include "includes/box_error.php";
echo "<p valign='middle'>
<span style='float:left'>
<a href='modify.php?id={$row['site_id']}'>
<button type='submit' class='trans-left' value='submit-previous'>
<img src='images/chevron_back.png' height='15' width='18' style='vertical-align: sub;' />
Retry 
</button></a>
</span></p>
<p>&nbsp;</p>";
echo "</div>";
include "footer.php";

}

} 

mysqli_close($conn);

?> 

Are you sure you are committing the changes to the database? I see you doing an update then closing the connection. Try adding the following before you close the connection.

mysqli_commit($conn);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM