How to retrieve data from multiple tables using a PHP form

0 votes

I want to retrieve data from multiple tables using dot operator/join where arguments are passed from an HTML/PHP form.

HTML CODE

<input name="rollno" type="text" placeholder="Roll Number" required>
<input name="submit_view_details" type="submit" value="Proceed">

PHP CODE

if(isset($_POST['submit_view_details']))
{
    $rollno = (int) $_POST['rollno'];
    $query = "select * from table1, table2 where table1.{$rollno}=table2.{$rollno}";
    $result=mysqli_query($connection,$query);
}

Can someone please help me with this?

Jun 25, 2022 in PHP by narikkadan
• 63,600 points
2,138 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Use the AND keyword to specify the Roll no:

SELECT * FROM table1, table2 WHERE table1.rollno = table2.rollno 
AND table1.rollno = {$rollno};

You can then use keyword JOIN :

SELECT * FROM table1 NATURAL JOIN table2 
WHERE rollno = {$rollno};

I hope this helps you.

answered Jun 26, 2022 by Kithuzzz
• 38,000 points

edited Mar 5
0 votes
<?php

    require_once dirname(__FILE__) . '/DbConnect.php';

    $conn=$con;

    //Operation Values

    //  New     -  

    //  Edit

    //  Del

    //  ReadAll

    //  Read

    $mydata;

    if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['operation']) && isset($_POST['om_id']))

    {

   

        $operation = $_POST['operation'];

   

        $om_id = $_POST['om_id'];

       

        if( isset($_POST['jsonData']) )

        {  

            $jsonData = $_POST['jsonData'];

            $mydata = json_decode($jsonData);

        }

    }

    else

    {

        echo "Required fields are not set";

        return;

    }

    if ($operation == "New" )

    {

        // Insert Operation

        order_insert($mydata, $conn);

    }

    elseif ($operation == "Edit" )

    {

        // Update Operation

        order_edit($om_id, $mydata, $conn);

    }

    elseif ($operation == "Del" )

    {

        // Delete Operation

        order_del($om_id, $conn );

    }

    elseif ($operation == "ReadAll" )

    {

        order_readall($conn);

       

    }

    elseif ($operation == "read " )

    {

     

        order_read($om_id,$conn);

    }

    else

    {

        echo "Invalid or incomplete data for operation";

    }

    // Close the database connection

    $conn->close();

function order_insert($data, $conn){

   

    // Prepare and execute SQL query for insert (use prepared statements for security)

    $stmt = $conn->prepare("INSERT INTO order_master (om_name, om_no, om_grpno) VALUES ( ?, ?, ?)");

    $stmt->bind_param("sss", $data->om_name, $data->om_no , $data->om_grpno);

    if ($stmt->execute()) {

        echo "Data inserted successfully";

    } else {

        echo "Insert Failed: " . $stmt->error;

    }

    /*  ****    ****    ****    ****    ****    

    $x = getId();   //  Read or Get the id of the newly inserted record.

   

    //$x = $conn->lastInsertId();

   

    $orderitems [] =jsondecode( $data->orderitems;)

   

    while ( $orderitems )   // loop thru all the items in the orderitems array

    {

        $item = $orderitems[];      // get the each and every record/row of the orderitems array

       

        //call Order_Txn.php          //  call the Order_Txn.php with for insert opration

        //"New" , $x , $item ;        //  and pass the value of newly inserted orderid

        //call Insert_Order_Items          

        Insert_Order_Items($x,$item, $conn) ;         //  and pass the value of newly inserted orderid

    }

    ****    ****    ****    ****    ****    */    

    // Close the statement

    $stmt->close();

}

function order_edit($om_id, $data, $conn)

{

   

    $stmt = $conn->prepare("UPDATE order_master SET om_name=?, om_no=?, om_grpno=? WHERE om_id=?");

    $stmt->bind_param("sssi", $data->om_name, $data->om_no , $data->om_grpno, $om_id);

    // Check for successful execution

    if ($stmt->execute()) {

        echo "Update Success";

    } else {

        echo "Update Failed: " . $stmt->error;

        // Add debugging statements

    }

    /*  ****    ****    ****    ****    ****    

    $x = $om_id;    //  Set the value of OT_Id to the id of the record being edited.

   

    //call Order_Txn.php          //  call the Order_Txn.php with for delete opration

    //"Del" , $x, $item ;         //  and pass the value of current orderid

    //call Del_Order_Items          

    Del_Order_Items($x, $conn) ;         //  and pass the value of newly inserted orderid

   

    //  now insert all the new items again in order transaction table

    $orderitems = $data->orderitems;

   

    while ( $orderitems )   // loop thru all the items in the orderitems array

    {

        $item = $orderitems[];      // get the each and every record/row of the orderitems array

       

        //call Order_Txn.php          //  call the Order_Txn.php with for insert opration

        //"New" , $x, $item ;         //  and pass the value of newly inserted orderid

       

        //call Insert_Order_Items          

        Insert_Order_Items($x,$item, $conn) ;         //  and pass the value of newly inserted orderid

    }

    ****    ****    ****    ****    ****    */    

    // Close the statement

    $stmt->close();

}

function order_del($om_id, $conn )

{  

    $stmt = $conn->prepare("DELETE FROM order_master WHERE om_id=?");

    $stmt->bind_param("i", $om_id);

    // Check for successful execution

    if ($stmt->execute()) {

        echo "Delete Success";

    } else {

        echo "Delete Failed: " . $stmt->error;

    }

    // Close the statement

    $stmt->close();

}

function order_readall($conn) {  

    //$query = "SELECT om_id ,om_name,om_no ,om_grpno  from order_master;";

    $query = "SELECT *  from order_master;";

    $result = $conn->query($query);

    $items = array();

    if ($result->num_rows > 0)

    {

        while($row = $result->fetch_assoc())

        {

            $items[] = $row;

        }

        echo json_encode($items);

    }

    else

    {

        echo "No items found";

    }

    $result->close();

}

function order_read($om_id,$conn)

{    // Read Operation

    $result = $conn->query("SELECT * FROM order_master WHERE om_id=?");

    $items = array();

    if ($result->num_rows > 0)

    {

        while($row = $result->fetch_assoc())

        {

            $items[] = $row;

        }

        echo json_encode($items);

    }

    else

    {

        echo "No items found";

    }

    // Close the result set

    $result->close();

   

}

function order_items_insert($orderitemid, $data, $conn)

{

    // Prepare and execute SQL query for insert (use prepared statements for security)

    $stmt = $conn->prepare("INSERT INTO order_transaction (ot_id, ot_srno, ot_itemid, ot_rate, ot_remark, ot_amount, ot_quantity) VALUES (?, ?, ?, ?, ?, ?, ?)");

   

    // Use appropriate data types in bind_param based on your database table

    $stmt->bind_param("iiiisss", $orderitemid, $data->ot_srno, $data->ot_itemid, $data->ot_rate, $data->ot_remark, $data->ot_amount, $data->ot_quantity);

    if ($stmt->execute()) {

        echo "Data inserted successfully";

    } else {

        echo "Insert Failed: " . $stmt->error;

    }

    // Close the statement

    $stmt->close();

}

function order_items_del($ot_id, $conn )

{  

    $stmt = $conn->prepare("DELETE FROM order_transaction WHERE ot_id=?");

    $stmt->bind_param("i", $ot_id);

    // Check for successful execution

    if ($stmt->execute()) {

        echo "Delete Success";

    } else {

        echo "Delete Failed: " . $stmt->error;

    }

    // Close the statement

    $stmt->close();

}

?>
answered Mar 7, 2024 by anonymous

edited Mar 5

Related Questions In PHP

0 votes
1 answer

How to retrieve or obtain data from the MySQL database using PHP?

Hello kartik,  Actually there are many functions that  ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,840 points
3,642 views
0 votes
1 answer

How to insert multiple rows from a single query using eloquent/fluent?

Hello @kartik, You can use the following approach. $data ...READ MORE

answered Aug 14, 2020 in PHP by Niroj
• 82,840 points
1,949 views
0 votes
1 answer

How to delete all files from a folder using PHP?

Hello @kartik, Use this: $files = glob('path/to/temp/*'); // get ...READ MORE

answered Sep 14, 2020 in PHP by Niroj
• 82,840 points
1,513 views
0 votes
1 answer

How to copy a file from one directory to another using PHP?

Hello @kartik, You could use the copy() function : // Will ...READ MORE

answered Sep 15, 2020 in PHP by Niroj
• 82,840 points
1,480 views
0 votes
2 answers

How to copy a file from one directory to another using PHP?

Simply, $source = 'Source_file_location' ...READ MORE

answered Oct 12, 2020 in PHP by anonymous
• 140 points
1,883 views
0 votes
1 answer

How to check if a file exists from a url using php?

Hello, You have to use CURL function does_url_exists($url) { ...READ MORE

answered Nov 3, 2020 in PHP by Niroj
• 82,840 points
4,260 views
0 votes
2 answers

How to send data to my database from html and css Contact Us Form?

Hello @Sign, It is simple to create contact ...READ MORE

answered Aug 4, 2020 in Database by Niroj
• 82,840 points
36,283 views
0 votes
0 answers

Establishing PHP connection with localhost server?

I am having trouble connecting my PHP ...READ MORE

Jun 3, 2022 in PHP by Kichu
• 19,040 points
594 views
0 votes
0 answers

Creating a search form in PHP [duplicate]

I am working on a function where ...READ MORE

Jun 9, 2022 in PHP by Kichu
• 19,040 points
500 views
0 votes
0 answers

How to export PHP/MYSQL data to PDF?

When I develop a student information form ...READ MORE

Jul 31, 2022 in PHP by Kithuzzz
• 38,000 points
744 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP