-1

MY inner Join sql query runs in my phpmyadmin . But not the PHP Code.

I have two tables. I want to update table 2 (opd) values when table 1 (registeration) values get updated . I found Inner Join. ### RUNS WELL INSIDE PHPMYADMIN ###

UPDATE opd INNER JOIN registeration ON opd.customerid = registeration.id SET opd.T_date = registeration.T_date,
opd.First_Name = registeration.First_Name,
opd.Last_Name = registeration.Last_Name,
opd.Address = registeration.Address,
opd.Pin_Code = registeration.Pin_Code,
opd.email = registeration.email,
opd.Doctor = registeration.Doctor,
opd.Posted_By = registeration.Posted_By

The corresponding php code is

<?php
include_once("config.php")
$sql= mysqli_query($conn,"UPDATE opd INNER JOIN registeration ON opd.customerid = registeration.id SET opd.T_date = registeration.T_date,\n"
. " opd.First_Name = registeration.First_Name,\n"
. " opd.Last_Name = registeration.Last_Name,\n"
. " opd.Address = registeration.Address,\n"
. " opd.Pin_Code = registeration.Pin_Code,\n"
. " opd.email = registeration.email,\n"
. " opd.Doctor = registeration.Doctor,\n"
. " opd.Posted_By = registeration.Posted_By");
$result=mysqli_query($conn,$sql);
header("Location:index.php");
?>

I am Getting this error. ( ! ) Parse error: syntax error, unexpected '$sql' (T_VARIABLE) in C:\wamp64\www\sonu\updateopd.php on line 3.

What is the mistake or correct code.

  • 1
    The question is based on a typo: You forgot the semicolon: `include_once("config.php");` Furthermore `$sql` is not a string but a result, however, that was *not* the actual question. This question should be closed due to *typo only* – Pinke Helga Sep 07 '19 at 23:03
  • 1
    See also: [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/15539535#15539535) – Pinke Helga Sep 07 '19 at 23:09
  • Aside: "registeration" is a misspelling, though it's likely spelled that way in your database too. Also, there's no need to include `\n`s in your query. It just makes your code harder to read. – ChrisGPT was on strike Sep 07 '19 at 23:10

1 Answers1

-1

You are assigning a variable called $sql to the output of the function call mysqli_query($conn,"UPDATE opd INNER....");.

this will return a TRUE or FALSE (based on if the query executed or not) - https://www.php.net/manual/en/mysqli.query.php

Then later in code, you are running mysqli_query again against a boolean value like this:

$result=mysqli_query($conn,$sql);

This is never going to work.

If your goal is to execute a SQL Query and then redirect a different page, then try this:

<?php

include_once("config.php")

mysqli_query(
    $conn,
    "
        UPDATE opd INNER JOIN registeration ON opd.customerid = registeration.id SET opd.T_date = registeration.T_date,
        opd.First_Name = registeration.First_Name,
        opd.Last_Name = registeration.Last_Name,
        opd.Address = registeration.Address,
        opd.Pin_Code = registeration.Pin_Code,
        opd.email = registeration.email,
        opd.Doctor = registeration.Doctor,
        opd.Posted_By = registeration.Posted_By
    "
);

header("Location:index.php");

?>

FYI - This is poor quality code; you are not testing if the sql query actually executed without errors (i.e. error handling).

Latheesan
  • 23,247
  • 32
  • 107
  • 201
  • Thanks Latheesan , errors are gone but my tables are not being updated. I am not a developer and learning php at beginner stage. Please bear with my questions. What I am doing add.php is redirected to updateregistration.php ( having above code) and updateregistration.php to index.php. But when I see the table opd in my database , its not being updated. Thanks for your detail answer. – Manish Srivastava Sep 08 '19 at 10:08
  • My answer assumes that when you include `config.php`, it gives you a mysql connection, i.e. `$conn` - if this is not the case, you may need to start a connection to mysql server and set it to `$conn` variable before executing `mysqli_query()` function for your update query to work. – Latheesan Sep 08 '19 at 21:32