简体   繁体   English

将两个MySQL数据库转换为一种PHP形式

[英]Two mySQL databases to one PHP form

I am looking for a way to submit data to two mySQL databases from one PHP form. 我正在寻找一种从一种PHP表单向两个mySQL数据库提交数据的方法。

I want all the text information to go to one database and all the images to go to another. 我希望所有文本信息都转到一个数据库,所有图像都转到另一个数据库。

How would I go about doing this? 我将如何去做呢? (See the attached code below, which is still far from where I would like it to be): (请参阅下面的附加代码,离我想要的位置还很远):

<?php
/*
Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password)
*/
$link = mysqli_connect("****", "****", "****", "****");

// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Escape user inputs for security
$title = mysqli_real_escape_string($link, $_POST['title']);
$price = mysqli_real_escape_string($link, $_POST['price']);
$sqm = mysqli_real_escape_string($link, $_POST['sqm']);
$sqm_land = mysqli_real_escape_string($link, $_POST['sqm_land']);
$type = mysqli_real_escape_string($link, $_POST['type']);
$area = mysqli_real_escape_string($link, $_POST['area']);
$location = mysqli_real_escape_string($link, $_POST['location']);
$bedroom = mysqli_real_escape_string($link, $_POST['bedroom']);
$terrace = mysqli_real_escape_string($link, $_POST['terrace']);
$orientation = mysqli_real_escape_string($link, $_POST['orientation']);
$water = mysqli_real_escape_string($link, $_POST['water']);
$seaview = mysqli_real_escape_string($link, $_POST['seaview']);
$pool = mysqli_real_escape_string($link, $_POST['pool']);
$ownerinfo = mysqli_real_escape_string($link, $_POST['ownerinfo']);
$gaddress = mysqli_real_escape_string($link, $_POST['gaddress']);
$description = mysqli_real_escape_string($link, $_POST['description']);




// attempt insert query execution
$sql = "INSERT INTO property (title, price, sqm, sqm_land, type, area, location, bedroom, terrace, orientation, water, seaview, pool, ownerinfo, gaddress, description) VALUES 
('$title', '$price', '$sqm', '$sqm_land', '$type', '$area', '$location', '$bedroom', '$terrace', '$orientation', '$water', '$seaview', '$pool', '$ownerinfo', '$gaddress', '$description' )";
if(mysqli_query($link, $sql)){
    echo "Records added successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}




// close connection
mysqli_close($link);
?>

Any help would be greatly appreciated on this. 任何帮助将不胜感激。

完成其他操作后,您可以更改数据库:

mysqli_select_db($link,"new_db_to_choose");

just create two mysql connections: 只需创建两个mysql连接:

$link1 and $link2 $ link1和$ link2

And then call mysqli_query function with both connections: 然后使用两个连接调用mysqli_query函数:

mysqli_query($link1, $sql);
mysqli_query($link2, $sql);

You can make more than one database connection like this 您可以像这样建立多个数据库连接

$text_db = mysqli_connect("****", "****", "****", "****");

$img_db = mysqli_connect("****", "****", "****", "****");

You then just have to use the correct handle in any subsequent queries you run. 然后,您只需要在运行的任何后续查询中使用正确的句柄即可。

mysqli_query($text_db, 'the query for the test parts')

mysqli_query($img_db, 'the query for the images')

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

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