There are not many tutorials in the web about Mssql using sqlsrv driver, so I followed similar tutorials with MySQL and using sqlsrv API I was converting my code. So far all works, I can add records with popup form etc.
Now I'm trying to delete multiple records with checkbox. I tried all the tutorials I was able to find but it still does not work.
There is no effect after selecting checkbox and pressing delete apart question from onDelete script which asking me if I like to delete records.
I have two php files - SQLTest.php and delete.php.
Code from 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>
Code from 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);
?>
Short onDelate function:
<script language="JavaScript">
function onDelete()
{
if(confirm('Do you want to delete ?')==true)
{
return true;
}
else
{
return false;
}
}
</script>
And my SQL details:
database -> test -> table : dbo.modIndex recordID - first - second - third - fourth - fifth
Thank you in advance for your help.
<?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) );
}
}