Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Oct 2011
    Posts
    92
    Thanks
    38
    Thanked 0 Times in 0 Posts

    mysql DELETE problem

    Please assist,

    I have the following code that needs to make use of the window.confirm before a row is deleted in the database.

    As the code is now below it runs through the motions but no matter what choices it still deletes the row. I managed to figure out it is because of the $delete_run = mysql_query($delete) that is outside the if statement but I cant get it to work within the if statement as it is javascript within PHP.

    Any suggestions and HELP PLEASE

    my code:
    Code:
    <?php
     
    require "config.php"; 
    require "connect_db.php"; 
     
    ?>
    
    <!DOCTYPE html PUBLIC
    "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Delete Record Page - 48323934 - ICT 2613</title>
    <meta name="Description" content="PHP ICT2613 Exam Assignment. 48323934 - Delete Record Page" />
    <meta http-equiv="content-language" content="en-za" />
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <meta name="Author" content="S.J. Fouche, Student number: 48323934" />
    <meta name="HandheldFriendly" content="True" />
    <!-- Link to the external style sheet (CSS) -->
    <link rel="stylesheet" type="text/css" href="my_css.css" />
    <!-- Link to the external javascript file -->
    <script type="text/javascript" src="my_javascript.js"></script>
    
    </head>
    
    <body">
    <!-- Used the include feature for the header and links on all pages -->
    <?PHP 
    // Check, if user is already login, then jump to secured page
     if(!loggedin()) {
    	// Set header
    	require "header.txt";
    	// Check, if user is logged, if if not give warning to login in first
    	echo ("<script type='text/javascript'>
        window.alert('You are not logged in, please log in or register to view this page.')
        window.location.href='index.php'; 
        </script>");
     	} 
     else {
    	require "header_secure.txt";
    	}
    	
    echo "<h2 align='center'>Delete Record</h2><br />";
    
    // This is to extract the values and allocate them to variables to be used in the message to confirm deletion
    $game_id = $_GET['game_id'];  // This is to get the value from the row selected when delete was clicked
    $query = "SELECT * FROM tb_list WHERE game_id='$game_id'";
    $query_run = mysql_query($query);
    $query_row = mysql_fetch_assoc($query_run);
    $gamename = $query_row['gamename'];
    $type = $query_row['type'];
    $platform = $query_row['platform'];
    $price = $query_row['price'];
    
    // Set the Query for delete 
    $delete = "DELETE FROM tb_list WHERE game_id='$game_id'";
    $delete_run = mysql_query($delete);
    
    // Confirm Deletion dialog box
    echo ("<script type='text/javascript'>	
    if (window.confirm('Press OK to confirm deletion of record or CANCEL to cancel deletion.')) {
    	window.alert(\"The $type game $gamename, for $platform, with a price of R$price, has been successfully deleted from the database!\");
    }
    else {
    	window.alert(\"You have canceled the deletion process!\");
    }	
    window.location.href='list.php';			
    </script>");
    	
    ?>
    
    
    <!-- PHP Coding -->
    <?PHP
    // Code below will create Code link at the center and bottom of the page
    echo("<p style='position:relative; bottom:5px; left:49%;'><a href='javascript: listcode()'>Code</a></p>")
    ?> 
    
    </body>
    </html>

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    window.confirm is a client function, not a server one. PHP cannot respond to the results of a client event. You need to rewrite the code to redirect to a script that handles the deletion confirm (can be this script if you give it get or post data), or use ajax.

  • #3
    New Coder
    Join Date
    Oct 2011
    Posts
    92
    Thanks
    38
    Thanked 0 Times in 0 Posts
    Thanks for response.
    This is a course I am taking and all my other pages works on this system with the windows.alert and windows.confirm. For me to keep uniformity I would like to keep using this feature. Below is a code that does work in PHP as well.
    I have not handled AJAX as yet it is a subject for next semester.
    Please help there must be a way.

    Below other page code:
    Code:
    <?php
     
    require "config.php";
    require "connect_db.php"
     
    ?>
    
    <!DOCTYPE html PUBLIC
    "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Registration Page - 48323934 - ICT 2613</title>
    <meta name="Description" content="PHP ICT2613 Exam Assignment. 48323934 - Registration Page" />
    <meta http-equiv="content-language" content="en-za" />
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <meta name="Author" content="S.J. Fouche, Student number: 48323934" />
    <meta name="HandheldFriendly" content="True" />
    <!-- Link to the external style sheet (CSS) -->
    <link rel="stylesheet" type="text/css" href="my_css.css" />
    <!-- Link to the external javascript file -->
    <script type="text/javascript" src="my_javascript.js"></script>
    
    </head>
    
    <body onload="hideDiv();">
    <!-- Used the include feature for the header and links on all pages -->
    <?PHP 
    require "header.txt";
    
    // Checking if the form fields are set with research on the internet I found that it is better to check all fields and not just $Submit=$_POST['Submit'] as it is unstable with IE.
    if (isset($_POST['title']) && isset($_POST['firstname']) && isset($_POST['surname']) && isset($_POST['username']) && isset($_POST['password1']) && isset($_POST['password2'])) {
    	// Setting form field variable apon submit
    	$title = $_POST['title'];   
    	$firstname = $_POST['firstname'];
    	$surname = $_POST['surname'];
    	$username = $_POST['username'];
    	$password1 = $_POST['password1'];
    	$password2 = $_POST['password2'];
    	
    	// To protect MySQL injection - Probably not needed as I used the preg_match below but just incase this offers extra protection
    	$firstname = stripslashes($firstname);
    	$surname = stripslashes($surname);
    	$username = stripslashes($username);
    	$password1= stripslashes($password1);
    	$firstname = mysql_real_escape_string($firstname);
    	$surname = mysql_real_escape_string($surname);
    	$username = mysql_real_escape_string($username);
    	$password1 = mysql_real_escape_string($password1);
    	
    	$password_hash = md5($password1);
    	$pattern1 = "/^[A-Za-z ]{3,50}$/";  // Use this for Name and Surname checking
    	$pattern2 = "/^[A-Za-z0-9_]{6}$/";  // Use this for Username and password checking
    	
    	if (!empty($title) && !empty($firstname) && !empty($surname) && !empty($username) && !empty($password1) &&!empty($password2)) {
    		// Check if passwords match
    		if ($password1 != $password2) {
    			echo ("<script type='text/javascript'>
    			window.alert('Password fields do not match, Please re-enter.');
    			</script>");
    		}
    		
    		// This check is done to $firstname and $surname to make sure only Alpha Charachters and spaces	
    		else if (!preg_match($pattern1, $firstname) || !preg_match($pattern1, $surname)){
    			echo ("<script type='text/javascript'>
    					window.alert(\"Name and Surname can only contain Alphabtic characters, No numeric or special characters allowed and must be more than 3 charachters!\");
    					</script>");
    		}
    		
    		// This check is done to $usernamee and $password1 to make sure only Alphanumerical Charachters
    		else if (!preg_match($pattern2, $username) || !preg_match($pattern2, $password1)){
    			echo ("<script type='text/javascript'>
    					window.alert(\"Username and Password can only contain Alphanumeric characters, No special characters allowed and must be 6 charachters long!\");
    					</script>");
    		}
    		/*	With the preg_match above I incorporated the length with the above code
    		else if (strlen($username) < 6 || strlen($password1) < 6) {
    			echo ("<script type='text/javascript'>
    					window.alert(\"Username and Password must be 6 charachters long!\");
    					</script>");
    		}*/
    			
    		else {
    			// Check if username already exist
    			$query = "SELECT username FROM tb_user WHERE username = '$username'";
    			$result = mysql_query($query);
    			
    			if (mysql_num_rows($result)==1) {
    				echo ("<script type='text/javascript'>
    				window.alert(\"Username $username already exists, Please choose another username\");
    				</script>");
    			}
    			
    			// If all checks passed enter the Registration details in the database
    			else {
    				$query = "INSERT INTO tb_user VALUES ('', '$title', '$firstname', '$surname', '$username', '$password_hash')";
    				if ($result = mysql_query($query)) {
    					echo ("<script type='text/javascript'>
    					if (window.confirm(\"You have succesfully registered, press OK to Login.\")) {
    						// If user press OK the login window will pop up, I played with a few options like the onclick and return but the onload worked perfectly
    						onload='hideDiv();'
    					}
    					else {
    						window.location.href='index.php';
    					}
    					</script>");
    				}
    			}
    		}
    	}	
    	else {
    		// If any fields are empty pop up error message
    		echo ("<script type='text/javascript'>
    		window.alert('All fields are compulsory, Please complete all fields.');
    		</script>");
    		}
        
    }
    else {
    	// Setting the default fields for the form below
    	$title = 'Mr';
    	$firstname = '';
    	$surname = '';
    	$username = '';
    	}
    
    
    // This code should never have to run but if user typed register.php in the address bar this will run
    // Check, if user is already login, if so log user out and proceed with registration
     if (loggedin()) {
    	echo ("<script type='text/javascript'>	
        window.alert('A Username is already logged in, logging out!')
    	window.location.href='logout.php'; 
        </script>");
     }
     ?>
    
    <!-- Insert form with fields required for registration -->
    <form action="register.php" method="POST">
    	<!-- Created this div to center the table -->
    	<div align="center">
    	<table border="0">
    	<tr align ="left"><td colspan="1">Title</td>
    		<!-- Created drop down options for the title as per specifications -->
    		<td colspan="1"><select name="title">
    						<!-- Took some research but eventually found a way, I wanted the selected dropdown option chosen
    						to stay displayed if the error popped up of all fields must be completed similar to the
    						php statement or the text inputs below -->
    						<option value="Mr" <?PHP if ($title == "Mr") echo 'selected="selected" '; ?>>Mr</option>
    						<option value="Mrs" <?PHP if ($title == "Mrs") echo 'selected="selected" '; ?>>Mrs</option>
    						<option value="Ms" <?PHP if ($title == "Ms") echo 'selected="selected" '; ?>>Ms</option>
    						<option value="Dr" <?PHP if ($title == "Dr") echo 'selected="selected" '; ?>>Dr</option>
    						</select></td></tr>
    						
    	<!-- Below I added a maxlength for each input field equivalant to the requirements -->
    	<tr align ="left"><td colspan="1">First Name:  </td>
    			<td colspan="1"><input name="firstname" type="text" maxlength="20" value = "<?PHP echo $firstname; ?>" /></td></tr>
    	
    	<tr align ="left"><td colspan="1">Surname:  </td>
    			<td colspan="1"><input name="surname" type="text" maxlength="50" value = "<?PHP echo $surname; ?>" /></td></tr>
    	
    	<!-- Used the maxlength to restrict input to only 6 charachters -->		
    	<tr align ="left"><td colspan="1">User name (6 characters):  </td>
    			<td colspan="1"><input name="username" type="text" maxlength="6" value = "<?PHP echo $username; ?>" /></td></tr>
    	
    	<!-- Used the type password as it is a password being entered -->		
    	<tr align ="left"><td colspan="1">Password (6 characters):  </td>
    			<td colspan="1"><input name="password1" type="password" maxlength="6" value = "" /></td></tr>
    			
    	<tr align ="left"><td colspan="1">Re-type your password:  </td>
    			<td colspan="1"><input name="password2" type="password" maxlength="6" value = "" /></td></tr>
    	
    	<!-- I include a reset button to clear the form -->			
    	<tr><td colspan="1"><input type="reset" value="Clear Form" /></td><td colspan="1"><input name="submit" type="submit" value="Register Now" /></td></tr>
    	
    	<tr><td colspan="2"></td></tr>
    	
    	<tr align ="center"><td colspan="2">Or Click <a href="#" onclick="showDiv()">here</a> to log in.</td></tr>
    	
    	</table></div></form>
    
    <!-- Setup login box and table properties will be hidden onload but once login link clicked it will appear-->
    <div class="picbox" id="login_form">
    <form name="form1" method="post" action="login.php">
    <table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
    <tr align="center"><td colspan="2" style="font-size: 120%;"><strong>Member Login </strong></td></tr>
    <tr><td align="right" width="100">Username:</td><td align="center" width="235"><input name="myusername" type="text" id="myusername" size="20" maxlength="6" /></td></tr>
    <tr><td align="right">Password:</td><td align="center"><input name="mypassword" type="password" id="mypassword" size="20" maxlength="6" /></td></tr>
    <!-- I have used a reset button bellow with the onclick to hide the login and at the same time it clears the form for the next time it is shown -->
    <tr><td align="right"><input type="reset" value="Exit Login" onclick="hideDiv();" /></td><td align="center"><input type="submit" name="Submit" value="Log me in"></td></tr>
    </table>
    </form>
    </div>
    
    <!-- PHP Coding -->
    <?PHP
    // Code below will create Code link at the center and bottom of the page
    // Tested and works with all browsers except IE9, it opens in new tab not new window, It might be a setting on my PC
    echo("<p style='position:absolute; bottom:5px; left:49%;'><a href='javascript: registercode()'>Code</a></p>");
    ?> 
    
    </body>
    </html>

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    When you do this:
    PHP Code:
    // This is to extract the values and allocate them to variables to be used in the message to confirm deletion
    $game_id $_GET['game_id'];  // This is to get the value from the row selected when delete was clicked
    $query "SELECT * FROM tb_list WHERE game_id='$game_id'";
    $query_run mysql_query($query);
    $query_row mysql_fetch_assoc($query_run);
    $gamename $query_row['gamename'];
    $type $query_row['type'];
    $platform $query_row['platform'];
    $price $query_row['price'];

    // Set the Query for delete 
    $delete "DELETE FROM tb_list WHERE game_id='$game_id'";
    $delete_run mysql_query($delete);

    // Confirm Deletion dialog box
    echo ("<script type='text/javascript'>    
    if (window.confirm('Press OK to confirm deletion of record or CANCEL to cancel deletion.')) {
        window.alert(\"The $type game $gamename, for $platform, with a price of R$price, has been successfully deleted from the database!\");
    }
    else {
        window.alert(\"You have canceled the deletion process!\");
    }    
    window.location.href='list.php';            
    </script>"
    );
        
    ?> 
    PHP will always execute the PHP code, and provide the string results within any echo or print statement. So this query runs before you even present the option to confirm. You need to provide a method to handle this deletion only after it is accepted, such as providing a querystring to the window.location.href. This querystring is then evaluated by PHP and issues the deletion.

  • Users who have thanked Fou-Lu for this post:

    hans_cellc (10-23-2012)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •