this is my first steps in PHP because I don't use it in my work. I would like to prepare form which will add data to database tables. I prepared this piece of code:
<?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;
}
?>
My idea was to insert a row with data into marinas table and based on its "size" value add extra rows with data to places table as many times as size value is. I used while loop, but my IF statement for $sql2 returns true only once and I don't have any idea why is that. For me, code logic seems ok and some data was added. Is there any limitation in PHP for the solution like mine?
Please check if place_id is a autoincrement key. Maybe it could be false because it violates the uniqueness of the key.