PHP IF语句在While循环中仅返回一次true值

时间:2017-12-21 07:29:15

标签: php mysql insert-into

这是我在PHP中的第一步,因为我不会在我的工作中使用它。我想准备将数据添加到数据库表的表单。 我准备了这段代码:

<?php 

require "init.php";

$marina_id=$_POST["marina_id"];
$b_cor=$_POST["b_cor"];
$size=$_POST["size"];
$available_place=$_POST["available_place"];
$l_cor=$_POST["l_cor"];
$is_open=$_POST["is_open"];
$description=$_POST["description"];

$sql= "insert into marinas (marina_id, b_cor, l_cor, size, available_place, is_open, description) values('$marina_id', '$b_cor', '$l_cor', '$size', '$available_place', '$is_open', '$description');";
if (mysqli_query($con, $sql))
{
  echo "values were added";
} else {
  echo "values were not added";
}
$count = 0;
while ($count<$size)
{
    if ($count<$available_place) {
        $isavailable = 1;
        $accesscode = "";
    } else {
        $isavailable = 0;
        $accesscode = "abcdefgh";
    }
    $dock_id=(string)$marina_id."0".(string)$count;
    $nullObject="";
    $sql2= "insert into places (
        place_id, 
        b_cor, l_cor, 
        is_available, 
        plan, 
        depth, 
        width, 
        length, 
        description, 
        marina_id, 
        access_code, 
        reserved_from, 
        reserved_to, 
        last_update) values (

        '$dock_id', 
        '$b_cor', 
        '$l_cor', 
        '$isavailable',
        '$nullObject', 
        '$nullObject', 
        '$nullObject', 
        '$nullObject', 
        '$nullObject', 
        '$marina_id', 
        '$accesscode', 
        '$nullObject',
        '$nullObject', 
        '$nullObject');";
    if (mysqli_query($con, $sql2)) //$con comes from init.php
    {
    echo "complete.";
    } else {
    echo "something wrong with data.";
    }
    $count = $count+1;
}


 ?>

我的想法是将一个包含数据的行插入到游艇码头表中并基于其大小&#34;尺寸&#34; value将数据添加到places表中的额外行与size值一样多。我使用while循环,但我的$ sql2的IF语句只返回true一次,我不知道为什么会这样。对我来说,代码逻辑似乎没问题,并且添加了一些数据。对于像我这样的解决方案,PHP有任何限制吗?

1 个答案:

答案 0 :(得分:1)

请检查place_id是否为自动增量键。也许它可能是错误的,因为它违反了密钥的唯一性。