2014-01-23 173 views
1

我有这样PHP更新多个选择选项值

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

这正在改变甚至唯一的选择已经改变了所有的值。所以数据库中的所有值都被转换为相同的值。那么如何解决这个问题?我想更新前端已更改的数据库中的值。

+0

您有两个名字为“show_block”和“search_type”的下拉菜单。 –

+0

有没有更好的方法来做到这一点的好方法? – Jagdish

回答

0

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

+0

是的......但在这种情况下,哪里会有什么条件? – Jagdish

+0

我不知道。你想知道如何解决更新查询,添加'where'子句将做到这一点。您需要将整个事件包装在'

'中,并为'id'使用隐藏字段或使用'
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语句要像:

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 

此查询将更新所有相关领域。

+0

感谢您的回复。但任何时候当我正在做任何更新时,它只会取得最后的选项值。第一个和第二个TD选项没有进行任何更新。它总是取最后一个选项对,它出现在最后的td – Jagdish

+0

“select”元素名称在顶部组中与底部组中的相同。 – Tony

+0

@Tony:是的。 OP需要将它们作为数组读取,以便在sql语句中使用。 –