SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy Problem Updating Records!!

    Hi folks,

    My first time posting on this site, but looks like there's a lot of knowledge floating about here!

    I'm having a prob updating records using the code below. The form fields are being populated by the current data, but will not update - and there is no error message either.

    The code is copied from another page that is working perfectly and I've been trying for days to figure out what's different, but can't get it! Any help would be very appreciated...!

    Code:
    <?php
    
    // form not yet submitted
    // display initial form with values pre-filled
    if (!$_POST['submit'])
    {
         // check for record ID
         if ((!isset($_GET['id']) || trim($_GET['id']) == '')) 
         { 
             die('Missing record ID!'); 
         }
    
    	include('../includes/dbconnect.php');
    
        // generate and execute query
        $country_id = $_GET['id'];
    	
    	echo $country_id;
    	
        $query = "SELECT country, capital, population, religion, language, imgname, content FROM tbcountryprofiles WHERE country_id = '$country_id'";
    	
        $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
        
        // if a result is returned
        if (mysql_num_rows($result) > 0)
        {
            // turn it into an object
            $row = mysql_fetch_object($result);
    
            // print form with values pre-filled
    ?>
    
    
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" >
    
    <input type="hidden" name="id"  value="<?php echo $country_id; ?>">
    
    
    <label for="Country Name"><strong>Country Name </strong></label>
    <input size="60" maxlength="250" type="text" name="country" value="<?php echo $row->country; ?>">
    
    <label for="Capital"><strong>Capital</strong></label>
    <input size="60" maxlength="250" type="text" name="capital" value="<?php echo $row->capital; ?>">
    
    <label for="Population"><strong>Population </strong></label>
    <input size="60" maxlength="250" type="text" name="population" value="<?php echo $row->population; ?>">
    
    <label for="Religion"><strong>Religion</strong></label>
    <input size="60" maxlength="250" type="text" name="religion" value="<?php echo $row->religion; ?>">
    
    <label for="Language"><strong>Language</strong></label>
    <input size="60" maxlength="250" type="text" name="language" value="<?php echo $row->language; ?>">
    
    <label for="Image Name"><strong>Img Name (Flag)</strong></label>
     <input size="60" maxlength="50" type="text" name="imgname" value="<?php echo $row->imgname; ?>">
    
    <textarea name="news" cols="55" rows="10">
    <?php echo $row->content; ?>
    </textarea>
    
    <input type="Submit" name="submit" value="Update">
    </form>
    
    <?php
        }
        // no result returned
        // print graceful error message
        else
        {
            echo '<font size=-1>That country profile could not be located in our database.</font>';
        }
    }
    // form submitted
    // start processing it
    else
    {
        // set up error list array
        $errorList = array();
        
    $countryname =  $_POST["countryname"];
    $capital =  $_POST["capital"];
    $population =  $_POST["population"];
    $religion =  $_POST["religion"];
    $language =  $_POST["language"];
    $content =  $_POST["content"];
    
     if ($_POST['imgname']=='')
          $imgname = "";
    	  	else {	  
    	  $imgname =  $_POST["imgname"];
    	  }
    	
            
        // check for record ID
        if ((!isset($_POST['id']) || trim($_POST['id']) == '')) 
        { 
          die ('Missing record ID!'); 
        }
        
        
        // check for errors
        // if none found...
        if (sizeof($errorList) == 0)
        {
            
    		include("../includes/dbconnect.php");
    		
    		// open database connection
            //$connection = mysql_connect($host, $user, $pass) or die ('Unable to connect!');
    
            // select database
           //mysql_select_db($db) or die ('Unable to select database!');
    
            // generate and execute query
            $query = "UPDATE tbcountryprofiles SET country = '$countryname', capital = '$capital', population = '$population', religion = '$religion', language = '$language', imgname = '$imgname', content = '$content' WHERE country_id = '$country_id'";
            $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
    
            // print result
            echo '</br><p><strong>Update successful.</strong>';
            echo '</br></br><a href=countryprofilelist.php><strong>Go back to the main menu</strong></a>.</p>';
        }
        else
        {
            // errors occurred
            // print as list
            echo '<font size=-1>The following errors were encountered:'; 
            echo '<br>';
            echo '<ul>';
            for ($x=0; $x<sizeof($errorList); $x++)
            {
                echo "<li>$errorList[$x]";
            }
            echo '</ul></font>';
        }
    }
    ?>

  2. #2
    SitePoint Zealot agoossens's Avatar
    Join Date
    Mar 2004
    Location
    Adelaide, Australia
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does it print anything on the screen? For example, does it print out 'Update Successful'?

    Double check the SQL query. Print out the value of $query and make sure it looks as expected - an update will not fail if it updates zero rows.
    This space for rent.

  3. #3
    SitePoint Addict amitjoshi's Avatar
    Join Date
    Jul 2005
    Location
    Oklahoma, USA
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your update query is not failing but it is not updating 0 rows.
    The reason it that the variable '$country_id' is not defined in the else part of your page.

    Add
    '$country_id = $_POST['id'];
    before you define the update query.

  4. #4
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile

    Thanks Amitjoshi and Agoossens - I'll try those suggestions and let you know - already making more sense!!


Bookmarks

Posting Permissions

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