-1

I'm trying to insert data into 2 tables in php mysqli but it inserts data in the first table fine but not inserting anything in the second where there are other columns as well in the second table.

Here is my code:

$sql = "INSERT INTO socio (name, age, dob, gender, year, stgroup, stadd) VALUES ('$stnam', '$stage', '$stdob', '$stgen', '$styer', '$stGr', '$stadd')";
$sql1 = "INSERT INTO parta (name, stgroup, year) VALUES ('$stnam', '$stGr', '$styer')";

$result = mysqli_query($con, $sql);
$result = mysqli_query($con, $sql1);

Is anything wrong in the above code? Any suggestions?

Kanna
  • 3
  • 4

2 Answers2

1

I can't really see what's exactly wrong with your code, it might happen that some of the columns on the parta table are not supposed to be null. With the code you provided, it's hard to tell, as there's no error handling at all. You might want to use transactions, then use proper error handling and also use prepared statements.

Try with this code that I have prepared for you.

<?php
$con = new mysqli("..."); // you should know this part already

$success = false;
try {

    $con->autocommit(FALSE);

    $con->begin_transaction();

    if ($sql = $con->prepare("INSERT INTO socio (name, age, dob, gender, year, stgroup, stadd) VALUES (?,?,?,?,?,?,?)")) {
        $sql->bind_param('sissss', $stnam, $stage, $stdob, $stgen, $styer, $stGr, $stadd);
        if (!$sql->execute()) {
            throw new Exception($sql->error);
        }

        if ($sql_two = $con->prepare("INSERT INTO parta (name, stgroup, year) VALUES (?,?,?)")) {
            $sql_two->bind_param('sss', $stnam, $stGr, $styer);
            if (!$sql_two->execute()) {
                throw new Exception($sql_two->error);
            }
        }
    }

    if ($con->commit()) {
        $success = true;
    } else {
        throw new Exception('Transaction commit failed...');
    }
}catch (Exception $ex) {
    try {
        // something went wrong,rollback and display message
        $con->rollback();
        echo $ex->getMessage();
    }
    catch (Exception $e) {
        echo $e->getMessage();
    }
}
$con->autocommit(TRUE);

if ($success) {
    echo "data successfully inserted";
}
?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34
  • Hey you are right its because I didn't mention other fields value as null that's why data is not inserted. I changed it to null and it works for now. – Kanna May 30 '17 at 09:38
-3

Try this. It could be because you have the same variable name for two different actions. I've just rename the second result as $result1

$sql = "INSERT INTO socio (name, age, dob, gender, year, stgroup, stadd) VALUES ('$stnam', '$stage', '$stdob', '$stgen', '$styer', '$stGr', '$stadd')";

$sql1 = "INSERT INTO parta (name, stgroup, year) VALUES ('$stnam', '$stGr', '$styer')";

$result = mysqli_query($con, $sql);
$result1 = mysqli_query($con, $sql1);
hans-könig
  • 553
  • 8
  • 10
  • 1
    The variable name has no effect here. OP does not check the variables or use them. You could have atleast improved OP's code to prepared statements. – Rotimi May 30 '17 at 08:20
  • I have already tried like that, but it won't work that way either. – Kanna May 30 '17 at 08:23