将表单中循环的字段值插入数据库

时间:2016-03-05 22:22:16

标签: php sql arrays database

我基本上是通过这个

多次循环一组字段
    <form name="insert_team" method="post" action="../includes/process.php?do=addteam">
            <table>
                <td style="width:40px;">Team:</td>
                <td id="data"><input type="text" name="team_name"/></td>
                <tr>
                <td>S.No</td>
                <td>Name</td>
                <td>Captain</td>
                <td>Category</td>
                <td>Batting Style</td>
                <td>Bowling Style</td>
                <td>Points</td>
                <tr>
                <?php
                for ($x=1;$x<2; $x++){
                ?>
                <td id="data"><?php echo $x ?> </td>
                <td id="data"><input name="player_name[]" type="text" value="abx"/></td>
                <td id="data">
                    <select style="width:50px;"name="is_captain[]"><option value="No">No</option>
                    <option value="Yes">Yes</option>
                    </select>
                </td>
                <td id="data">
                <select name="player_cat[]">
                <option value="Batsman">Batsman</option>
                <option value="Bowler">Bowler</option>
                <option value="Keeper">Wicket Keeper</option>
                <option value="All Rounder">All Rounder</option>
                </select>
                </td>
                <td id="data"><select style="width:160px;"name="bat_type[]">
                <option value="RHB">Right Hand Batsman</option>
                <option value="LHB">Left Hand Batsman</option>
                </select></td>
                <td id="data"><select name="bowl_type[]">
                <option value="Fast">Fast</option>
                <option value="Off Spinner">Off Spinner</option>
                <option value="Leg Spinner">Leg Spinner</option>
                <option value="Medium">Medium</option>
                </select></td>
                <td><input type="text" name="points[]" value="20"/></td>
                <td><input type="text" name="status[]" hidden value="1" /></td>
                </tr>
                <?php
                }
                ?>


            <td style="color:black;" colspan="6"><center><input type="submit"  style="background-color:#009900;"/></center></td>
            </table>

            </form>
                        </div>
        </center>

        </div>  
    </div>
    <?php
    }
    ?>
</div>

这是我用来在数据库中插入每个字段的值的process.php。

<?php session_start();

$from_Page = (string) $_REQUEST["do"];

require "dbcon.php";
require "func.php";
if( $from_Page=="addteam" ) {
    $team_name = $_POST["team_name"];
    echo $team_name;

    for ( $i=1; $i<2; $i++ ) {
        $player_name = $_POST["player_name"][$i];
        $category = $_POST["player_cat"][$i];
        $is_captain = $_POST["is_captain"][$i];
        $bat_type = $_POST["bat_type"][$i];
        $bowl_type = $_POST["bowl_type"][$i];
        $points = $_POST["points"][$i];
        $status = $_POST["status"][$i];
    }

    $sql = "INSERT INTO tbl_players (player_name,country_name,is_captain,category,bat_type,bowl_type,points,status) VALUES ($player_name,$team_name,$is_captain,$category,$bat_type,$bowl_type,$points,$status)";

    if (!$sql) 
        die('Invalid query: ' . mysql_error());
    }

?>

我仍然无法在数据库中插入数据,也没有出现任何错误。

提前致谢。

1 个答案:

答案 0 :(得分:2)

您是否应该与数据库建立任何连接以执行查询?使用以下语句打开数据库连接(假设您使用的是mysql数据库):

$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 

然后在执行sql语句的内部执行以下操作:

if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

最后关闭与数据库的连接:

$conn->close();

有关详细信息,请参阅http://www.w3schools.com/php/php_mysql_insert.asp