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

时间:2014-01-23 11:03:13

标签: php mysql

我有一个像这样的数据库

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";
          }
    }

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

改变:

<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';

更新

如果所有选择列表都在同一个表单元素中,那么在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

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