1

I am not able to give post request using axios in react while i am giving post request to my php server and wanted to insert data in mysql fetched by post request. this is my code.

const url = "http://localhost:80/react-backenda/";
const axios = require('axios');
axios.post(url, { 
  SendBy: 'username', 
  Sendto: 'got_name', 
  Msg: 'input'
})
.then(res=> console.log(res.data))
.catch(err => console.log(err));

i am getting this in my console:

Notice: Undefined index: Msg in C:\xampp\htdocs\react-backenda\index.php on line 10

Notice: Undefined index: Sendby in C:\xampp\htdocs\react-backenda\index.php on line 11

Notice: Undefined index: Sendto in C:\xampp\htdocs\react-backenda\index.php on line 12
done

this is my php script:

    <?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "msg-clone";
$conn = mysqli_connect($servername, $username, $password, $database);
if(!$conn){
    die("could not connect to server!");
}
$recText = $_POST['Msg'];
$sendby = $_POST['Sendby'];
$sendto = $_POST['Sendto'];
     echo $recText;
     $sql = "INSERT INTO `messages` (`id`, `text`, `sendby`, `sendto`, `time`) VALUES (NULL, '$recText', '$sendby', '$sendto', current_timestamp());";
     $res = mysqli_query($conn, $sql);
     if ($res){
         echo "done";
     }else {
         echo "err";
     }
?>

help to solve this issue.

Mrunmai Dahare
  • 128
  • 1
  • 8
  • 2
    **Warning:** You are wide open to [SQL Injections](https://stackoverflow.com/a/60496/1839439) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Sep 05 '20 at 17:39
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Dharman Sep 05 '20 at 17:40
  • @Dharman No sir/mam that is just related to php, not wht i am looking for. – Mrunmai Dahare Sep 05 '20 at 17:47
  • Your question is tagged PHP and MySQL. You have shown us PHP error and PHP code. If this is not what you are asking for then please edit it and explain better. – Dharman Sep 05 '20 at 17:48

1 Answers1

1

By default, Axios serializes your object to JSON. It means that you'll have to receive it as a JSON in you PHP backend, or config your axios instance to send it as a form data as a browser would on a simple form.

To receive it as JSON on PHP you would do it like this:

$data = json_decode(file_get_contents('php://input'), true);

$recText = $data['Msg'];
$sendby = $data['Sendby'];
$sendto = $data['Sendto'];

But if you want to handle it on frontend, it would be something like this:

var formData = new FormData();
formData.append("SendBy", "username");
formData.append("Sendto", "got_name");
formData.append("Msg", "input");

axios.post(url, formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
})

Note: As Dharman kindly warned you, your backend code has seriously security problems related to SQL Injection, it would also be nice to isolate your connection and query handling em separate Classes/Files. If you're developing a production application or even just learning, pay attention to those details, there are also some beginner's friend frameworks that allow you to easily create a good structure for small or bigger projects like Laravel.

Samples:

R. Zanel
  • 108
  • 1
  • 5