多行到数据库,而不是添加

时间:2014-07-30 17:22:35

标签: javascript php database rows

我发现这个脚本在某个地方向我的表单添加行,我可以添加有效的行,唯一的问题是当我提交它时,只提交第一行。从来没有尝试过这样的事情所以我可能会完全离开,如果是这样的话,至少我会知道。

这是HTML

<form name="addshow" action="addshow.php" method="post">
       <input type="submit" value="Lägg till">
       <input type="reset" value="Nollställ"/>
       <script type="text/javascript"  src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
       <script type="text/javascript">
                $(document).ready(function() {
                    $("#add").click(function() {
                      $('#tableclassen tbody>tr:last').clone(true).insertAfter('#tableclassen tbody>tr:last');
                      return false;
                    });
                });
            </script>
            <a  id="add">+</a>
            <TABLE id='tableclassen' cellpadding="3" cellspacing="0">
                <TBODY>
                <TR valign="top">
                    <TD width>
                    <input type="text" placeholder="ID" name="Horse_id" required oninvalid="this.setCustomValidity('Ange ID')" oninput="setCustomValidity('')" size="5"/>
                    </TD>

                    <TD>
                    <input type="text" placeholder="Årtal" name="years" required oninvalid="this.setCustomValidity('Ange årtal')" oninput="setCustomValidity('')" size="6"/>
                    </TD>

                    <TD>
                    <input type="text" placeholder="Plac." name="place" required oninvalid="this.setCustomValidity('Ange placering')" oninput="setCustomValidity('')" size="4"/>
                    </TD>

                    <TD>
                    <input type="text" placeholder="Ange eventnamn" name="event" required oninvalid="this.setCustomValidity('Ange eventnamn')" oninput="setCustomValidity('')" size="35"/>
                    </TD>

                    <TD>
                    <input type="text" placeholder="Ange klass" name="class" required oninvalid="this.setCustomValidity('Ange klass')" oninput="setCustomValidity('')" />
                    </TD>

                    <TD>
                    <input type="text" placeholder="Assoc." name="association" required oninvalid="this.setCustomValidity('Ange Assoc.')" oninput="setCustomValidity('')" size="6"/>
                    </TD>

                    <TD>
                    <input type="text" placeholder="Poäng" name="points" required oninvalid="this.setCustomValidity('Ange poänh')" oninput="setCustomValidity('')" size="3"/>
                    </TD>

                    <TD>
                    <select name="type" required oninvalid="this.setCustomValidity('Välj typ')" oninput="setCustomValidity('')">
                        <option value="" disabled selected>Välj typ</option>
                        <option value="SWE">SWE</option>
                        <option value="INTER">INTER</option>
                    </select>
                    </TD>

                    <TD>
                    <select name="dis">
                        <option value="" disabled selected>Välj Dis</option>
                        <option value="D">D</option>
                        <option value="SJ">SJ</option>
                        <option value="E">E</option>
                        <option value="U">U</option>
                        <option value="K">K</option>
                        <option value="T">T</option>
                    </select>
                    </TD>
                </TR>
                </TBODY>
            </TABLE>

            </form>

这是PHP /数据库过程

<?php
session_start();
ob_start();
include("function/conn.php"); //Databas koppling
mysql_set_charset("utf8");

$Horse_id = addslashes($_POST['Horse_id']);
$Year = addslashes($_POST['years']);
$Place = addslashes($_POST['place']);
$Event = addslashes($_POST['event']);
$Class = addslashes($_POST['class']);
$Assoc = addslashes($_POST['association']);
$Point = addslashes($_POST['points']);
$Type = addslashes($_POST['type']);
$Dis = addslashes($_POST['dis']);
//Sparar värden i variabler ifrån form


$result = mysql_query(" INSERT INTO shows 
                        (Horse_id, years, place, event, class, association, points, type, dis) 
                        VALUES('$Horse_id', '$Year', '$Place', '$Event', '$Class', '$Assoc', '$Point', '$Type', '$Dis') ") or die(mysql_error()); 
// Lägger in ny post i databasen

if ($result){ //Skicka tillbacka användaren till backend
header("location: admin.php");
}
else{
echo "Something went wrong.";
} 
?>

2 个答案:

答案 0 :(得分:1)

给表单字段添加数组样式名称,例如

<input type="text" placeholder="ID" name="Horse_id[]" required oninvalid="this.setCustomValidity('Ange ID')" oninput="setCustomValidity('')" size="5"/>

然后在PHP中,$_POST['Horse_id']将是所有输入的数组;服务器脚本可以遍历它们并将它们全部插入到数据库中。如果您在没有[]的所有行中使用相同的名称,$_POST['Horse_id']将只包含最后一个。

答案 1 :(得分:0)

问题是因为您没有更改克隆输入的名称字段,后端在大多数情况下只接受最后一个字段。

我在一个类似主题中留下了一个例子:Change name attribute of input/select field nested in a cloned table for dynamic form entry - Javascript