sql server - SQLSRV PHP removing selected record -
there not many tutorials in web mssql using sqlsrv driver, followed similar tutorials mysql , using sqlsrv api converting code. far works, can add records popup form etc.
now i'm trying delete multiple records checkbox. tried tutorials able find still not work.
there no effect after selecting checkbox , pressing delete apart question ondelete script asking me if delete records.
i have 2 php files - sqltest.php , delete.php.
code 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 database !"; } else { echo "opps - went wrong !"; die( print_r( sqlsrv_errors(), true)); } $tsql = "select * 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 delete.php
<?php $servername = "thomas-pc\sqlexpress"; $connectioninfo = array( "database"=>"test"); $conn = sqlsrv_connect( $servername, $connectioninfo); if( $conn === false ) { echo "<script>alert('opps - went wrong !');</script>"; die( print_r( sqlsrv_errors(), true)); } for($i = 0; $i < count($_post["chkdel"]);$i++) { if($_post["chkdel"][$i] != "") { $tsql = "delete 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 want delete ?')==true) { return true; } else { return false; } } </script>
and sql details:
database -> test -> table : dbo.modindex recordid - first - second - third - fourth - fifth
thank in advance 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 - went wrong !');</script>"; die( print_r( sqlsrv_errors(), true) ); } $query = 'delete dbo.modindex id=?'; $stmt = sqlsrv_prepare($conn, $query, array(&$id)); if( !$stmt ) { die( print_r( sqlsrv_errors(), true) ); } foreach( $_post["chkdel"] $id ) { if( !sqlsrv_execute( $stmt ) ) { die( print_r( sqlsrv_errors(), true) ); } }
Comments
Post a Comment