未定义索引-将动态数据插入mysql DB

时间:2018-07-17 21:36:09

标签: javascript php html mysql

im使用Java脚本来调用输入列表,以使用PHP将插入到我的数据库中。我的表单中有第一个输入块,而第二个输入块是通过单击按钮来调用的。如果我调用第二个块,则一切正常,但是,如果仅使用第一个块提交表单,则会出现未定义的索引错误。有任何想法吗? 注意添加旧地址是可选的。

这是我的表格:

if(isset($_POST['submit'])){


$address = $_POST['address'];
$suburb = $_POST['suburb'];
$state = $_POST['state'];
$country = $_POST['country'];

$oldAddress = $_POST['oldAddress'];
$oldSuburb = $_POST['oldSuburb'];
$oldState = $_POST['oldState'];
$oldCountry = $_POST['oldCountry'];


$query = "INSERT INTO address(address, suburb, state, country, oldAddress, oldSuburb, oldState, oldCountry)";

$query .= "VALUES ('$address', '$suburb', '$state', '$country', '$oldAddress', '$oldSuburb', '$oldState', '$oldCountry')";

$result = mysqli_query($connection, $query);

if(!$result){
    die("Failed" . mysqli_error($connection));
    }

}

<form action="address.php" method="POST">
    <div id="list">
        <input type="text" name="address" placeholder="Address">
        <input type="text" name="suburb" placeholder="Suburb">
        <input type="text" name="state" placeholder="State">
        <input type="text" name="country" placeholder="Country">
        <button id="addMore">Add Address</button>
    </div>
        <button type="submit">Send</button>
</form>

<script type="text/javascript">
    $(var count = 0;
       $("#addMore").click(function(e){
          e.preventDefault();
           $("#list").append("<input type='text' name='oldAddress'"+count+" placeholder="Address"><input type="text" name='oldSuburb'"+count+" placeholder="Suburb"><input type="text" name='oldState'"+count+" placeholder="State"><input type="text" name='oldCountry'"+count+" placeholder="Country"></div>");
       count++;
       });
    });
</script>

如果我单击“添加地址”,请输入一些详细信息,然后提交,即可。如果我不这样做,则会得到一个未定义的索引:oldAddress / oldSuburb / oldState / oldCountry。

请帮助...

0 个答案:

没有答案