php - 如何在循环中 $_POST

标签 php mysql forms

我当前的主机不允许远程 mysql 访问,因此我需要通过让 server1 上的 script1 与 server2 上的 script2 通信来解决这个问题。我正在尝试将发布数据发送到 script2,然后 script2 获取该数据并将其放入 mysql 中。为了让我的问题简单,我将代码简化为:

脚本1

for ( $counter = $counternumbernao; $counter <= $amountofcomments; $counter += 1) 
{
echo'
<form action="http://server2.x.com/form-receive.php" method="post">
<INPUT TYPE=HIDDEN NAME="comment_content" value=$comment_content>
<INPUT TYPE=HIDDEN NAME="comment_date" value=$comment_date">
<input type="submit" />
</form>
';
}

我怎样才能改变这段代码,以便每次循环发生时它都会自动将$_POST数据发送到script2,然后将其放入mysql?我认为没有必要包含 script2,因为它对这个问题并不重要。

最佳答案

为什么不使用 cURL 将 FORM 数据发送到 server2。它允许您发送 HTTP 请求。尝试以下脚本:

$url = "http://server2.x.com/form-receive.php";
$postvars = "comment_content=$comment_content&comment_date=$comment_date";

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST ,1);
curl_setopt($ch, CURLOPT_POSTFIELDS ,$postvars);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1); 
curl_setopt($ch, CURLOPT_HEADER ,0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,1);  
$data = curl_exec($ch);

关于php - 如何在循环中 $_POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6452294/

相关文章:

php - 使用 html 5 或 php 来操作服务器上的文件

mysql - couchdb 与 mysql 速度

javascript - 设置隐藏输入的值

javascript - 输入 url 的提交按钮

django - django无法在用于指定中介模型的ManyToManyField上设置值。改用Manager

php - 将多个变量输入表中,分配给每个适当的记录

执行数值数组时 PHP PDO 错误

php - 查询或 PHP 中 mysql 的列总计?

mysql - Pentaho MySQL 8 连接错误 Driver class 'org.gjt.mm.mysql.Driver' could not be found

mysql - Ubuntu 16.04 Node.JS 应用程序无法连接到 MySQL 数据库 - 错误 : connect ECONNREFUSED