SQLSRV PHP删除所选记录

时间:2015-07-08 22:04:35

标签: php sql-server sqlsrv delete-record

网上关于使用sqlsrv驱动程序的Mssql的教程并不多,所以我跟着类似的MySQL教程并使用sqlsrv API我正在转换我的代码。到目前为止所有作品,我都可以添加弹出窗口等记录。

现在我尝试使用复选框删除多个记录。我尝试了所有能够找到的教程,但它仍然无效。

选择复选框并按下onDelete脚本中的删除分隔问题后无效,询问我是否要删除记录。

我有两个php文件 - SQLTest.php delete.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详细信息:

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

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

我改变了代码,因为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.
        )

)

我的数据库详情:

数据库 - &gt;测试 - &gt;表:dbo.customer - &gt; CustomerID - 名称 - 电子邮件 - CountryCode - 预算 - 已使用。

我是SQL PHP的新手,考虑到网上关于mssql和php的教程不多,这个问题比较困难。

谢谢大家的帮助和建议。

答案 1 :(得分:0)

<?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) );
    }
}