我是PHP MySQL的初学者。我想问一下如何将此代码用于INSERT DATA IN MULTIPLE TABLES
我将在单个FORM中使用它。
以下是单表的工作代码:
if (isset($_POST['btn-signup'])) {
//table 1
$variable1 = mysql_real_escape_string($_POST['column1']);
$variable2 = mysql_real_escape_string($_POST['column2']);
$variable3 = mysql_real_escape_string($_POST['column3']);
//table 2
$var1 = mysql_real_escape_string($_POST['columnT1']);
$var2 = mysql_real_escape_string($_POST['columnT2']);
$var3 = mysql_real_escape_string($_POST['columnT3']);
if(mysql_query("INSERT INTO tableName( column1, column2, column3 )
VALUES ('$variable1, '$variable2', '$variable3' )"))
{
?>
<script type="text/javascript">('ADDED SUCCESFULLY!');</script>
<?php
} else {
?>
<script type="text/javascript">('Failed!');</script>
<?php
但我想将INSERT的新代码添加到另一个表中。
我试过但仍然错误。
if(mysql_query("INSERT INTO tableName( column1, column2, column3 )
VALUES ('$variables', '$variable2', '$variable3' )")
("INSERT INTO tableName2( columnT1, columnT2, columnT3 )
VALUES ('$var1', '$var2, '$var3' )"))
答案 0 :(得分:2)
试试这个..
if(mysql_query("INSERT INTO tableName( column1, column2, column3 )
VALUES ('$variable1', '$variable2', '$variable3' )")) // '$variable1'
{
?>
<script type="text/javascript">alert('ADDED SUCCESFULLY!');</script> // add alert()
<?php
} else {
?>
<script type="text/javascript">alert('Failed!');</script> // add alert()
<?php }?>
second table :- ("INSERT INTO tableName2( columnT1, columnT2, columnT3 )
VALUES ('$var1', '$var2, '$var3' )"));
您可以Echo
查询
echo ("INSERT INTO tableName2( columnT1, columnT2, columnT3 )
VALUES ('$var1', '$var2, '$var3' )")) or die(mysql_error());
或
echo ("INSERT INTO tableName2( columnT1, columnT2, columnT3 )
VALUES ('$var1', '$var2, '$var3' )"));
INSERT INTO tableName2( columnT1, columnT2, columnT3 ) VALUES ('var1', 'var2, 'var3' ) // out put
您可以检查生成查询或错误的输出,复制生成查询并在
中运行phpmyadmin > select database > table > Sql > paste there > Go
如果它不起作用,请告诉我
答案 1 :(得分:1)
您可以再次使用mysql_query()
if(mysql_query("INSERT INTO tableName( column1, column2, column3 ) {
VALUES ('$variable1, $variable2, $variable3' )"))
echo 'Success insert into tableName';
} else {
echo 'Failed insert into tableName';
}
if(mysql_query("INSERT INTO tableName2( columnT1, columnT2, columnT3 )
VALUES ('$var1, $var2, $var3' )")) {
echo 'Success insert into tableName2';
} else {
echo 'Failed insert into tableName2';
}
答案 2 :(得分:1)
您需要在值周围添加引号。并使用mysql_affected_rows
检查是否插入了行
<?php
$query1=mysql_query("INSERT INTO tableName( column1, column2, column3)
VALUES('".$variables."','". $variable2."','". $variable3."')");
$row=mysql_affected_rows();
if($row>0){
echo "INSERT SUCESSFULLY";
}else{
echo "ERROR";
}
$query2=mysql_query("INSERT INTO tableName2( columnT1, columnT2, columnT3 )
VALUES ('".$var1."', '".$var2."', '".$var3."' )");
$row1=mysql_affected_rows();
if($row1>0){
echo "INSERT SUCESSFULLY";
}else{
echo "ERROR";
}
答案 3 :(得分:1)
我可能知道您在执行页面时遇到的错误可以在第二次插入查询之前尝试添加mysql_query
。
mysql_query("INSERT INTO tableName2( columnT1, columnT2, columnT3 )
VALUES ('$var1, $var2, $var3' )"))
答案 4 :(得分:1)
首先,您必须检查使用mysqli
或PDO
而不是mysql
。
$con = mysqli_connect("localhost", "user", "password", "databasename");
解决方案1:
您必须在单独的mysqli_query来电中执行此操作:
if(mysqli_query($con, "INSERT INTO tableName( column1, column2, column3 ) VALUES ('$variables', '$variable2', '$variable3' )") && mysqli_query($con, "INSERT INTO tableName2( columnT1, columnT2, columnT3 ) VALUES ('$var1', $var2', '$var3' )"))
{
...
}
使用两个mysqli_query
来电时,您应该看看mysql transactions。
解决方案2:
if(mysqli_multi_query($con, "INSERT INTO tableName2( columnT1, columnT2, columnT3 )
VALUES ('$var1', '$var2', '$var3'); INSERT INTO tableName2( columnT1, columnT2, columnT3 )
VALUES ('$var1', $var2', '$var3')"))
{
...
}
使用mysqli
时,您应该使用mysqli_prepare和mysqli_stmt_bind_param,而不是直接在SQL字符串中使用php变量。这将有助于您避免SQL注入。
答案 5 :(得分:1)
您无法在一个语句中对多个表执行插入操作。但是你可以使用交易。
START TRANSACTION;
INSERT INTO tableName (column1,column2,column3) VALUES ('$variable1','$variable2','$variable3');
INSERT INTO tableName2 (column1,column2,column3) VALUES ('$var1','$var2','$var3');
COMMIT;
答案 6 :(得分:0)
你可以试试这个,这对你来说可能看起来很复杂,但它更好:)
<?php
$link = mysqli_connect("localhost", "<username>", "<password>", "<database>");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
if (isset($_POST['btn-signup'])) {
// Your tables here
$tables = array('table1', 'table2', 'table3');
for ($x=0; $x < 3; $x++) {
switch ($x) {
// Escape user inputs for security and your columns
case "1":
$values[$tables[$x]] = array(mysqli_real_escape_string($link, $_POST['column1']), mysqli_real_escape_string($link, $_POST['column2']), mysqli_real_escape_string($link, $_POST['column3']));
$columns[$tables[$x]] = array('columnT1', 'columnT2', 'columnT3');
break;
case "2":
$values[$tables[$x]] = array(mysqli_real_escape_string($link, $_POST['columnT1']), mysqli_real_escape_string($link, $_POST['columnT2']), mysqli_real_escape_string($link, $_POST['columnT3']));
$columns[$tables[$x]] = array('columnT1', 'columnT2', 'columnT3');
break;
case "3":
$values[$tables[$x]] = array(mysqli_real_escape_string($link, $_POST['columnT1']), mysqli_real_escape_string($link, $_POST['columnT2']), mysqli_real_escape_string($link, $_POST['columnT3']));
$columns[$tables[$x]] = array('columnT1', 'columnT2', 'columnT3');
break;
}
// attempt insert query execution
$sql = "INSERT INTO ".$tables[$x]." (".$columns[$tables[$x]][0].", ".$columns[$tables[$x]][1].", ".$columns[$tables[$x]][2].") VALUES ('".$values[$tables[$x]][1]."', '".$values[$tables[$x]][2]."', '".$values[$tables[$x]][3]."')";
if(mysqli_query($link, $sql)){
echo "Records added successfully for ".$tables[$x].".";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
}
// close connection
mysqli_close($link);
}
?>