我想使用 qt5 界面中的“QNetworkAccessManager”将数据发布到我的 php 文件中,但数据没有发送。我正在尝试保存发送到数据库的数据。当我运行 php 文件时,它正在录制。当我想发送数据不去。我试过这种方法,没有结果。
senddata.cpp 代码
#include "sendata.h"
#include "ui_sendata.h"
#include <QUrlQuery>
#include "mainwindow.h"
QNetworkRequest request;
sendata::sendata(QWidget *parent) :
QDialog(parent),
ui(new Ui::sendata)
{
ui->setupUi(this);
nams= new QNetworkAccessManager(this);
request.setUrl(QUrl("http://localhost/phpserver/mysqlsorgu.php"));
request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/x-www-form-urlencoded"));
replys = nams->get(request);
}
void sendata::onResult()
{
replys->deleteLater();
if (replys->error() != QNetworkReply::NoError) {
qDebug() << "Hataa!";
return;
}
qDebug() << "Response!";
}
void sendata::on_pushButton_clicked()
{
query.clear();
query.addQueryItem("pname", ui->nameline->text());
query.addQueryItem("pusername", ui->usernameline->text());
query.addQueryItem("pemail", ui->emailline->text());
query.addQueryItem("pphone", ui->phoneline->text());
query.addQueryItem("pwebsite", ui->websiteline->text());
query.addQueryItem("padress", ui->adressline->text());
query.addQueryItem("pcompany", ui->companyline->text());
QByteArray postData;
postData.append(query.toString());
nams->post(request,postData);
//nams->post(request, query.query().toUtf8());
}
发送数据.php 代码
<?php
header("Content-Type:application/json");
$database_name = 'employer';
$database_user = 'root';
$database_password = '';
$database_host = 'localhost';
$name = $_POST['pname'];
$username = $_POST['pusername'];
$email = $_POST['pemail'];
$phone = $_POST['pphone'];
$website = $_POST['pwebsite'];
$adress = $_POST['padress'];
$company = $_POST['pcompany'];
$conn = mysqli_connect($database_host, $database_user,$database_password,$database_name );
if(!$conn) {
die("Error, could not connect: " . mysqli_connect_error());
}
else{
$sql = "INSERT INTO data (name, username, email,phone,website,address,company) VALUES
('$name', '$username', '$email', '$phone', '$website', '$adress', '$company')";
if(mysqli_query($conn, $sql)){
echo "Records added successfully.";
} } mysqli_close($conn);?>