4

I am trying to insert data in 2 tables using one form.

This is my form

<form action="don.php" method="post">
<tr><td>
    <p>
        <label for="nume">Nume:</label></td>
        <td><input type="text" name="nume" id="nume" autocomplete="off"></td>
    </p>
    </tr>
    <tr><td>
    <p>
        <label for="prenume">Prenume:</label></td>
       <td> <input type="text" name="prenume" id="prenume" autocomplete="off"></td>
    </p></tr>
    <tr><td>
    <p>
        <label for="grupa">Numar telefon:</label></td>
        <td><input type="text" name="numar" id="numar" autocomplete="off"></td>
    </p></tr>
    <tr><td>
    <p>
        <label for="grupa">Suma:</label></td>
        <td><input type="text" name="suma" id="suma" autocomplete="off"></td>
    </p></tr>
    <tr><td>
    <p>
        <label for="grupa">Data:</label></td>
        <td><input type="text" name="data" id="data" autocomplete="off"></td>
    </p></tr>
    <tr><td>
    <p>
        <label for="grupa">IBAN:</label></td>
        <td><input type="text" name="iban" id="iban" autocomplete="off"></td>
    </p></tr>
    <tr><td><input type="submit" name="submit" value="Donează" onclick="alert('Operatiune finalizata cu succes. Va multumim!')"></td>
</form>

And this is my PHP code

<?php
if(isset($_POST['submit'])){
    $con=mysql_connect("localhost","root","");
    if(!$con)
    {
        die("Nu se poate face conexiunea la baza de date" . mysql_error());
    }

    mysql_select_db("laborator",$con);
    $sql="INSERT INTO donator (nume, prenume, numar_telefon) VALUES ('$_POST[nume]','$_POST[prenume]','$_POST[numar]')";
    $sql="INSERT INTO donatie (suma, data_donatie, IBAN) VALUES ('$_POST[suma]','$_POST[data]','$_POST[iban]')";
    mysql_query($sql,$con);
    mysql_close($con);
}
?>

When I press the submit button it shows me the alert that my dates were inserted, but only the second INSERT works. Table donator is empty. What should I do to fix this problem?

AAEM
  • 1,837
  • 2
  • 18
  • 26
Eleanordum
  • 125
  • 1
  • 3
  • 10

5 Answers5

17

You must call mysql_query() for every query.

$sql1 = "INSERT INTO donator ...";
$sql2 = "INSERT INTO donatie ...";
mysql_query($sql1, $con);
mysql_query($sql2, $con);

Important

mysql_query() is deprecated! Please use mysqli_query() http://php.net/manual/de/book.mysqli.php

You can also use mysqli_multi_query() http://php.net/manual/de/mysqli.multi-query.php

$query = "INSERT INTO donator ...; INSERT INTO donatie ...;";
mysqli_multi_query($link, $query);
Scoutman
  • 1,630
  • 12
  • 20
0

You run your queries with the same variable $sql. You should call them differentely like that and call them after.

$sql="INSERT INTO donator (nume, prenume, numar_telefon) VALUES ('mysql_real_escape_string($_POST[nume])','mysql_real_escape_string($_POST[prenume])','mysql_real_escape_string($_POST[numar])')";
$sql2="INSERT INTO donatie (suma, data_donatie, IBAN) VALUES ('mysql_real_escape_string($_POST[suma])','mysql_real_escape_string($_POST[data])','mysql_real_escape_string($_POST[iban])')";

Otherwise, you must change your post values by protecting them. Refer here

Then, you have to switch to new mysql syntax for PHP, like mysqli or PDO. THe mysql syntax you are using is deprecated.

Community
  • 1
  • 1
w3spi
  • 4,380
  • 9
  • 47
  • 80
0

You're overwriting your $sql variable. Save them as separate variables, or run the first query before declaring the second one.

That said, you should REALLY consider both switching two either PDO or mysqli for your queries rather than the mysql extension and also looking into prepared statements and properly sanitizing strings, because you're very vulnerable to sql injections.

Shane Lessard
  • 655
  • 1
  • 8
  • 18
0

Use like this It will work perfectly.

<?php
$GLOBALS['server']="localhost";
$GLOBALS['username']="root";
$GLOBALS['password']="*****";
$GLOBALS['database']="performance";

$GLOBALS['conn']= mysqli_connect($server,$username,$password,$database);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
//echo "Connected successfully";    

$GLOBALS['db'] = mysqli_connect($server,$username,$password,$database);

$sql="INSERT INTO animals (id,name) VALUES('2211','vidya');";
$sql .="INSERT INTO birds (id,fame) VALUES('2211','viddi');";
//mysqli_query($conn,$sql);
//mysqli_query($conn,$sql1);
if (mysqli_multi_query($GLOBALS['db'], $sql)) {
    echo "New records created successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);
?>
Vidya
  • 63
  • 2
  • 10
  • 1
    Welcome to SO. It would be nice if you could also explain as to why your suggestion should work and where the OP might've made a mistake. – Exception_al Feb 15 '18 at 10:11
0

This is a example of one form data insert for a multiple tables.

Here html form has one submit button. "ok" name is the submit button name.

<?php
ob_start();
session_start();
$con=mysqli_connect("localhost","root","","cultureframework");
if(mysqli_connect_errno())
{
    echo "Failed to connect to MySql:".mysqli_connect_error();
}
?>
<?php
if(isset($_POST['ok']))
{
    $ip=$_POST['ip'];
    $date=$_POST['date'];
    $gender=$_POST['gender'];
    $age=$_POST['age'];
    $tenure=$_POST['tenure'];
    //-------------------------------------
    $caring_past_1=$_POST['caring_past_1'];
    $caring_present_1=$_POST['caring_present_1'];
    $caring_future_1=$_POST['caring_future_1'];
    $caring_past_2=$_POST['caring_past_2'];
    $caring_present_2=$_POST['caring_present_2'];
    $caring_future_2=$_POST['caring_future_2'];

    //-------------------------------------

        $insert="INSERT INTO `generalinfo`(`ip`,`datez`,`gender`,`age`,`tenure`) VALUES('$ip','$date','$gender','$age','$tenure')";
        $query=mysqli_query($con,$insert);

        $ins="INSERT INTO `caring`(`caring_past_1`,`caring_present_1`,`caring_future_1`,`caring_past_2`,
        `caring_present_2`,`caring_future_2`) VALUES('$caring_past_1','$caring_present_1','$caring_future_1','$caring_past_2','$caring_present_2','$caring_future_2')";
        $quy=mysqli_query($con,$ins);   
}

?>