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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Trying to update mysql database with php/html form

    I am working on an assignment and trying to use a form to update a database. I can type into the form and I receive the message saying "All fields have been updated". However, when I go back and look at the data, the information was not updated. What am I missing?

    Here is the updateClient.php that contains the form
    Code:
    <?php
    session_start();
    ?>
    
    <!DOCTYPE html>
    	
    <html>
    	
    	<title>Update Client</title>
    	<link rel="stylesheet" type="text/css" href="CSS/default.css" />
    	<link rel="stylesheet" type="text/css" href="CSS/register2.css" />
    
    	<script type="text/javascript" src="Javascript/formValidation.js"></script>
    	<script type="text/javascript" src="Javascript/userpassValidation.js"></script>
    	
    </head>
    
    <?php
    	include '../reuse/loginHeader.html';
    ?>
    
    <?php
    	/* server login info */
    include("../PHP/directConnection.php");
    	
    if(!$_POST['submit']){
    
    	// get username
    	$clientID	= $_GET['ClientID'];
    
    	// compare username to table username
    	$query		= "SELECT * FROM clients WHERE ClientID = '$clientID'";
    	// ACTUALLY tun query
    	$result		= mysql_query($query) or die ('Error: ' .mysql_error());
    	// again we number how many total rows we have
    	$num		= mysql_num_rows($result);
    	
    	mysql_close();
    
    	// if no information in table
    	if($num == 0){
    		echo "<b><center>No Records</center></b>";}
    		
    	$firstname		=	mysql_result($result, 0, "first_name");
    	$lastname		=	mysql_result($result, 0, "last_name");
    	$address		=	mysql_result($result, 0, "address");
    	$city			=	mysql_result($result, 0, "city");
    	$state			=	mysql_result($result, 0, "state");
    	$zip			=	mysql_result($result, 0, "zip");
    	$email			=	mysql_result($result, 0, "email");	
    	$tel			=	mysql_result($result, 0, "telephone");
    	$pending_status	=	mysql_result($result, 0, "pending_status");
    ?>
    
    		<div id="x">
    			<!--middle bar-->
    			<h1><strong>Update Contacts</strong></h1>
    			<form id="log" action="<?=$_SERVER['PHP_SELF']?>" method="POST" onsubmit="return validateForm(this);" name="theForm">
    				<h3>Update</h3>
    			<!--First Name-->
    				<label id="firstname" for="firstname">First Name</label>
    				<input id="firstname" name="firstname" type="text" value="<? echo $firstname; ?>" /></br>	
    			<!--Last Name-->				
    				<label id="lastname" for="lastname">Last Name</label>
    				<input id="lastname" name="lastname" type="text" value="<? echo $lastname; ?>" /></br>	
    				<br/>
    			<fieldset>
    			<!--address-->
    				<label for="address">Address</label>
    				<input type="text" id="address" name="address" value="<? echo $address; ?>"/></br>
    			<!--City-->
    				<label for="city">City</label>
    				<input type="text" id="city" name="city" value="<? echo $city; ?>" /></br>	
    			<!--State-->
    				<label for="state" title="Gotta pick one">State</label>
    				<select id="state" name="state" value="<? echo $state; ?>">
    					<!--used DevDaily.com to copy all 50 state options because I didn't want to waste 10 mins -_- -->
    					<option value="AK">AK</option>
    					<option value="AL">AL</option>
    					<option value="AR">AR</option>
    					<option value="AZ">AZ</option>
    					<option value="CA">CA</option>
    					<option value="CO">CO</option>
    					<option value="CT">CT</option>
    					<option value="DC">DC</option>
    					<option value="DE">DE</option>
    					<option value="FL">FL</option>
    					<option value="GA">GA</option>
    					<option value="HI">HI</option>
    					<option value="IA">IA</option>
    					<option value="ID">ID</option>
    					<option value="IL">IL</option>
    					<option value="IN">IN</option>
    					<option value="KS">KS</option>
    					<option value="KY">KY</option>
    					<option value="LA">LA</option>
    					<option value="MA">MA</option>
    					<option value="MD">MD</option>
    					<option value="ME">ME</option>
    					<option value="MI">MI</option>
    					<option value="MN">MN</option>
    					<option value="MO">MO</option>
    					<option value="MS">MS</option>
    					<option value="MT">MT</option>
    					<option value="NC">NC</option>
    					<option value="ND">ND</option>
    					<option value="NE">NE</option>
    					<option value="NH">NH</option>
    					<option value="NJ">NJ</option>
    					<option value="NM">NM</option>
    					<option value="NV">NV</option>
    					<option value="NY">NY</option>
    					<option value="OH">OH</option>
    					<option value="OK">OK</option>
    					<option value="OR">OR</option>
    					<option value="PA">PA</option>
    					<option value="RI">RI</option>
    					<option value="SC">SC</option>
    					<option value="SD">SD</option>
    					<option value="TN">TN</option>
    					<option value="TX">TX</option>
    					<option value="UT">UT</option>
    					<option value="VA">VA</option>
    					<option value="VT">VT</option>
    					<option value="WA">WA</option>
    					<option value="WI">WI</option>
    					<option value="WV">WV</option>
    					<option value="WY">WY</option>
    				</select>
    			<!--ZIP-->
    				<label for="zip">ZIP</label>
    				<input type="text" id="zip" name="zip" size="4" value="<? echo $zip; ?>" /></br>		
    			</fieldset>
    			
    			<!--Email-->	
    				<label id="email" for="email">Email</label>
    				<input id="email2" name="email" type="email" title="JonDoe@web.com, MrFantastic@HeroMail.com" value="<? echo $email; ?>" />
    			<!--Telephone-->
    				<label id="tel" for="tel">Primary Contact Number</label>
    				<input id="tel2" type="tel" name="tel" title="123-1234-1234, 789-7890-7890" value="<? echo $tel; ?>" />
    			<!--Submit-->			
    				<input class="button" type="submit" value="Turn In" title="no turning back..." name="submit" />
    			<!--reset-->
    				<input class="button" type="reset" value="Reset" name="clear" />
    			</form>		
    		</div>
    <?php
    }else{
    
    	$firstname 		= mysql_real_escape_string($_POST['firstname']);
    	$lastname 		= mysql_real_escape_string($_POST['lastname']);
    	$address 		= mysql_real_escape_string($_POST['address']);
    	$city 			= mysql_real_escape_string($_POST['city']);
    	$state 			= mysql_real_escape_string($_POST['state']);
    	$zip 			= mysql_real_escape_string($_POST['zip']);
    	$email 			= mysql_real_escape_string($_POST['email']);
    	$tel 			= mysql_real_escape_string($_POST['tel']);
    	$pending_status = mysql_real_escape_string($_POST['pending_status']);	
    	
    	
    	$query	= "UPDATE clients SET first_name='$firstname', last_name='$lastname', address='$address', city='$city', state='$state', zip='$zip', email='$email', telephone='$tel', pending_status='$pending_status' WHERE ClientID='$clientID'";
    
    	if(!mysql_query($query,$connection)){
    		die('Error: ' .mysql_error());
    	}else{
    		echo "<center><p><b>$firstname, All fields have been updated</b></p></center>";}
    mysql_close();}
    ?>
    Here is the directConnection.php that contains the access to the database
    I removed the passwords, username, and database name for security reasons.
    Code:
    ru<?php
    //ENCODED IN UTF-8 without BOM//
    /* login information */
    $xusername = "";	// XAMPP has root as a default user
    $xpassword = "";		// XAMPP has no password as default
    $xdatabase = "";	// testing database created
    
    /* connect to database setup */
    $connection = mysql_connect('localhost',$xusername,$xpassword); // file location and login info
    
    // if could not connect spit out an error otherwise move on
    if(!$connection){
    	die('Could not connect: ' .mysql_error());}
    
    // choose database otherwise if no database than an error	
    mysql_select_db($xdatabase, $connection) or die ("Couldn't open $xdatabase: " .mysql_error());
    
    ?>

  • #2
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    PHP Code:
    if(!$_POST['submit']){ 
    this says "if it is not submitted".

    BTW You have an HTML5 DOCTYPE, but are using the obsolete center tag and form-name attribute. There shouldn't be a blank line before the DOCTYPE declaration.
    Last edited by AndrewGSW; 12-08-2012 at 03:20 PM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #3
    Regular Coder Redcoder's Avatar
    Join Date
    May 2012
    Location
    /dev/null
    Posts
    332
    Thanks
    2
    Thanked 47 Times in 46 Posts
    Another thing, don't use the $_POST['submit'] to check for form submission, there's an IE bug that would mess stuff up. You'd better use another field or a hidden field e.g if($_POST['myhiddenfieldname'].

    Here's the bug explained by Tangoforce - The IE if (isset($_POST['submit'])) bug explained.


  •  

    Posting Permissions

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