繁体   English   中英

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

[英]php update multiple select option values in the database

我有一个这样的数据库

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更新语句中添加where子句。 没有它,所有行都会更新。

更改:

<td>Home Page</td>

至:

<td>
 Home Page
 <input type="hidden" name="page_name" value="Home Page" />
</td>

将类似的更改应用于其他页面名称类型。

然后在update查询中添加一个where子句,如下所示:

$update_query = "UPDATE `pages` SET "
                . "`search_type` = '$search_type', "
                . "`search_block_position` =  '$show_block' "
                . "where `page_name` = '$page_name';

更新

如果所有选择列表都在相同的form元素中,则在php脚本中,由于输入元素具有相同的名称,因此需要将值读入数组。

$page_names = $_POST[ 'page_name' ];  
$search_types = $_POST[ '$search_type' ];  
$show_blocks = $_POST[ '$show_block' ];  

然后,您的更新语句应为:

update ia_pages
  set search_type =
        case page_name
             when '$page_names[ 0 ]' then '$search_types[ 0 ]' -- Home Page
             when '$page_names[ 1 ]' then '$search_types[ 1 ]' -- Product Page
             when '$page_names[ 2 ]' then '$search_types[ 2 ]' -- Category Page
             else search_type
        end
    , show_block_position =
        case page_name
             when '$page_names[ 0 ]' then '$show_blocks[ 0 ]' -- Home Page
             when '$page_names[ 0 ]' then '$show_blocks[ 1 ]' -- Product Page
             when '$page_names[ 0 ]' then '$show_blocks[ 2 ]' -- Category Page
             else show_block_position
        end

该查询将更新所有相关字段。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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