I am trying to insert data in 2 tables using one form.
This is my form
< 相关标签: 5条回答 一生所求 2020-12-29 18:17 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. 0 讨论(0) 发布评论: 提交评论 加载中... 忘掉有多难 2020-12-29 18:22 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); ?> 0 讨论(0) 发布评论: 提交评论 加载中... 花落未央 2020-12-29 18:25 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); 0 讨论(0) 发布评论: 提交评论 加载中... 走了就别回头了 2020-12-29 18:31 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. 0 讨论(0) 发布评论: 提交评论 加载中... 一向 2020-12-29 18:39 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); } ?> 0 讨论(0) 发布评论: 提交评论 加载中... 验证码 看不清? 提交回复 热议问题
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.
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); ?>
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
mysqli_query()
You can also use mysqli_multi_query() http://php.net/manual/de/mysqli.multi-query.php
mysqli_multi_query()
$query = "INSERT INTO donator ...; INSERT INTO donatie ...;"; mysqli_multi_query($link, $query);
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.
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); } ?>