简体   繁体   中英

INSERT INTO sqlsrv_query statement not working

When working with the sqlsrv_query command I can request data from the MSSQL server. This works But!

When I want to add data it returns the error [error:array].

The code I use for this is:

$tsql= "INSERT INTO dbo.VERLOF_events (id,
                username,
                soort,
                afdeling,
                description,
                evdate,
                trdate) 
                VALUES
                (?, ?, ?, ?, ?, ?, ?)";
                $var = array('', $username, $soort, $afdeling,     $description, $evdate, $trdate);
                if (!sqlsrv_query($conn, $tsql, $var))
                     {
                die('Error: ' . sqlsrv_errors());
                     }
                echo "1 record added"; 

The array values are set in the POST statement.

$afdeling = $row['Afdeling'];
$submit = @$_POST['submit'];
$description = @$_POST["description"];
$evdate = @$_POST["evdate"];
$trdate = @$_POST["trdate"];
$username = @$_SESSION['username'];
$soort = @$_POST['Dagen'];

Why does it return the array error? I looked it up but could not find the problem returning the error.

Any help is appreciated!

The problem is probably you're trying to add an empty value in the id field. If you set identity on it with auto-numbering, you don't need to include it in your query :

$tsql= "INSERT INTO dbo.VERLOF_events (
            username,
            soort,
            afdeling,
            description,
            evdate,
            trdate) 
            VALUES
            (?, ?, ?, ?, ?, ?)";
            $var = array($username, $soort, $afdeling,     $description, $evdate, $trdate);
            if (!sqlsrv_query($conn, $tsql, $var))
                 {
            die('Error: ' . sqlsrv_errors());
                 }
            echo "1 record added"; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM