Normally this code works as a T-SQL script in Visual Studio except in T-SQL I add in:
declare @json nvarchar(MAX) = '{...}'
@json goes in the OPENJSON function
I am attempting to create a PHP script for the OPENJSON insert but this doesn't seem to work. I am a total beginner to all things Azure/SQL/PHP.
For reference on the OPENJSON function: https://blogs.msdn.microsoft.com/sqlserverstorageengine/2015/09/22/openjson-the-easiest-way-to-import-json-text-into-table/
<?php
include_once("connection.php");
$JSONrow = $_POST["jsonrow"];
$table = $_POST["table"];
$with = "(jobticketnumber nvarchar(50), contractor nvarchar(50),joblocation nvarchar(50), ...";
$pdo_JSONinsert = $conn -> prepare('INSERT INTO '.$table.' SELECT * FROM OPENJSON('.$JSONrow.') WITH '.$with.'');
$pdo_JSONinsert -> execute();
$conn = null;
?>
Thanks for the help!
Edit: error from PDO
<br />
<b>Fatal error</b>: Uncaught PDOException: SQLSTATE[42000]: [Microsoft][ODBC Driver 13 for SQL Server]Syntax error, permission violation, or other nonspecific error in D:\home\site\wwwroot\insertrow.php:7
Stack trace:
#0 D:\home\site\wwwroot\insertrow.php(7): PDO->prepare('INSERT INTO job...')
#1 {main}
thrown in
<b>D:\home\site\wwwroot\insertrow.php</b> on line
<b>7</b>
<br />