I would like to create 3 tables connected by foreign keys and have the end user input the data from one form
The tables are client, work, and accessories
The sql file is below
CREATE TABLE `Work` (
`WorkId` int(11) NOT NULL AUTO_INCREMENT,
`WrkWork` varchar(100) NOT NULL,
primary key(WorkId)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `Accessories` (
`AccId` int(11) NOT NULL AUTO_INCREMENT,
`product_name` varchar(20) NOT NULL,
`product_price` float NOT NULL,
`product_quantity` int(11) NOT NULL,
primary key(AccId)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `Client` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`CltDate` varchar(11) NOT NULL,
`CltName` varchar(100) NOT NULL,
`CltEmail` varchar(150) NOT NULL,
`CltPhone` varchar(20) NOT NULL,
`CltOptions` varchar(9) NOT NULL,
`AccId` int(11) NOT NULL,
`WorkId` int(11) NOT NULL,
primary key(Id), foreign key(AccId) references Accessories(AccId),
foreign key(WorkId) references Work(WorkId)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
The html form is below
<form name="form" method="psot" action="save.php">
<h1>Client</h1>
<label>
<input type="text" name="txtName" id="txtName" onfocus="if(this.value=='Name'){this.value='';}" onblur="if(this.value==''){this.value='Name';}" value="Name" title="Name" required="">
</label>
<label>
<input type="date" id="txtDate" name="txtDate">
<?php
$month = date('m');
$day = date('d');
$year = date('Y');
$today = $year . '-' . $month . '-' . $day;
?>
</label>
<label>
<input type="text" name="txtEmail" id="txtEmail" onfocus="if(this.value=='Email Address'){this.value='';}" onblur="if(this.value==''){this.value='Email Address';}" value="Email Address" title="Email Address" required="">
</label>
<label>
<input type="text" name="txtPhone" id="txtPhone" onfocus="if(this.value=='Phone Number'){this.value='';}" onblur="if(this.value==''){this.value='Phone Number';}" value="Phone Number" title="Phone Number" required="">
</label>
<label>
<select name="txtOptions">
<option value="">Select Options</option>
<option value="OptOne">Option one</option>
<option value="OptTwo">Option two</option>
<option value="OptThree">Option three</option>
<option value="OptFour">Option four</option>
</select>
</label>
<h1>Work</h1>
<label>
<input type="checkbox" name="txtWork" value="one">One<br />
<input type="checkbox" name="txtWork" value="two">Two<br />
<input type="checkbox" name="txtWork" value="three">Three<br />
<input type="checkbox" name="txtWork" value="four">Four<br />
</label>
<h1>Accessories</h1>
<table class="table table-bordered">
<thead>
<tr>
<th></th>
<th>Product Name</th>
<th>Price</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox" name="AccId[]" value="Car"></td>
<td>Accessory One
<input type="hidden" name="prodname[]" value="Car">
</td>
<td><input type="number" name="prod_price[]" class="form-control"></td>
<td><input type="number" name="prod_qty[]" class="form-control"></td>
</tr>
<tr>
<td><input type="checkbox" name="AccId[]" value="Bike"></td>
<td>Accessory Two
<input type="hidden" name="prodname[]" value="Bike">
</td>
<td><input type="number" name="prod_price[]" class="form-control"></td>
<td><input type="number" name="prod_qty[]" class="form-control"></td>
</tr>
<tr>
<td><input type="checkbox" name="AccId[]" value="Accessories"></td>
<td>Accessory Three
<input type="hidden" name="prodname[]" value="Accessories">
</td>
<td><input type="number" name="prod_price[]" class="form-control"></td>
<td><input type="number" name="prod_qty[]" class="form-control"></td>
</tr>
</tbody>
</table>
<label>
<input type="submit" name="Submit" id="Submit" value="Submit">
</label>
</form>
And the php file for saving the data is below
<?php
$con = mysqli_connect('localhost', 'root', 'password','database');
// get the post records
$txtName = $_POST['txtName'];
$txtDate = $_POST['txtDate'];
$txtEmail = $_POST['txtEmail'];
$txtPhone = $_POST['txtPhone'];
$txtOptions = $_POST['txtOptions'];
$txtWork = $_POST['txtWork'];
$checked_array=$_POST['AccId'];
foreach ($_POST['prodname'] as $key => $value)
{
if(in_array($_POST['prodname'][$key], $checked_array))
{
$prodname=$_POST['prodname'][$key];
$prod_price= $_POST['prod_price'][$key];
$prod_qty= $_POST['prod_qty'][$key];
}
}
// database insert SQL code
$sql1 = "INSERT INTO `ClientDets` (`Id`, `CltName`, `CltDate`, `CltEmail`, `CltPhone`, `CltOptions`)
VALUES ('0', '$txtName', '$txtDate', '$txtEmail', '$txtPhone', 'txtOptions')";
$sql2 = "INSERT INTO `Work` (`WorkId`, `WrkWork`)
VALUES ('0', '$txtWork')";
$insertqry="INSERT INTO `Accessories`( `AccId`, `product_name`, `product_price`, `product_quantity`) VALUES ('0', $prodname','$prod_price','$prod_qty')";
// insert in database
$rs1 = mysqli_query($con, $sql1);
$rs2 = mysqli_query($con, $sql2);
$rs3 = mysqli_query($con,$insertqry);
if($rs1 AND $rs2 AND $rs3)
{
/*echo "Contact Records Inserted";*/
header("location: index.php");
}
?>
I am not sure where I am going wrong