0

我正在尝试一次更新 11 行 - 之前从表单页面发布的板球队(11 名球员),然后我想更新与这些球员一起调用的第一个表。(表格包括球员、顺序和球队。)

<?php
session_start();

// Edit this to connect to your database with your username and password
$db_name="a1467234_cricket"; // Database name 
$tbl_name="statistics"; // Table name 

// Connect to server and select databse.
$conn = mysql_connect("mysql17.000webhost.com","*******","*******") or die(mysql_error()); 
mysql_select_db("$db_name")or die("cannot select DB");


$one = $_POST["one"];
$two = $_POST["two"];
$three = $_POST["three"];
$four = $_POST["four"];
$five = $_POST["five"];
$six = $_POST["six"];
$seven = $_POST["seven"];
$eight = $_POST["eight"];
$nine = $_POST["nine"];
$ten = $_POST["ten"];
$eleven = $_POST["eleven"];

$team = $_POST["team"];

$sql = "UPDATE first
   SET player = (CASE
       WHEN order = 1 ='$team' THEN '$one'
       WHEN order = 2 AND team ='$team' THEN '$two'
       WHEN order = 3 AND team ='$team' THEN '$three'
       WHEN order = 4 AND team ='$team' THEN '$four'
       WHEN order = 5 AND team ='$team' THEN '$five'
       WHEN order = 6 AND team ='$team' THEN '$six'
       WHEN order = 7 AND team ='$team' THEN '$seven'
       WHEN order = 8 AND team ='$team' THEN '$eight'
       WHEN order = 9 AND team ='$team' THEN '$nine'
       WHEN order = 10 AND team ='$team' THEN '$ten'
       WHEN order = 11 AND team ='$team' THEN '$eleven'
       ELSE player
     END
     WHERE order IN (1,2,3,4,5,6,7,8,9,10,11))";

echo "Updated team selection";

mysql_close($conn);

?>

有谁知道如何让更新脚本工作?不会显示任何错误,它只是不会更改数据库。

4

3 回答 3

1

您需要mysql_query($sql);在数据库上执行查询

于 2013-05-03T15:57:14.060 回答
0

对于您的查询,使用 for 循环可能更简单、更简洁、更智能。这可能对您没什么帮助:

<?php 
for($i=1;$i<$count;$i++) {
        $updatesql="update first set player ='".$_POST['player_'.$i]"' where team ='".$_POST['team_'.$i]"' ";
        mysql_query($updatesql);

}
?>

您可能需要对此进行轻微修改。

例如。您需要以这种方式发布数据:

player_1、player_2 等以及 team_1、team_2 等中的团队。

于 2013-05-03T15:54:24.217 回答
0

bviously, you are missing a closing parenthesis ')' after the END. By the way, the first case condition (order = 1 ='$team') doesn't seem to make any sense.

Anyway, the query below should work. Please note that the . . . would need to be populated in the same pattern as the first and second players.

 $sql = "
       UPDATE first a,
              (
                 SELECT 1 AS `order`, '{$one}' AS player, '{$team}' AS team
                 UNION ALL
                 SELECT 2 AS `order`, '{$two}' AS player, '{$team}' AS team
                 .
                 .
                 .
                 UNION ALL
                 SELECT 11 AS `order`, '{$eleven}' AS player, '{$team}' AS team
              ) b
          SET a.player = b.player
        WHERE a.`order` = b.`order`
          AND a.team = b.teamenter
      ";
于 2013-05-03T15:59:25.317 回答