0

This one has had me stumped for a while I cannot see why I am getting this error. This is my code

<?php
include('include/auth.php');
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

if(isset($_POST['submit']))
{

    $serverName = "localhost";
    $connectionInfo = array( "Database"=>"db", "UID"=>"sa", "PWD"=>"****");
    $conn = sqlsrv_connect( $serverName, $connectionInfo );
    if( $conn === false ) {
        die( print_r( sqlsrv_errors(), true));
    }


    $pathfinderid = $_POST['pathfinderid'];
    $locationid = $_POST['locationid'];
    $status = $_POST['status'];
    $statusnote = $_POST['statusnote'];
    $user = $_SESSION['SESS_USER'];
    $date = new DateTime();
    $ims = 'New Device Added';

    if(empty($pathfinderid) || empty($locationid) || empty($status) || empty($statusnote)) {
        echo "<div id='source'><p style='color:red;'>Please complete all fields</p></div>";
    }
    else
    {   
        //SQL to check if pathdnder exsists
        $stmt = sqlsrv_query( $conn, "SELECT * FROM devices WHERE pathfinderid='$pathfinderid'");
        //If statement to check rows
        if ($stmt) {
            $rows = sqlsrv_has_rows( $stmt );

            if ($rows === true) {
                echo "<div id='source'><p style='color:red';>PathfinderID already exists</div>";
            }
            else 
            {
                //Insert in to Devices Table
                $tsql="INSERT INTO devices (pathfinderid, locationid, addeddate, status, creation_date, status_note) VALUES (?,?,?,?,?,?)";
                $var = array ($pathfinderid, $locationid, $date, $status, $date, $statusnote);
                if (!sqlsrv_query($conn, $tsql, $var)) {
                    die('Error: ' . sqlsrv_errors());
                }
                //Insert in to Transaction Log
                $tsql="INSERT INTO transaction_log (Date, IMS, PathfinderID, LocationID, TransactionNotes, ManagedBy) VALUES (?,?,?,?,?,?,?)";
                $var = array ($date, $ims, $pathfinderid, $locationid, $statusnote, $statusnote, $user);
                if (!sqlsrv_query($conn, $tsql, $var)) {
                    die('Error: ' . sqlsrv_errors());
                }
                //Insert in to Movment Log
                $tsql="INSERT INTO movement_log (pathfinderid, locationid, status, update_timestamp, addeddate, status_note) VALUES (?, ?, ?, ?, ?, ?')";
                $var = array ($pathfinderid, $locationid, $status, $date, $date, $statusnote);
                if (!sqlsrv_query($conn, $tsql, $var)) {
                    die('Error: ' . sqlsrv_errors());
                }
                //Display the confirmation messgae
                echo "<div id='source'><p style='color:green;'>Device Added</p></div>";

            }
        }


    }
}

?>

The error is flagging as beng on line 52 which is:

//Insert in to Transaction Log
                $tsql="INSERT INTO transaction_log (Date, IMS, PathfinderID, LocationID, TransactionNotes, ManagedBy) VALUES (?,?,?,?,?,?,?)";
                $var = array ($date, $ims, $pathfinderid, $locationid, $statusnote, $statusnote, $user);
                if (!sqlsrv_query($conn, $tsql, $var)) {
                    die('Error: ' . sqlsrv_errors());
                }

Any ideas? The only thing I can think is if it is because I am reusing variable names?

1 Answer 1

2

You've got a double variable ($statusnote)

       //Insert in to Transaction Log
        $tsql="INSERT INTO transaction_log (Date, IMS, PathfinderID, LocationID, TransactionNotes, ManagedBy) VALUES (?,?,?,?,?,?,?)";
        $var = array ($date, $ims, $pathfinderid, $locationid, $statusnote, $statusnote, $user);
        if (!sqlsrv_query($conn, $tsql, $var)) {
            die('Error: ' . sqlsrv_errors());
        }

should be

       //Insert in to Transaction Log
        $tsql="INSERT INTO transaction_log (Date, IMS, PathfinderID, LocationID, TransactionNotes, ManagedBy) VALUES (?,?,?,?,?,?)";
        $var = array ($date, $ims, $pathfinderid, $locationid, $statusnote, $user);
        if (!sqlsrv_query($conn, $tsql, $var)) {
            die('Error: ' . sqlsrv_errors());
        }
Sign up to request clarification or add additional context in comments.

2 Comments

There's also an extra placeholder question-mark in the values.
yes, so either the variable has to be some other variable or things got out of sync by copy paste of code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.