php mysql表单不会更新数据库

ukxgm1gy  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(314)

我有一个php表单,它在页面的一侧显示当前数据,另一侧是可以填充以更新数据的字段。我有两个文件“modify.php”和“modify.config.php”来处理更新。当点击modify.php上的“save”按钮时,我得到一条成功消息(通过配置页面);记录id(site\ id)传递到url中的成功消息,但数据不会在mysql数据库中更新。非常感谢您的帮助!
修改.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>

和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);

?>
bqjvbblv

bqjvbblv1#

您确定要将更改提交到数据库吗?我看到你正在更新然后关闭连接。在关闭连接之前,请尝试添加以下内容。

mysqli_commit($conn);

相关问题