0

I have to save more than one row of data into database table.

I wrote this code for that. But when i run this code, two rows will be saved into two rows of db table, but every other row is same as the last row data.

i.e. that is the last row data is overwriting every other row data.

$values = array();

if (isset($_POST['sub_save'])) {
    $row_count = $_POST['rowcount'];

    while ($row_count > 0) {
        for ($r = 1; $r <= 2; $r++) {
            $val = $_POST['txt'.$row_count.$r];
            $values[] = $val;
        }

        $row_count = $row_count - 1;
        $sql = "insert into timesheet_entry (name, address) values ('$values[0]', '$values[1]')";

        if (mysql_query($sql)) {
            echo "inserted";
        } else {
            echo "fail";
        }
    }
}
6
  • The way you are structuring the data in the posting HTML is horrible and likely the cause of the problem here. Commented Nov 6, 2015 at 12:45
  • could tell me where should i change my code? Commented Nov 6, 2015 at 13:07
  • From the beginning - but I suspect that's not the answer you're looking for. Don't try to amalgamate pre-generated, surrogate keys into a composite key (or if you must, at least delimit the components). Commented Nov 6, 2015 at 13:11
  • omg im a begginer can you tell me in a simpler way!!!!!!!!!!!!!!!!! Commented Nov 6, 2015 at 13:28
  • I can't tell you how you you should structure unless I know a lot more about it. Commented Nov 6, 2015 at 13:30

2 Answers 2

1

You aren't resetting your values array inside the loop so values[0] and values[1] will always have the first to values.

if (isset($_POST['sub_save'])) {
    $row_count = $_POST['rowcount'];

    while ($row_count > 0) {
        $values = array();
        for ($r = 1; $r <= 2; $r++) {
            $val = $_POST['txt'.$row_count.$r];
            $values[] = $val;
        }

        $row_count = $row_count - 1;
        $sql = "insert into timesheet_entry (name, address) values ('$values[0]', '$values[1]')";

        if (mysql_query($sql)) {
            echo "inserted";
        } else {
            echo "fail";
        }
    }
}

On a sidenote I would recommend looking into the PDO extension and parameterised queries as mysql_ is deprecated and the above code is vulnerable to SQL injection

Sign up to request clarification or add additional context in comments.

Comments

0

You are mixing mysql and mysqli:

$conn = mysqli_connect("localhost", "root", ""); 
$db = mysqli_select_db("dbname");

if (mysqli_query($sql)) {
   echo "inserted";
} else {
   echo "fail";
}

1 Comment

ok i will correct it but the problem is rows are saving but the last row data overwriting the above rows..can you help me with that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.