Author Topic: Updating database after data verification  (Read 117 times)

JayDude

  • Guest
Updating database after data verification
« on: March 21, 2012, 09:55:24 AM »
I am caught between a rock and a hard place here...
My update page (to update an existing entity in the database) is a normal selection page, which then directs you to the page where you can update or change the data (1st srcipt snippet below), up to here everything works well,
but on submit the page must be directed to a page that verifies the data (2nd script snippet below) and if correct, update the database or redirect to the 2nd page to correct the faulty data entry - easy right (well I have been struggling to get this working for 2 days straight. Pleeaaaaase Help. :shrug:

After the selection page, this is the input page-script:-

$labels = array("company_descr" => "Description:");
               
include("../includes/xxxxxxxxx.inc"); // database login details
$cxn = mysqli_connect($host,$user,$password,$dbname)
   or die ("Couldn't connect to server.");
$query = "SELECT * FROM `company`
      WHERE `company_id`=\"{$_POST['comp']}\"";
            
$result = mysqli_query($cxn,$query)
   or die ("Cannot execute query.");
$row = mysqli_fetch_assoc($result);
?>

<html>
<head>
<title>Update Location</title>
<style type='text/css'>
<!--
   form { margin: 1.5em 0 0 0; padding: 0; }
   .field { padding-top: .5em; }
   label { font-weight: bold; float: left;
      margin-right: 1em; text-align: right; }
   #submit { margin-left: 20%; padding-top: 1em; }
-->
</style>

<body>
<?php
   echo "<div style='text-align: centre'>
      <h3>Update Company - \"{$_POST['comp']}\"</h3>\n";
   echo "<p style='font-color: #FF0000; font-size: 12; font-align: center'>
      Please change the field(s) that you want to update below.</p>
      <hr /></div>\n";
   echo "<form action='../update/updateCompany.php' method='POST'>"; 
   
   foreach($labels as $field => $label)
   {
      echo "<div class='field'>
         <label for='$field'>$label</label>
            <input type='text' name='$field' id='$field'
               value='$row[$field]' size='35'
               maxlength='35' /></div>\n";
   }
   echo "<div id='submit'><input type='submit'
               value='Update' />\n";
   echo "</div>\n</form>\n</body>\n</html>";
?>



This is the script that is supposed to verify the data and then send you back to the input page or update the database if the data is correct


foreach($_POST as $field => $value)
      {
         if(empty($value))
            {
               $blanks[] = $field;
            }
            else
            {
               $good_data[$field] = strip_tags(trim($value));
            }
      }
   
      if(isset($blanks))
      {
         $message_2 = "The following fields are blank.
                  Please enter the required information:   ";
         foreach($blanks as $value)
         {
            $message_2 .="$value,  ";
         }
         extract($good_data);
         include("../updateInput/updateInputCompanyTest.php");
         exit();
      }
   /* validate data */
      foreach($_POST as $field => $value)
      {
         if(empty($value))
         {
            if(preg_match("/descr/i",$field))
            {
               if(!preg_match("/^[A-Za-z0-9.,' -]{1,50}$/",$value))
               {
                  $errors[] = "$value is not a valid address or city.";
               }
            }
         }  // end if not empty
      }
      foreach($_POST as $field => $value)
      {
         $$field = strip_tags(trim($value));
      }
      if(@is_array($errors))
      {
         $message_2 = "";
         foreach($errors as $value)
         {
            $message_2 .= $value." Please try again<br />";
         }
         include("../updateInput/updateInputCompanyTest.php");
         exit();
      }  // end if errors are found
      
      else  // add new member to database
      {
         include("../includes/xxxxxxxx.inc"); // database login details
         $cxn = mysqli_connect($host,$user,$password,$dbname)
            or die ("Couldn't connect to server.");
         $query = "INSERT INTO `company` (`company_descr`) VALUES
               ('$company_descr')
               WHERE `company_id`=\"{$_POST['comp']}\"";
         $result = mysqli_query($cxn,$query)
            or die ("Couldn't execute query.");
         header("Location: ../backtomainpage.php");
      }
      include("../updateInputCompanyTest.php");
?>

ErnieAlex

  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 1847
  • Karma: 32
    • View Profile
Re: Updating database after data verification
« Reply #1 on: March 21, 2012, 11:49:11 AM »
Well, it looks like you just need to alter your format of your query a little...
Your current code:
         $cxn = mysqli_connect($host,$user,$password,$dbname)
            or die ("Couldn't connect to server.");
         $query = "INSERT INTO `company` (`company_descr`) VALUES
               ('$company_descr')
               WHERE `company_id`=\"{$_POST['comp']}\"";
         $result = mysqli_query($cxn,$query)
            or die ("Couldn't execute query.");
Should be like this:
PHP Code: [Select]

         $cxn 
mysqli_connect($host,$user,$password,$dbname)
            or die (
"Couldn't connect to server.");
         
$query "INSERT INTO company (company_descr) VALUES
               ('" 
$company_descr "')
               WHERE company_id='" 
$_POST['comp'] . "'";
         
$result mysqli_query($cxn,$query)
            or die (
"Couldn't execute query.");

See if that works correctly...