php页面插入数据到sql无法正常工作

时间:2018-03-25 16:18:51

标签: php sql azure azure-sql-database

我试图通过php从Unity游戏向我的sql server添加数据。 php代码如下。我正在测试的网址是

SQL服务器以及php文件托管在Azure中,我已经能够通过标准SQL命令等添加数据(数据库连接正常工作),并在使用URL {{3进行测试时运行以下页面仅返回Test84,并且不对表进行任何更改。

如果有人能够告诉我为什么这不起作用我真的很感激

    <?php 
// 
echo"Test";
$data=$_GET[one];
$data1=$_GET[two];
echo $data1;
echo $data;
    $conn = new PDO("sqlsrv:server = {}; Database = {}", "{}", "{}");
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


$sql = $conn->prepare ("INSERT INTO registratib_tbl (name,email ) VALUES ( ? , ?)");
$sql->bindParam(1, $data);  
$sql->bindParam(2, $data1);  
$sql->execute();  

//$conn->query($sql); 
echo "<h3>Data Inserted!</h3>"; 

?>

1 个答案:

答案 0 :(得分:0)

Please try to insert data as shown below:

 <?php

 $serverName = "tcp:yourserver.database.windows.net,1433";
 $connectionOptions = array("Database"=>"yourdatabase",
 "Uid"=>"yourusername", "PWD"=>"yourpassword");
 //Establishes the connection
 $conn = sqlsrv_connect($serverName, $connectionOptions);
 //Select Query
 $tsql = "SELECT [CompanyName] FROM SalesLT.Customer";
 //Executes the query
 $getProducts = sqlsrv_query($conn, $tsql);
 //Error handling
 if ($getProducts == FALSE)
 die(FormatErrors(sqlsrv_errors()));
 $productCount = 0;
 $ctr = 0;
 ?> 
 <h1> First 10 results are : </h1>
 <?php
 while($row = sqlsrv_fetch_array($getProducts, SQLSRV_FETCH_ASSOC))
 { 
 if($ctr>9)
 break; 
 $ctr++;
 echo($row['CompanyName']);
 echo("<br/>");
 $productCount++;
 }
 sqlsrv_free_stmt($getProducts);

 $tsql = "INSERT SalesLT.Product (Name, ProductNumber, StandardCost, ListPrice, SellStartDate) OUTPUT INSERTED.ProductID VALUES ('SQL New 1', 'SQL New 2', 0, 0, getdate())";
 //Insert query
 $insertReview = sqlsrv_query($conn, $tsql);
 if($insertReview == FALSE)
 die(FormatErrors( sqlsrv_errors()));
 ?> 
 <h1> Product Key inserted is :</h1> 
 <?php
 while($row = sqlsrv_fetch_array($insertReview, SQLSRV_FETCH_ASSOC))
 { 
 echo($row['ProductID']);
 }
 sqlsrv_free_stmt($insertReview);
 //Delete Query
 //We are deleting the same record
 $tsql = "DELETE FROM [SalesLT].[Product] WHERE Name=?";
 $params = array("SQL New 1");

 $deleteReview = sqlsrv_prepare($conn, $tsql, $params);
 if($deleteReview == FALSE)
 die(FormatErrors(sqlsrv_errors()));

 if(sqlsrv_execute($deleteReview) == FALSE)
 die(FormatErrors(sqlsrv_errors()));

 ?>

For more information, please visit this article.