SQLSRV PHP 删除选定的记录

SQLSRV PHP removing selected record

网络上关于使用 sqlsrv 驱动程序的 Mssql 的教程并不多,所以我按照 MySQL 和使用 sqlsrv API 的类似教程来转换我的代码。到目前为止一切正常,我可以使用弹出表单等添加记录

现在我正在尝试使用复选框删除多条记录。我尝试了我能找到的所有教程,但仍然无法正常工作。

选择复选框并按下删除后没有效果 onDelete 脚本问我是否要删除记录的问题。

我有两个 php 文件 - SQLTest.phpdelete.php.

代码来自 SQLTest.php

<h1>SQL Testing</h1>
<h1>Component Group Modifications</h1>

<table class="table_general">
<tbody>

<tr>
  <th>ID</th>
  <th colspan="2">MODIFICATIONS</th>
  <th colspan="3">APPLICABLE WORKSHEET</th> 
  <th>Delete</th>
</tr>

<?php
$serverName = "THOMAS-PC\SQLEXPRESS";
$connectionInfo = array( "Database"=>"test");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) 
{
     echo "Connected to Database !";
}
else
{
     echo "Opps - something went wrong !";
     die( print_r( sqlsrv_errors(), true));
}

$tsql = "SELECT * FROM dbo.modIndex";
$query = sqlsrv_query($conn, $tsql);
if ($query === false){  exit("<pre>".print_r(sqlsrv_errors(), true));

}
while ($row = sqlsrv_fetch_array($query))
{  
?>
  <tr>
    <td><?php echo $row["recordID"];?></td>
    <td><?php echo $row["first"];?></td>
    <td><?php echo $row["second"];?></td>
    <td><?php echo $row["third"];?></td>
    <td><?php echo $row["fourth"];?></td>
    <td><?php echo $row["fifth"];?></td>
    <td><input type="checkbox" name="chkDel[]" value="<?php echo $row["recordID"];?>"></td>
  </tr>
<?php
}
sqlsrv_free_stmt($query);
?>

</tbody>
<input type="button" value="Delete Selected Records" action="delete.php" method="post" onclick="return onDelete();"/>
</table>

</section>
</section>

代码来自 delete.php

<?php

$serverName = "THOMAS-PC\SQLEXPRESS";
$connectionInfo = array( "Database"=>"test");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn === false )
{
     echo "<script>alert('Opps - something went wrong !');</script>";
     die( print_r( sqlsrv_errors(), true));
}
for($i = 0; $i < count($_POST["chkDel"]);$i++)
{
    if($_POST["chkDel"][$i] != "")

        {
            $tsql = "DELETE FROM dbo.modIndex ";
            $tsql = "WHERE recordID = '".$_POST["chkDel"][$i]."'  "; 
            $query = sqlsrv_query($conn, $tsql);
        }
}
    echo "Record Deleted.";

sqlsrv_free_stmt( $tsql);
sqlsrv_close( $conn);

?>

短onDelate函数:

<script language="JavaScript">
    function onDelete()
    {
        if(confirm('Do you want to delete ?')==true)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
</script>

还有我的 SQL 详细信息:

数据库 -> 测试 -> table : dbo.modIndex recordID - 第一 - 第二 - 第三 - 第四 - 第五

预先感谢您的帮助。

我按照 VolkerK 的建议使用 POST 方法更改了代码,并向 delete.php 添加了错误处理,这为我提供了更多信息。

以下更改:

    <form name="frmMain" action="delete.php" method="post" OnSubmit="return onDelete();">

<table class="table_general">
<tbody>
<tr>
   <th>CustomerID</th>
    <th>Name</th>
    <th>Email</th>
    <th>CountryCode</th>
    <th>Budget</th>
    <th>Used</th>
    <th>Delete</th>
</tr>

<?php
$serverName = "THOMAS-PC\SQLEXPRESS";
$connectionInfo = array( "UID"=>"", "PWD"=>"", "Database"=>"test");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) 

{
     echo "Connected to Database !";
}
else
{
     echo "Opps - something went wrong !";
     die( print_r( sqlsrv_errors(), true));
}

$tsql = "SELECT * FROM dbo.customer";
$query = sqlsrv_query($conn, $tsql);
if ($query === false){  exit("<pre>".print_r(sqlsrv_errors(), true));

}
while ($objResult = sqlsrv_fetch_array($query))

{  

?>
  <tr>
    <td><?=$objResult["CustomerID"];?></td>
    <td><?=$objResult["Name"];?></td>
    <td><?=$objResult["Email"];?></td>
    <td><?=$objResult["CountryCode"];?></td>
    <td><?=$objResult["Budget"];?></td>
    <td><?=$objResult["Used"];?></td>
    <td><input type="checkbox" name="chkDel[]" value="<?=$objResult["CustomerID"];?>"></td>
  </tr>
<?php
}

sqlsrv_free_stmt($query);

?>
</tbody>

<input type="submit" name="btnDelete" value="Delete">
</table>
</form>

并且 delete.php 选择一个复选框并按下删除按钮后现在给我错误:

delete.php代码:

    <?php

$serverName = "THOMAS-PC\SQLEXPRESS";
$connectionInfo = array( "Database"=>"test");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn === false )
{
     echo "<script>alert('Opps - something went wrong !');</script>";
     die( print_r( sqlsrv_errors(), true));
}
for($i = 0; $i < count($_POST["chkDel"]);$i++)
{
    if($_POST["chkDel"][$i] != "")

        {
            $tsql = "DELETE FROM dbo.customer";
            $strSQL .="WHERE CustomerID = '".$_POST["chkDel"][$i]."' ";
            $query = sqlsrv_query($conn, $tsql, $strSQL);
            var_export($_POST);

        }

        print_r(sqlsrv_errors());
        }


sqlsrv_free_stmt( $tsql);
sqlsrv_close( $conn);

?>

选中三个可用的复选框后出现错误。

       Array
(
    [chkDel] => Array
        (
            [0] => 1
        )

    [btnDelete] => Delete
)
chkDel=Array<br />btnDelete=Delete<br />

Array
(
    [0] => Array
        (
            [0] => IMSSP
            [SQLSTATE] => IMSSP
            [1] => -14
            [code] => -14
            [2] => An invalid parameter was passed to sqlsrv_query.
            [message] => An invalid parameter was passed to sqlsrv_query.
        )

)

我的数据库详细信息:

数据库 -> 测试 -> table : dbo.customer -> 客户 ID - 姓名 - 电子邮件 - 国家代码 - 预算 - 使用。

我是 SQL PHP 的新手,考虑到网络上关于 mssql 和 php.

的教程不多,这会更加困难

感谢大家的帮助和建议。

<?php
if ( !isset($_POST["chkDel"]) || !is_array($_POST["chkDel"]) ) {
    die('missing or wrong parameter "chkDel"');
}

$serverName = "THOMAS-PC\SQLEXPRESS";
$connectionInfo = array( "Database"=>"test");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false ) {
    echo "<script>alert('Opps - something went wrong !');</script>";
    die( print_r( sqlsrv_errors(), true) );
}

$query = 'DELETE FROM dbo.modIndex WHERE ID=?';
$stmt = sqlsrv_prepare($conn, $query, array(&$id));
if( !$stmt ) {
    die( print_r( sqlsrv_errors(), true) );
}

foreach( $_POST["chkDel"] as $id ) {
    if( !sqlsrv_execute( $stmt ) ) {
        die( print_r( sqlsrv_errors(), true) );
    }
}