gpt4 book ai didi

php更新数据库中的多个选择选项值

转载 作者:行者123 更新时间:2023-11-30 23:07:18 24 4
gpt4 key购买 nike

我有这样一个数据库

CREATE TABLE IF NOT EXISTS `ia_pages` (
`pages_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`page_name` varchar(255) NOT NULL,
`search_type` varchar(120) NOT NULL,
`search_block_position` varchar(255) NOT NULL,
PRIMARY KEY (`alphabet_search_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;



INSERT INTO `ia_pages` (`pages_id`, `page_name`, `search_type`, `search_block_position`) VALUES
(1, 'home_page', 'product_search', 'right_column'),
(2, 'product_page', 'category_search', 'right_column'),
(3, 'category_page', 'product_search', 'right_column');

和这样的html表单

<table id="admin-settings">
<tbody>
<tr>
<th>Page Name</th>
<th>Search Type</th>
<th>Show Search in Block</th>
</tr>
<tr>
<td>Home Page</td>
<td>
<select id="search_type" name="search_type">
<option value="product_search">Product Search</option>
<option value="category_search">Category Search</option>
</select>
</td>
<td>
<select id="show_block" name="show_block">
<option value="left_column">Left Column</option>
<option value="right_column">Right Column</option>
</select>
</td>
</tr>
<tr>
<td>Product page</td>
<td>
<select id="search_type" name="search_type">
<option value="product_search">Product Search</option>
<option value="category_search">Category Search</option>
</select>
</td>
<td>
<select id="show_block" name="show_block">
<option value="left_column">Left Column</option>
<option value="right_column">Right Column</option>
</select>
</td>
</tr>
<tr>
<td>Category page</td>
<td>
<select id="search_type" name="search_type">
<option value="product_search">Product Search</option>
<option value="category_search">Category Search</option>
</select>
</td>
<td>
<select id="show_block" name="show_block">
<option value="left_column">Left Column</option>
<option value="right_column">Right Column</option>
</select>
</td>
</tr>
<tr>
<td style="text-align:center;" colspan="4">
<input type="submit" name="submit" value="save" class="button">
</td>
</tr>
</tbody>
</table>

现在,当我在表单中选择任何值并单击时进行任何更改时,它会使所有行的值都相同。我的更新查询是这样的

$host = 'localhost';
$username = 'root';
$username = 'root';
$dbname = 'ia_pages';
$con=mysqli_connect($host,$username,$username);
mysqli_select_db($con,$dbname) or die ("no database");

if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$search_type = $_POST['search_type'];
$show_block = $_POST['show_block'];

if(isset($_POST['submit'])) {
$update_query = "UPDATE `pages` SET `search_type` = '$search_type',`search_block_position` = '$show_block'";
$query_execute = mysqli_query($con, $update_query);
if($query_execute) {
echo "Data has been updated";
} else {
echo "data has not been updated";
}
}

这里它正在更改所有值,即使只有选项已更改。因此数据库中的所有值都已转换为相同的值。那么如何解决这个问题呢?我想更新已在前端更改的数据库中的值。

最佳答案

您需要在更新语句中添加一个where 子句。没有它,所有行都会更新。

关于php更新数据库中的多个选择选项值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21306279/

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