To illustrate the comment above - the function does not know what the variable $id is as it is not defined within the function. So you can either assign as a parameter. Additionally, which I didn't immediately spot, was that the delete function similarly requires the $conn variable to be defined either as a parameter or as a global within the function itself.
The generated HTML was incorrect - the closing table row tag NEEDS to be within the loop and the th sections should be within a table row.
function delete($conn,$id){
$delete1 =("DELETE FROM `usrdata` WHERE id = '$id'");
$result = mysqli_query($conn,$delete1) or die(mysqli_error());
echo "record deleted";
}
or declare as global within the function
function delete(){
global $id;
global $conn;
$delete1 =("DELETE FROM `usrdata` WHERE id = '$id'");
$result = mysqli_query($conn,$delete1) or die(mysqli_error());
echo "record deleted";
}
The variable needs to be defined first, a hidden field in the form with the ID or some other means to define the variable.
Better yet would be to use a prepared statement in conjunction with the above so
function delete( $conn=false, $id=false ){
if( $conn && $id ){
$sql='delete from `usrdata` where id = ?';
$stmt=$conn->prepare( $sql );
if( $stmt ){
$stmt->bind_param('i',$id);
$result=$stmt->execute();
$stmt->close();
echo $result ? 'record deleted' : 'error';
}
}
}
As a possible solution, hastily written so excuse errors, you could perhaps do something like this:
The table is wholly contained within a form ( delrecord ) with a single hidden field id which is assigned a value by some javascript using the buttons dataset attribute data-id
<?php
include 'conn.php';
$result = ("SELECT * FROM usrdata");
$count=mysqli_query( $conn, $result );
if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $_POST['id'] ) ){
function delete( $conn=false, $id=false ){
if( $conn && $id ){
$sql='delete from `usrdata` where id = ?';
$stmt=$conn->prepare( $sql );
if( $stmt ){
$stmt->bind_param('i',$id);
$result=$stmt->execute();
$stmt->close();
echo $result ? 'record deleted' : 'error';
}
}
}
$id=filter_input( INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT );
call_user_func( 'delete', $conn, $id );
}
echo'
<form name=\'delrecord\' method=\'post\'>
<input type=\'hidden\' id=\'h_id\' name=\'id\' />
<table border=1px>
<tr>
<th>id</th>
<th>email</th>
<th>Password</th>
<th> </th>
</tr>';
while( $data = mysqli_fetch_array( $count ) ) {
echo"
<tr>
<td>{$data['id']}</td>
<td>{$data['email']}</td>
<td>{$data['password']}</td>
<td><input data-id='{$data['id']}' type='button' name='delete' value='delete'></td>
</tr>";
}
echo '
</table>
</form>
<script>
var col=Array.prototype.slice.call( document.querySelectorAll("input[type=\'button\'][name=\'delete\']") );
col.forEach(function(bttn){
bttn.onclick=function(event){
document.getElementById("h_id").value=this.dataset.id;
document.forms.delrecord.submit();
}.bind(bttn)
})
</script>';
?>
deletehas no idea what$idis - either pass it as a parameter or declare as global inside the function. That said this is vulnerable to sql injection