0

I want to pass some datas from table to another table when every month starts..

So I tried this method. but I'm getting an error in that pass method.

$date = date('d');

if ($date == 01) {

$sql = "SELECT student_name, payment FROM student_detail";
$result = $conn->query($sql);

if ($result->num_rows > 0) {

    while ($row = $result->fetch_assoc()) {
        
       $sql = "INSERT INTO arrears (student_name, amount) VALUES ('echo $row [student_name]', 'echo $row [payment]')";
    }

}

This is an error

Notice: Array to string conversion in C:\xampp\htdocs\table_form\inc\header.php on line 38

But I'm sure that there is an error in this

'echo $row [student_name]', 'echo $row [payment]

and how can I do that?

Jazim Max
  • 111
  • 1
  • 10
  • **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 06 '20 at 12:17
  • Does this answer your question? [How to include a PHP variable inside a MySQL statement](https://stackoverflow.com/questions/7537377/how-to-include-a-php-variable-inside-a-mysql-statement) – Dharman Sep 06 '20 at 12:17

1 Answers1

0

You should not use echo within string expression. You should to concatenate strings an variables into query string like:

$sql = "INSERT INTO arrears (student_name, amount) VALUES ('" . $row['student_name'] . "', '" . $row ['payment'] ."')";
Slava Rozhnev
  • 9,510
  • 6
  • 23
  • 39