gpt4 book ai didi

php - 两个 MySQL 数据库到一个 PHP 表单

转载 作者:行者123 更新时间:2023-11-29 21:06:33 24 4
gpt4 key购买 nike

我正在寻找一种从一个 PHP 表单向两个 mySQL 数据库提交数据的方法。

我希望所有文本信息都转到一个数据库,所有图像都转到另一个数据库。

我该如何去做呢? (请参阅下面所附的代码,它距离我想要的位置还很远):

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

对此的任何帮助将不胜感激。

最佳答案

您可以像这样建立多个数据库连接

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

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

然后,您只需在运行的任何后续查询中使用正确的句柄即可。

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

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

关于php - 两个 MySQL 数据库到一个 PHP 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36773303/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com