简体   繁体   English

使用一种形式将数据插入到多个表中

[英]Insert data into multiple tables using one form

I am trying to insert data in 2 tables using one form.我正在尝试使用一种形式在 2 个表中插入数据。

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 代码

<?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.当我按下提交按钮时,它会显示我的日期已插入的警报,但只有第二个INSERT有效。 Table donator is empty.donator是空的。 What should I do to fix this problem?我应该怎么做才能解决这个问题?

You must call mysql_query() for every query.您必须为每个查询调用mysql_query()

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

Important重要的

mysql_query() is deprecated! mysql_query()已弃用! Please use mysqli_query() http://php.net/manual/de/book.mysqli.php请使用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您也可以使用mysqli_multi_query() http://php.net/manual/de/mysqli.multi-query.php

$query = "INSERT INTO donator ...; INSERT INTO donatie ...;";
mysqli_multi_query($link, $query);

You run your queries with the same variable $sql .您使用相同的变量$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.然后,您必须为 PHP 切换到新的 mysql 语法,例如 mysqli 或 PDO。 THe mysql syntax you are using is deprecated.您正在使用的 mysql 语法已被弃用。

You're overwriting your $sql variable.您正在覆盖 $sql 变量。 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.也就是说,您真的应该考虑为您的查询切换两个 PDO 或 mysqli,而不是 mysql 扩展,并且还要查看准备好的语句并正确清理字符串,因为您很容易受到 sql 注入的影响。

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);
?>

This is a example of one form data insert for a multiple tables.这是为多个表插入一个表单数据的示例。

Here html form has one submit button.这里 html 表单有一个提交按钮。 "ok" name is the submit button name. “ok”名称是提交按钮名称。

<?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);   
}

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM