将值放入数组中

时间:2014-09-18 09:23:47

标签: php mysql arrays

下面是我用于循环并更新表的当前代码。但是,我遇到了正确定义数组的问题。数组从HTML表单获取值然后再更新到表。

 $data = mysql_query("SELECT min(leagueID) AS minLeagueID, max(leagueID) AS maxLeagueID , leageuID , trackID FROM leagueTracks WHERE legueID = 'vaildLeagueID'");

    $track = array ([0] = $_POST['track1'];
                    [1] = $_POST['track2'];
                    [2] = $_POST['track3'];
                    [3] = $_POST['track4'];
                    [4] = $_POST['track5'];
                    [5] = $_POST['track6'];
                    [6] = $_POST['track7'];
                    [7] = $_POST['track8'];
                    [8] = $_POST['track9'];
                    [9] = $_POST['track10'] );

    $info = mysql_fetch_array( $data );

    $i = '0';
    $min =  $info['minLeagueID'];
    $max =  $info['maxLeagueID'];

    while( $min != $max) {

        $updateTable = mysqli_query("UPDATE leagueTracks SET trackID = '$track[$counter]' WHERE leagueID = '$vaildLeagueID' AND leagueTracks = '$min' ");
        $i++;
    }

代码更新 - 但它仍然不会更新表。

$data = mysql_query("SELECT min(leagueTrackID) AS minLeagueID, max(leagueTrackID) AS maxLeagueID , leagueID , trackID FROM leagueTracks WHERE leagueID = 'vaildLeagueID'");

$track = array (

        0 => $_POST['track1'],
        1 => $_POST['track2'],
        2 => $_POST['track3'],
        3 => $_POST['track4'],
        4 => $_POST['track5'],
        5 => $_POST['track6'],
        6 => $_POST['track7'],
        7 => $_POST['track8'],
        8 => $_POST['track9'],
        9 => $_POST['track10']

  );

$info = mysql_fetch_array( $data );

$i = '0';
$min =  $info['minLeagueID'];
$max =  $info['maxLeagueID'];

while( $min != $max ) {

    $updateTable = mysqli_query("UPDATE leagueTracks SET trackID = '$track[$i]' WHERE leagueID = '$vaildLeagueID' AND leagueTrackID = '$min' ");
    $i++;
    $min++;
}

这是我目前正在使用的表单结构,每次输入都会复制十次。

<div class="form-group" align="center">

    <label class="col-lg-3 control-label">
        <h5>Track 1</h5>
    </label>

    <div class="form-group col-lg-8">

        <select name="track1" class="form-control col-lg-12" style="background:#e1e1e1;">

            <option value="0">Select a Track</option>
            <option value="1">Track 1</option>
            <option value="2">Track 2</option>
            <option value="3">Track 3</option>
            <option value="4">Track 4</option>
            <option value="5">Track 5</option>
            <option value="6">Track 6</option>
            <option value="7">Track 7</option>

        </select>

    </div>
</div>

请注意,我将此表单称为“track1”。这应该是name =“track []”还是什么?

正如您在图像中看到的那样,WHERE和AND功能不会像SELECT和from那样以红色突出显示,这意味着它的结构存在问题。

以下是当前错误:

SQL Error

4 个答案:

答案 0 :(得分:0)

这不是你如何定义多个数组值 - 我不太确定你从哪里得到它,但正确的语法是这样的:

$track = array(
    0 => $_POST['track1'],
    1 => $_POST['track2'],
    // etc
);

缩进是可选的,但是密钥周围的[]只是使用=作为赋值运算符而数组条目之间的;都是不正确的。

同样来自你的SQL查询看起来像leagueID(或至少,legueID,我认为它是同一个东西)是一个整数,因为你正在做{{ 1}}和MIN就可以了,但是你用MAX完成了查询,这是一个字符串,因此不会匹配任何内容(除非您只是用实际数字替换描述性文本)

答案 1 :(得分:0)

您是否检查过mySQL查询是否正确选择了您想要的内容?我可以看到许多拼写错误的&#34;联盟&#34;。

另外,你想在循环中做一些像$ min ++这样的事情,否则min永远不会=最大。

答案 2 :(得分:0)

定义数组有两种方法:

    $track = array();
    $track[0] = $_POST['track1'];
    $track[1] = $_POST['track2'];
    $track[2] = $_POST['track3'];
    .
    .
    .
    so on

另一个是:

    $track = array($_POST['track1'],$_POST['track2'],........);

答案 3 :(得分:0)

如果您多次使用此选择下拉菜单,如下所示,并从

提交

提交后

echo "<pre>";
    print_r($_POST["track"]);

您将获得结果

   Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)

您的表格     

    <select name="track[]" class="form-control col-lg-12" style="background:#e1e1e1; ">
            <option value="0">Select a Track</option>
            <option value="1">Track 1</option>
           .....
    </select>


    <select name="track[]" class="form-control col-lg-12" style="background:#e1e1e1; ">
            <option value="0">Select a Track</option>
            <option value="1">Track 1</option>
           .....
    </select>
    .........
    <input type="submit" name="submit">
</form>
  1. 从代码中删除 $ track array
  2. 在您的查询中添加 $ _ POST [&#34; track&#34;] [$ i]

    您的查询

    $updateTable = mysqli_query("UPDATE leagueTracks SET trackID = '".$_POST["track"][$i]."' WHERE leagueID = '$vaildLeagueID' AND leagueTrackID = '$min' ");