如何在单独的数据库中将值更新为三个表?

时间:2015-03-12 08:25:54

标签: php mysql

以下代码在我的本地主机上,但在远程主机中不起作用,其中第一个连接工作正常,其他数据库连接不起作用。如何连接多个mysql DB?

<?php
 $error=''; 
if(isset($_POST['submit']))
{ 
$sno=$_POST['sno'];
$date=$_POST['date'];
$item=$_POST['item'];
$name=$_POST['product'];    

include "connection/order_connection.php";  
$sql2="select item_code from warehouse_stock WHERE item_code='".$item."' and    sno!='".$sno."' ";
$result2=mysql_query($sql2);
if($result2 === FALSE)
{    
die(mysql_error()); // TODO: better error handling
}
if(mysql_fetch_array($result2)>= 1) 
{ 
echo  "Entered Item Code Already in Exists!<br/>";
echo '<script>
$(window).load(function () {
    window.setTimeout(function () {
    window.location.href = "list_products.php";
    }, 3500)
    }); 
    </script>'; 
}
else
{
// product insert into order and warehouse table
$sql1=("update warehouse_stock set date='".$date."',     product_name='".$name."', item_code='".$item."' where sno='".$sno."' ");
 $query1=mysql_query($sql1) or die(mysql_error());
 if($query1)
$sql2=("update order_stock set date='".$date."', product_name='".$name."',  item_code='".$item."' where sno='".$sno."' ");
$query2=mysql_query($sql2) or die(mysql_error());
mysql_close($con);

// product insert into us hub table
include "connection/us_connection.php"; 
$sql3=("update stock_bal set date='".$date."', product_name='".$name."', item_code='".$item."' where sno='".$sno."' ");
$query3=mysql_query($sql3) or die(mysql_error());
mysql_close($con);

// product insert into canada hub table
include "connection/canada_connection.php"; 
$sql4=("update stock_bal set date='".$date."', product_name='".$name."', item_code='".$item."' where sno='".$sno."' ");
$query4=mysql_query($sql4) or die(mysql_error());
mysql_close($con);
header('location:list_products.php');   
echo '<script>
$(window).load(function () {
window.setTimeout(function () {
    window.close();
}, 1500)
}); 
</script>';
//$error="Product details updated successfully!";

}
}
?>

是否有其他方法可以将值更新到表中。?

1 个答案:

答案 0 :(得分:0)

<?php
$error=''; 
if(isset($_POST['submit']))
{ 
$sno=$_POST['sno'];
$date=$_POST['date'];
$item=$_POST['item'];
$name=$_POST['product'];

// product insert into us hub table1    
$hostname="localhost";
$db_user="root";
$db_pwd="";
$dbname="jasmineus";

$con1 = mysql_connect($hostname,$db_user,$db_pwd);
if (!$con1)
{
  die('Could not connect: ' . mysql_error());
}

mysql_select_db($dbname,$con1); 
$sql3=("update stock_bal set date='".$date."', product_name='".$name."', item_code='".$item."' where sno='".$sno."' ");
$query3=mysql_query($sql3) or die(mysql_error());
mysql_close($con1);

// product insert into canada hub table2
$hostname="localhost";
$db_user="root";
$db_pwd="";
$dbname="jasminecanada";

$con3 = mysql_connect($hostname,$db_user,$db_pwd);
if (!$con3)
{
  die('Could not connect: ' . mysql_error());
}

mysql_select_db($dbname,$con3); 
$sql4=("update stock_bal set date='".$date."', product_name='".$name."', item_code='".$item."' where sno='".$sno."' ");
$query4=mysql_query($sql4) or die(mysql_error());
mysql_close($con3);

$yourURL="list_products.php";
echo ("<script>location.href='$yourURL'</script>");
exit();
//$error="Product details updated successfully!";

}
$yourURL="list_products.php";
echo ("<script>location.href='$yourURL'</script>");
exit();
die(mysql_error());
}
?>