gpt4 book ai didi

PHP MYSQL 表单不会更新数据库

转载 作者:行者123 更新时间:2023-11-30 21:35:29 25 4
gpt4 key购买 nike

我有一个 PHP 表单,它在页面的一侧显示当前数据,而另一侧是可以填写以更新数据的字段。我有两个文件“modify.php”和“modify.config.php”来处理更新。当点击 modify.php 上的“保存”按钮时,我收到一条成功消息(通过配置页面);记录 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>

和修改.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);

?>

最佳答案

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

mysqli_commit($conn);

关于PHP MYSQL 表单不会更新数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54085629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com