Hi! I am trying to insert the data froma a form into 3 tables. Although I get no errors only the first insert works. Could you please help me and tell me what am I doing wrong?

$query_add = "insert into autoturisme values('', '$producator', '$model', '$categorie', '$nume_poza', '$motor', '$consum', '$locuri', '$capacitate', '$tip', '$transmisie', '1', '1', '$km', '$data_revizie', '$comentarii')";

mysql_query($query_add) or die("Problema Add auto: ".mysql_error());

    //insert into table facilitati
    if(isset($_POST['aer_conditionat']) && $_POST['aer_conditionat'] == '1') 
            { $ac=1;}
        else {$ac=0;}
    if(isset($_POST['inchidere_centralizata']) && $_POST['inchidere_centralizata'] == '1') 
            { $ic=1;}
        else {$ic=0;}
    if(isset($_POST['geamuri_electrice']) && $_POST['geamuri_electrice'] == '1') 
            { $ge=1;}
        else {$ge=0;}
    if(isset($_POST['alarma']) && $_POST['alarma'] == '1') 
            { $a=1;}
        else {$a=0;}
    if(isset($_POST['servo_directie']) && $_POST['servo_directie'] == '1') 
            { $sd=1;}
        else {$sd=0;}
    if(isset($_POST['cd_player']) && $_POST['cd_player'] == '1') 
            { $cd=1;}
        else {$cd=0;}
    if(isset($_POST['airbag']) && $_POST['airbag'] == '1') 
            { $air=1;}
        else {$air=0;}
    if(isset($_POST['priza']) && $_POST['priza'] == '1') 
            { $p=1;}
        else {$p=0;}
    if(isset($_POST['abs']) && $_POST['abs'] == '1') 
            { $abs=1;}
        else {$abs=0;}

    $id=mysql_insert_id();
    $query_add_facilitati="insert into facilitati values('$id', '$ac', '$ic', '$ge', '$a', '$sd', '$cd', '$air', '$p', '$abs')";

    mysql_querry($querry_add_facilitati) or die("Problema Add in tabela facilitati ".mysql_error()); 

    //insert into  tabel tarife
        $t_1_3      = $_POST['1_3_zile'];
        $t_4_7      = $_POST['4_7_zile'];
        $t_8_14     = $_POST['8_14_zile'];
        $t_15_21    = $_POST['15_21_zile'];
        $t_p_21     = $_POST['peste_21_zile'];
        $query_add_tarife = "insert into tarife values('$id','$t_1_3','$t_4_7','$t_8_14','$t_15_21','$t_p_21')";
        mysql_query($query_add_tarife) or die("Eroare inserare in tabela tarife:".mysql_error());

Dani AI

Generated

There are two immediate, visible problems in the code shown by that explain why only the first INSERT completes: a misspelled MySQL function name and a mismatched variable being passed into that call. Those errors will stop the script (so the third INSERT never runs). ’s advice to echo or log the constructed SQL is useful — do that and enable error reporting so PHP shows the actual failure instead of silently stopping.

Fixes and improvements to apply now:

  • Correct the function/variable typos so the second INSERT actually executes.
  • Call the “get last insert id” function immediately after the successful first insert and before any other queries run.
  • Always check the return value of each query and surface errors (or use exceptions).
  • Specify column names in your INSERTs instead of relying on table order.
  • If any column name starts with a digit or contains special characters (for example the posted form keys like 1_3_zile), wrap that column name in backticks or rename the column to a safe identifier.
  • Consider using PDO or MySQLi with prepared statements and transactions so either all three inserts succeed or none do.

Example pattern using PDO (prepared statements, get last insert id, transaction):

$pdo->beginTransaction();

$st = $pdo->prepare('INSERT INTO cars (make,model,year) VALUES (?, ?, ?)');
$st->execute([$make, $model, $year]);

$carId = $pdo->lastInsertId();

$st = $pdo->prepare('INSERT INTO features (car_id,ac,abs) VALUES (?, ?, ?)');
$st->execute([$carId, $ac, $abs]);

$st = $pdo->prepare('INSERT INTO rates (car_id, d1, d2) VALUES (?, ?, ?)');
$st->execute([$carId, $d1, $d2]);

$pdo->commit();

References: PDO prepared statements and transaction usage can be found in the PHP manual (PDO prepared statements, PDO::lastInsertId).

You echo query on screen copy it and try to run in mysql directly may be on mysql command prompt or phpmyadmin. Check whether your query works directly to backend or not. keep echo statment before calling mysql_query() function.

echo $query_add_tarife."<br>";

echo $query_add_facilitati."<br>";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.