-3

I am a beginner in PHP and MySQL and don't know what's wrong. It says error on line 8:

syntax error unexpected 'FROM' (T_STRING)

Here is the code:

<?php
require "conn.php";

$category = $_POST["category"];
$fruit = $_POST["fruit"];
$cost = $_POST["cost"];

DELETE FROM `Temptable` WHERE image_path = "";
UPDATE Temptable SET (`category`, `fruit`, `cost`) VALUES ('$category','$fruit','$cost');
$mysql_qry = "INSERT INTO Datatable (`category`, `fruit`, `cost`) SELECT `categrory`, `fruit`, `cost` FROM `Temptable` WHERE `id` >= '1'";

if($Datatable->query($mysql_qry) === true) {
 echo "Successful";
}
else {
 echo "Error: " . $mysql_qry . "<br>" . $Datatable->error;
}
$Datatable->close();
$Temptable->close();
?>
Gholamali Irani
  • 4,391
  • 6
  • 28
  • 59
H. Bauer
  • 13
  • 6

2 Answers2

1

Error is being generated simply because you have put mysql queries directly into PHP code which PHP is unable to understand. You should put DELETE and UPDATE statements as string variable like you did for the INSERT statement

<?php
require "conn.php";

$category = $_POST["category"];
$fruit = $_POST["fruit"];
$cost = $_POST["cost"];

$delete_query = "DELETE FROM `Temptable` WHERE image_path = ''";
$update_query = "UPDATE Temptable SET (`category`, `fruit`, `cost`) VALUES ('$category','$fruit','$cost')";
$mysql_qry = "INSERT INTO Datatable (`category`, `fruit`, `cost`) SELECT `categrory`, `fruit`, `cost` FROM `Temptable` WHERE `id` >= '1'";

if($Datatable->query($mysql_qry) === true) {
    echo "Successful";
}
else {
    echo "Error: " . $mysql_qry . "<br>" . $Datatable->error;
}

$Datatable->close();
$Temptable->close();
?>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Devang Naghera
  • 731
  • 6
  • 13
0

The delete and update statements need to be commented out or created correctly as variables - you were getting an error because of those strings

<?php
    require "conn.php";

    $category = $_POST["category"];
    $fruit = $_POST["fruit"];
    $cost = $_POST["cost"];

    #DELETE FROM `Temptable` WHERE image_path = "";
    #UPDATE Temptable SET (`category`, `fruit`, `cost`) VALUES ('$category','$fruit','$cost');
    $mysql_qry = "INSERT INTO Datatable (`category`, `fruit`, `cost`) SELECT `categrory`, `fruit`, `cost` FROM `Temptable` WHERE `id` >= '1'";

    if($Datatable->query($mysql_qry) === true) {
        echo "Successful";
    }
    else {
        echo "Error: " . $mysql_qry . "<br>" . $Datatable->error;
    }
    $Datatable->close();
    $Temptable->close();
?>
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46