php - Removing data from database according with what user selects -
hy,
i have form data passed php script , in conjunction user selection php processes query. instead of validating query in relation input stops @ first condition , not evaluates rest.
bellow script
$username=$_post['user']; $userid=$_post['id']; $studentname=$_post['studentname']; $studentsurname=$_post['studentsurname']; if(count($_post)>0){ if(isset($userid)){ echo 'var num'.$userid; $sql="delete user user_id=$userid"; $result=mysqli_query($conn,$sql); if(! $result ) { die('could not delete data: '.mysqli_error()); } echo "deleted data successfully\n"; /*or die('error'.mysqli_error); echo 'student deleted';*/ } else if(isset($username)){ echo 'var num'.$userid; $sql="delete user user_name='$username'"; $result=mysqli_query($conn,$sql); if(! $result ) { die('could not delete data: '.mysqli_error()); } echo "deleted data successfully\n"; /*or die('error'.mysqli_error); echo 'student deleted';*/ } else(isset($studentname)and isset($studentsurname)){ echo 'var num'.$userid; $sql="delete user name='$studentname' , surname='$studentsurname'"; $result=mysqli_query($conn,$sql); if(! $result ) { die('could not delete data: '.mysqli_error()); } echo "deleted data successfully\n"; /*or die('error'.mysqli_error); echo 'student deleted';*/ } } else{ echo "you have not completed fild!"; }
and html form:
<!doctype html> <html lang="en"> <head> <meta charset="utf-8" /> <title>delete course</title> <script src="validateuser.js"></script> </head> <body> <form action="removeuser.php" method ="post"> <table> <tr><td> <label for="user">user name:</label> </td> <td> <input type="text" id="user" name="user"></input> </td></tr> <tr><td> <label for="id">user id:</label> </td> <td> <input type="text" id="id" name="id"></input> </td></tr> <tr><td> <label for="studentname">student name:</label> </td> <td> <input type="text" id="studentname" name="studentname"></input> </td></tr> <tr><td> <label for="studentsurname"> student surname:</label></td><td> <input type="text" id="studentsurname" name="studentsurname"></input> </td></tr> <tr><td> <tr><td> <tr colspan="2"><td> <input type="submit" value="add"/></td></tr> </form> </body> </html>
what doing wrong?
i modified querys refect string input variables inspired by: fred-ii, faruk, iolka , pankaj
the question still remains. how evaluate in seqvential order conditions because evaluations stops after if(isset($userid) if , receive mesage:
var num warning: mysqli_error() expects 1 parameter, 0 given in ... not delete data:
sincerely,
your first query alright because $userid integer
$sql="delete user user_id=$userid";
rest of quires string change
$sql="delete user user_name='$username'"; $sql="delete user name='$studentname' , surname='$studentsurname'";
run correspondent delete query based on user input
if (!empty($_post['id'])) { $sql = "delete user user_id={$_post['id']}"; $result = mysqli_query($conn, $sql); if (!$result) { die('could not delete data: ' . mysqli_error()); } } if (!empty($_post['user'])) { $sql = "delete user user_name='{$_post['user']}'"; $result = mysqli_query($conn, $sql); if (!$result) { die('could not delete data: ' . mysqli_error()); } } if (!empty($_post['studentname']) , ! empty($_post['studentsurname'])) { $sql = "delete user name='{$_post['studentname']}' , surname='{$_post['studentsurname']}'"; $result = mysqli_query($conn, $sql); if (!$result) { die('could not delete data: ' . mysqli_error()); } }
Comments
Post a Comment