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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30466422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:48:20  来源:igfitidea点击:

Insert data into multiple tables using one form

phpmysql

提问by Eleanordum

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 INSERTworks. Table donatoris empty. What should I do to fix this problem?

当我按下提交按钮时,它会显示我的日期已插入的警报,但只有第二个INSERT有效。桌子donator是空的。我应该怎么做才能解决这个问题?

回答by Scoutman

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! Please use mysqli_query()http://php.net/manual/de/book.mysqli.php

mysql_query()已弃用!请使用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);

回答by w3spi

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

您使用相同的变量运行查询$sql。你应该这样称呼他们,然后再称呼他们。

$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.

然后,您必须为 PHP 切换到新的 mysql 语法,例如 mysqli 或 PDO。您正在使用的 mysql 语法已被弃用。

回答by Shane Lessard

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

您正在覆盖 $sql 变量。将它们保存为单独的变量,或者在声明第二个之前运行第一个查询。

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 注入的影响。

回答by Vidya

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

回答by thisal selaka

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.

这里 html 表单有一个提交按钮。“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);   
}

?>