i'm beginner in PHP and i having problems with my code..i want to make a little fee and income system.its a web based app.. i want to insert data in two different tables 1) fees and 2)income using options..here im taking two steps (1) select data from database (2) inserting in data base. 1st step is working good problem with 2nd step the data is inserted in both 1) fees and 2)income, not all records saved i click for saving 3 records but there is only one or two record save.
select data from database
<?php
if(isset($_POST['classes'])){
$cla=$_POST['classes'];}$sql = "SELECT class,std_name,roll_no,monthly_fee from students where class = '$cla'";
$result = $conn->query($sql);if ($result->num_rows > 0) {$counter = 0;while($row = $result->fetch_assoc()) {?><tr><td><?php echo $row["roll_no"]; ?><input type="hidden" value="<?php echo $row["roll_no"]; ?>" name="roll_no[]"></td><td><?php echo $row["std_name"]; ?><input type="hidden" value="<?php echo$row["std_name"]; ?>" name="std_name[]"></td><td><?php echo $row["class"]; ?><input type="hidden" value="<?php echo $row["class"]; ?>" name="class[]"></td><td><?php echo $row["monthly_fee"]; ?><input type="hidden" value="<?php echo$row["monthly_fee"]; ?>" name="monthly_fee[]"></td><td><select name="status[<?php echo $counter; ?>]"><option >--Select Status--</option>
<option value="Paid">Paid</option>
<option value="Unpaid">Unpaid</option>
Insert into Database i think the problems is with this code here i want to store data with condition
if (isset($_POST['submit'])) {
foreach ($_POST['status'] as $state => $status) {
$roll_no = $_POST['roll_no'][$state];
$std_name = $_POST['std_name'][$state];
$class = $_POST['class'][$state];
$monthly_fee = $_POST['monthly_fee'][$state];
$stat = $_POST['status'][$state];
$date = date('Y-m-d H:i:s');
$sql = "INSERT INTO fees(std_id,std_name,std_class,fee_status,monthly_fee,taken_date) VALUES( '$roll_no','$std_name','$class','$stat','$monthly_fee','$date')";
if ($state == 'Paid') {
$sql = "INSERT INTO income(std_fee) values ('$monthly_fee')";if ($conn->query($sql) === TRUE) {$flag = 1;}else{echo "error".$sql."<br>".$conn->error;} }}}?>
i hope you will understand