how to keep inserting data continuous in second column if first column already inserted more than second

Hello friends please help to create coding against this little problem if possible:

I have a table which contains 4 columns:

| id | course | college | branch |

If we entered value in college column upto 10 value and in course we entered 4 value and in branch we enter 1 value so finally we entered 10 id

suppose I want to enter data in id(6,7,8,9,10,11,12) in column name: college , without gap

database name is "test" and table name is "admin_data"

I m using:

<?php
include "db.php";

if (isset($_POST["submit"])){
foreach ($_POST['course'] as $course) {
    $data1 = mysql_real_escape_string($course);
    mysql_query("INSERT INTO admin_data (course) VALUES ('$data1')") or            die(mysql_error());
}
}

if (isset($_POST["submit"])){
foreach ($_POST['college'] as $college) {
    $data2 = mysql_real_escape_string($college);
    mysql_query("INSERT INTO admin_data (college) VALUES ('$data2')") or   die(mysql_error());
}
}

if (isset($_POST["submit"])){
foreach ($_POST['branch'] as $branch) {
    $data1 = mysql_real_escape_string($branch);
    mysql_query("INSERT INTO admin_data (branch) VALUES ('$data3')") or    die(mysql_error());
}
}
header("location: data_enter.php");

?>

But not getting proper sollution....:(

Answers


Well I couldnt understand your question properly but why don't you just add data in once like

 "INSERT INTO admin_data (course,college,branch) VALUES ('$data1','$data2','$data3')"

This way you will not leave any empty cells or gaps. and if still you want to fill empty cells then i would suggest this query

if (isset($_POST["submit"])){
     $data1 = mysql_real_escape_string($_POST['course']);
     mysql_query("update admin_data set course = $data1 where id = (select id from admin_data where course = null limit 1);");
}

Note: don't use obselet MySQL move to PDO and MySQLi.


Need Your Help

LINQ to SQL for an existing database

sql linq linq-to-sql

I have a current project that uses LINQ to SQL to create a database and then to work with the data.