0

I want to post data to my php file using "QNetworkAccessManager" from the qt5 interface, but the data is not going. I am trying to save the data I sent to the database. When I run the php file it is recording. when i want to send data not going. I tried this way, no results.

sendata.cpp code

 #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());

}

Senddata.php code

   <?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);?>
  • **Warning:** Your code is vulnerable to SQL Injection attacks. You should use parameterised queries and prepared statements to help prevent attackers from compromising your database by using malicious input values. http://bobby-tables.com gives an explanation of the risks, as well as some examples of how to write your queries safely using PHP / mysqli. **Never** insert unsanitised data directly into your SQL. The way your code is written now, someone could easily steal, incorrectly change, or even delete your data. – ADyson Dec 17 '21 at 11:13
  • https://phpdelusions.net/mysqli also contains good examples of writing safe SQL using mysqli. See also the [mysqli documentation](https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php) and this: [How can I prevent SQL injection in PHP?](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) . Parameterising your queries will also greatly reduce the risk of accidental syntax errors as a result of un-escaped or incorrectly quoted input values. Right now a single `'` in the input could break your query! – ADyson Dec 17 '21 at 11:13
  • Never configure your web app to login to the database as `root`. Root can do whatever it likes, so on top of the SQL injection vulnerabilities this just leaves your database an open book for hackers. Instead create a separate user account specifically for this application which has only the permissions it actually _needs_ in order to work properly. Don't even use the root account as a shortcut during development or testing, because you need to test your account permissions as well - otherwise when you go live you might have unexpected errors relating to the user account setup. – ADyson Dec 17 '21 at 11:14
  • `header("Content-Type:application/json");` makes no sense, because your PHP code does not return JSON (it returns plain text) – ADyson Dec 17 '21 at 11:14
  • Add `mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);` before your `mysqli_connect()` command, and this will ensure that errors with your SQL queries are reported correctly to PHP. – ADyson Dec 17 '21 at 11:14
  • Anyway you need to give a clearer description of the problem and explain what you have done to debug it. "Not going" isn't clear or useful information. If writing in English is difficult for you, please find someone to help you translate. – ADyson Dec 17 '21 at 11:16
  • I reviewed the sites you suggested about secure query methods. I updated the code to be safe again. I created it as in the link you shared. But my code still doesn't work. I cannot send data. @ADyson thank you – Ersin Cengiz Dec 17 '21 at 13:24
  • @ADyson I am trying to post my data to php file from qt interface. but my code is not working. I'm trying to take the data I brought in json format on the php side and add it to the database. but the values ​​for the variables in the php file are not visible. The variables in the php file appear to be empty. – Ersin Cengiz Dec 17 '21 at 13:27
  • Ok. So have you tried logging what the C++ code is sending? Have you tried using a tool such as wireshark (or similar) to monitor the network traffic and see what's in the request? – ADyson Dec 17 '21 at 13:44
  • @ADyson No, I haven't tried it, but I think there is a detail I missed. I've seen it used like this in sample projects as well. The json data I sent appears as empty in the php file or does not go at all. I don't know what to do. I would be very happy if you could help. – Ersin Cengiz Dec 17 '21 at 13:47
  • You're not actually sending JSON, you're sending form-url-encoded data, but that's beside the point. You should a) look back at sample projects to compare what they've done with your code, and b) do the network debugging as I suggested, and c) add some logging to the C++ side to see what values are in the query data. What does the output of `query.toString()` look like, for example? – ADyson Dec 17 '21 at 13:52
  • @ADysonI reviewed my code again but couldn't find a problem. It looks like this in sample projects on the internet, but I can't send my data to the php file. I need to be getting the data from the post method. When I print the variables in the php file, they appear empty. – Ersin Cengiz Dec 19 '21 at 10:30
  • See, have you done any if the debugging of the C++ I suggested? It's not clear from your description and you haven't reported any results – ADyson Dec 19 '21 at 11:28
  • @ADyson The php code in the example works. I tested the php code. It doesn't work in the post part, the other parts are as it should be. – Ersin Cengiz Dec 20 '21 at 05:31
  • Ok. So you need to debug the c++ in detail then, as I've already mentioned twice – ADyson Dec 20 '21 at 08:33

0 Answers0