[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

5.8

php form displaying errors on submit

Asked by gwh2 in PHP Scripting Language, MySQL Server, PHP and Databases

Tags: lvl_openwin, php

Hi,

I'm putting together a content managment system using php, MySQL etc. The page that I'm currently having problems with contains a form whereby you can enter information about a garment and then the form is supposed to insert the garment record into a 'garments' table within my MySQL database. I have a couple of questions to ask but firstly when I test the page, ie. I enter all the information into the form then press the submit button, I get a list of errors. The following are the errors I get and some code relating to the line each error refers to - I've also included the full page code at the very end. I wondered if someone could help me get passed these errors?

Appreciated any help offered.

Notice: Undefined index: category in /Applications/MAMP/htdocs/newsite/admin/insertNewGarment.php on line 43

        if ($_POST['category'][0] == 'choose' && count($_POST['category']) <= 1) {


Notice: Undefined index: code in /Applications/MAMP/htdocs/newsite/admin/insertNewGarment.php on line 65

      $code = $_POST['code'];


Notice: Undefined index: supplier_id in /Applications/MAMP/htdocs/newsite/admin/insertNewGarment.php on line 66

      $supplier_id = $_POST['supplier_id'];


Notice: Undefined index: category in /Applications/MAMP/htdocs/newsite/admin/insertNewGarment.php on line 75

              if (($_POST['supplier'] AND $_POST['garment_type']  AND $_POST['category'] AND $_POST['colour'] AND $_POST['size']) == 'other')


Notice: Undefined index: garment_id in /Applications/MAMP/htdocs/newsite/admin/insertNewGarment.php on line 84

      VALUES ("'.$_POST['supplier'].'","'.$_POST['garment_id'].'",'.


Notice: Undefined index: code in /Applications/MAMP/htdocs/newsite/admin/insertNewGarment.php on line 85

      $_POST['title'].',"'.$_POST['code'].',"'.$_POST['description'].',"'.$_POST['extra_info'].',"'.$_POST['image'].'","'.$_POST['swatch_image'].'")';


Notice: Undefined index: extra_info in /Applications/MAMP/htdocs/newsite/admin/insertNewGarment.php on line 85

      $_POST['title'].',"'.$_POST['code'].',"'.$_POST['description'].',"'.$_POST['extra_info'].',"'.$_POST['image'].'","'.$_POST['swatch_image'].'")';


Notice: Undefined index: swatch_image in /Applications/MAMP/htdocs/newsite/admin/insertNewGarment.php on line 85

      $_POST['title'].',"'.$_POST['code'].',"'.$_POST['description'].',"'.$_POST['extra_info'].',"'.$_POST['image'].'","'.$_POST['swatch_image'].'")';


MySQL Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'title,","test description,","y","")' at line 2



<?php
require_once('classes/database.php');
// this code always runs, and gets lists of suppliers and garment categories
$db=new Database('localhost','root','password','catalogue',0);
$getSuppliers = 'SELECT * FROM suppliers ORDER BY supplier';
$suppliers = $db->query($getSuppliers);
$getGarment_types = 'SELECT * FROM garment_types ORDER BY garment_type';
$garment_types = $db->query($getGarment_types);
$getCategories = 'SELECT * FROM categories ORDER BY category';
$categories = $db->query($getCategories);
$getColours = 'SELECT * FROM colours ORDER BY colour';
$colours = $db->query($getColours);
$getSizes = 'SELECT * FROM sizes ORDER BY size';
$sizes = $db->query($getSizes);
// this first block runs only if the form has been submitted
if ($_POST) {
  // check for empty fields
  foreach($_POST as $key=>$value) {
    // category and colour are sub-arrays, so skip
    if (is_array($value)) continue;
      if ($key == 'extrainfo') continue;
    $value = trim($value);
    if (empty($value)) {
      if ($key == 'stylenum') {
        $error[] = 'You must enter a style number';
        }
      // if no supplier selected, value is 0, considered empty by PHP
      elseif ($key == 'supplier') {
        $error[] = 'You must select a supplier';
        }
      elseif ($key == 'sizes') {
        $error[] = 'You must select a size';
        }
      elseif ($key == 'garment_type') {
        $error[] = 'You must select a garment type';
        }
      else {
        $error[] = ucfirst($key).' is required';
        }
      }
    }
            // check that a garment category has been chosen
        if ($_POST['category'][0] == 'choose' && count($_POST['category']) <= 1) {
    $error[] = 'Select at least one category, or choose "Not listed"';
    }
            // check that a garment colour has been chosen
  if ($_POST['colour'][0] == 'choose' && count($_POST['colour']) <= 1) {
    $error[] = 'Select at least one colour, or choose "Not listed"';
    }
  // if all fields correctly filled, prepare to insert in database
  if (!isset($error)) {
    // final preparations for insertion
      // escape quotes and apostrophes if magic_quotes_gpc off
    if (!get_magic_quotes_gpc()) {
      foreach($_POST as $key=>$value) {
        // skip category and colour sub-arrays
        if (is_array($value)) continue;
        $temp = addslashes($value);
        $_POST[$key] = $temp;
        }
      }
    // create a Database instance, and set error reporting to plain text
    $db = new Database('localhost','root','password','catalogue',0);
    // first check that the same garment code and supplier combination doesn't already exist
      $code = $_POST['code'];
      $supplier_id = $_POST['supplier_id'];
      $checkCode = "SELECT code FROM garments WHERE code = '$code' AND supplier_id = '$supplier_id'";
        $result = $db->query($checkCode);
    if ($result->num_rows > 0) {
      $error[] = 'A garment with that code and supplier already exists in the database';
      }
    else {
      // if code and supplier combination unique, insert garment into garments table
               
              if (($_POST['supplier'] AND $_POST['garment_type']  AND $_POST['category'] AND $_POST['colour']                                   AND $_POST['size']) == 'other')
      {
      $_POST['supplier'] = 0;
      $_POST['garment_type'] = 0;
      $_POST['category'] = 0;
      $_POST['colour'] = 0;
      $_POST['size'] = 0;
      }
        $insert = 'INSERT INTO garments (supplier_id, garment_type_id, title, code, description, extra_info,                   image, swatch_image)
      VALUES ("'.$_POST['supplier'].'","'.$_POST['garment_id'].'",'.
      $_POST['title'].',"'.$_POST['code'].',"'.$_POST['description'].',"'.$_POST['extra_info'].',"'.$_POST['image'].'","'.$_POST['swatch_image'].'")';
      $result = $db->query($insert);
      // get the primary key of the record just inserted
        $getGarment_id = 'SELECT garment_id FROM garments
                     WHERE code = "'.$_POST['code'].'"';
      $result = $db->query($getGarment_id);
      $row = $result->fetch_assoc();
      $garment_id = $row['garment_id'];
        
        // if "Select category(s)" and "Select colour(s) still selected, remove from the array
      if ($_POST['category'][0] == 'choose' || $_POST['colour'][0] == 'choose')
      array_shift(
      $_POST['category'],      $_POST['colour']
      );
if (in_array('other', $_POST['category']) && in_array('other', $_POST['colour'])) {
      $i = array_search('other', $_POST['category'] && $_POST['colour']);
      $_POST['category'][$i] = 0 AND $_POST['colour'][$i] = 0;
}
// build array of garment_id/category_id pairs, garment_id/colour_id pairs, and garment_id/size_id pairs one for each category, colour and size

$values = array();
$i = 0;

foreach ($_POST['category'] as $category_id) {
      $values[$garment_id]['category'] = $category_id;
      $values[$garment_id]['colour'] =  $colour_id[$i];
      $values[$garment_id]['size'] = $size_id[$i];
      $i++;
      }

      // insert garment_id/category_id, garment_id/color_id, and garment_id/size_id pairs into garment to category, garment to color, and garment to size lookup tables

foreach( $values as $k ) {
      $cat_sql = "INSERT INTO garment_to_category SET garment_id = '$k', category_id = '$values[$k]['category']'";

      $colour_sql = "INSERT INTO garment_to_colour SET garment_id = '$k', colour_id = '$values[$k]['colour']'";

      $size_sql = "INSERT INTO garment_to_size SET garment_id = '$k', size_id = '$values[$k]['size']'";

      $result = $db->query($cat_sql);
      $result = $db->query($colour_sql);
      $result = $db->query($size_sql);
}
      // if successful, redirect to confirmation page
      if ($result) {
          $db->close();
        header('Location: listGarments.php?action=inserted&title='.$_POST['title']);
        }
      }
      }
  }
?>
<!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"><!-- InstanceBegin template="/Templates/AdminTemplate.dwt" codeOutsideHTMLIsLocked="false" -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<!-- InstanceBeginEditable name="doctitle" -->
<title>Insert new garment</title>
<!-- InstanceEndEditable -->
<link href="../main.css" rel="stylesheet" type="text/css" />
<link href="admin.css" rel="stylesheet" type="text/css" />

<script type="text/javascript" src="../p7pm/p7popmenu.js"></script>
<style type="text/css" media="screen">
<!--
@import url("../p7pm/p7pmh2.css");
-->
</style>
<!--[if lt IE 7]>
<link href="../win_ie.css" rel="stylesheet" type="text/css" />
<![endif]-->
<style type="text/css"></style>
<script language="JavaScript" type="text/JavaScript">
<!--
function Lvl_openWin(u,n,w,h,l,t,c,f) { //v2.2 4LevelWebs
  var x=((screen.width-w)/2);if(c==1){l=x;t=(screen.height-h)/2;}if(c==2){l=x}
      f+=',top='+t+',left='+l;LvlWin=window.open(u,n,f);LvlWin.focus();
}
//-->
</script>
<!-- InstanceBeginEditable name="head" --><!-- InstanceEndEditable -->
</head>
<body onload="P7_initPM(1,2,1,-20,10)">
<div id="topstrip"></div>
<div id="header"></div>
<div id="navcorp">
  <ul id="p7PMnav">
    <li><a href="#" class="p7PMtrg" id="height">List garments</a>
        <ul>
          <li><a href="#">List suppliers</a></li>
          <li><a href="#">List categories</a></li>
          <li><a href="#">List garment types</a></li>
          <li><a href="#">List colours</a></li>
          <li><a href="#">List sizes</a></li>
        </ul>
    </li>
    <li><a href="#" class="p7PMtrg" id="height">Add new garment</a>
        <ul>
          <li><a href="#">Add new supplier</a></li>
          <li><a href="#">Add new category</a></li>
          <li><a href="#" id="unique">Add new garment type</a></li>
          <li><a href="#">Add new colour</a></li>
          <li><a href="#">Add new size</a></li>
        </ul>
    </li>
    <li><a href="#" class="p7PMtrg" id="height">Register new user</a>
        <ul>
          <li><a href="#">List registered users</a></li>
        </ul>
    </li>
    <li><a href="#" id="height">View Catalogue</a></li>
    <li><a href="#">Logout</a></li>
    <!--[if lte IE 6]><style>#p7PMnav a{height:1em;}#p7PMnav li{height:1em;}#p7PMnav ul li{float:left;clear:both;width:100%}</style><![endif]-->
    <!--[if IE 6]><style>#p7PMnav ul li{clear:none;}</style><![endif]-->
  </ul>
</div>

<!-- InstanceBeginEditable name="EditRegion3" -->
<div id="background">
  <div id="wrapper">
    <div id="innerlayer">
      <h1>Insert new garment</h1>
        <?php
if (isset($error)) {
  echo '<div id="alert"><p>Please correct the following:</p><ul>';
  foreach ($error as $item) {
    echo "<li>$item</li>";
      }
  echo '</ul></div>';
  }
?>
      <form id="garmentDets" name="garmentDets" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
        <table id="garmentInsert" cellpadding="0">
            
          <tr>
            <th width="180" class="labelLeft">Supplier</th>
            <td colspan="2"><select name="supplier" id="supplier">
                <option value="0" selected="selected">Select supplier</option>
                <option value="other">Not listed</option>
                        <?php
      while ($row = $suppliers->fetch_assoc()) {
        echo '<option value="'.$row['supplier_id'].'">';
        echo $row['supplier'].'</option>';
        }
      ?>
      </select>
            </td>
          </tr>
             
          <tr>
            <th class="labelLeft">Garment type</th>
            <td colspan="2"><select name="garment_type" id="garment_type">
                <option value="0" selected="selected">Select garment type</option>
                <option value="other">Not listed</option>
                                                                    <?php
      while ($row = $garment_types->fetch_assoc()) {
        echo '<option value="'.$row['garment_type_id'].'">';
        echo $row['garment_type'].'</option>';
        }
        ?>
              </select>
            </td>
          </tr>
             
          <tr>
            <th class="labelLeft">Garment category(s)</th>
            <td colspan="2"><select name="garment_cat[]" size="6" multiple="multiple" id="garment_cat">
                <option value="choose" selected="selected">Select garment category(s)</option>
                <option value="other">Not listed</option>
         <?php
       while ($row = $categories->fetch_assoc()) {
         echo '<option value="'.$row['category_id'].'">';
         echo $row['category'].'</option>';
         }
       ?>
              </select>
            </td>
          </tr>
          <tr>
            <th class="labelLeft">Title</th>
            <td colspan="2"><input name="title" type="text" class="widebox" id="title" /></td>
          </tr>
          <tr>
            <th class="labelLeft" scope="row">Style Number </th>
            <td colspan="2"><input name="stylenum" type="text" class="narrowbox" id="stylenum" /></td>
          </tr>
          <tr>
            <th class="labelLeft" scope="row">Description</th>
            <td colspan="2"><textarea name="description" id="description"></textarea></td>
          </tr>
          <tr>
            <th class="labelLeft" scope="row">Extra info </th>
            <td colspan="2"><input name="extrainfo" type="text" class="widebox" id="extrainfo" /></td>
          </tr>
          <tr>
            <th class="labelLeft" scope="row">Colours</th>
            <td colspan="2"><select name="colour[]" size="6" multiple="multiple" id="colour">
                <option value="choose" selected="selected">Select colour(s)</option>
                <option value="other">Not listed</option>
                                                                    <?php
      while ($row = $colours->fetch_assoc()) {
        echo '<option value="'.$row['colour_id'].'">';
        echo $row['colour'].'</option>';
        }
      ?>
              </select>

            </td>
          </tr>
          <tr>
            <th class="labelLeft" scope="row">Swatch image </th>
            <td colspan="2">Yes
              <input name="swatch" type="radio" value="y" />
              No
              <input name="swatch" type="radio" value="n" /></td>
          </tr>
          <tr>
            <th class="labelLeft" scope="row">Sizes</th>
            <td colspan="2"><select name="sizes" id="sizes">
                <option value="0" selected="selected">Select sizes</option>
                <option value="other">Not listed</option>
                                                          <?php
      while ($row = $sizes->fetch_assoc()) {
        echo '<option value="'.$row['size_id'].'">';
        echo $row['size'].'</option>';
        }
          // close database connection
      $db->close();
        ?>
                    </select>
            </td>
          </tr>
          <tr>
            <th class="labelLeft" scope="row">Image</th>
            <td width="190">Yes
              <input name="image" type="radio" value="y" />
              No
              <input name="image" type="radio" value="n" /></td>
            <td id="garmentSubmit"><input type="submit" name="Submit" value="Insert new garment" /></td>
          </tr>
        </table>
      </form>
    </div>
  </div>
</div>
<!-- InstanceEndEditable --><img src="../Images/transparent.gif" width="1" height="1"/>
<div id="footer">&copy; Copyright 200. All rights reserved. </div>
</body>
<!-- InstanceEnd --></html>
[+][-]06/25/07 06:15 PM, ID: 19360378Assisted Solution

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]06/25/07 06:59 PM, ID: 19360546Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06/25/07 07:01 PM, ID: 19360555Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06/25/07 07:14 PM, ID: 19360606Assisted Solution

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]06/25/07 07:16 PM, ID: 19360621Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06/25/07 07:22 PM, ID: 19360639Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06/25/07 07:35 PM, ID: 19360676Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06/25/07 07:45 PM, ID: 19360696Assisted Solution

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]06/25/07 08:33 PM, ID: 19360807Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06/25/07 08:59 PM, ID: 19360868Assisted Solution

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]06/25/07 10:03 PM, ID: 19361065Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06/25/07 10:33 PM, ID: 19361141Assisted Solution

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]06/25/07 11:21 PM, ID: 19361270Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06/26/07 12:13 AM, ID: 19361407Assisted Solution

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]06/26/07 04:33 AM, ID: 19362634Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06/26/07 07:25 AM, ID: 19363946Assisted Solution

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]06/26/07 07:41 AM, ID: 19364113Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06/26/07 08:01 AM, ID: 19364346Assisted Solution

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]06/26/07 08:33 AM, ID: 19364774Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06/26/07 08:34 AM, ID: 19364786Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06/26/07 08:49 AM, ID: 19364964Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06/26/07 08:51 AM, ID: 19364983Assisted Solution

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]06/26/07 09:01 AM, ID: 19365131Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06/26/07 09:09 AM, ID: 19365216Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: PHP Scripting Language, MySQL Server, PHP and Databases
Tags: lvl_openwin, php
Sign Up Now!
Solution Provided By: DrNikon224
Participating Experts: 2
Solution Grade: A
 
[+][-]06/26/07 09:25 AM, ID: 19365379Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06/26/07 11:05 AM, ID: 19366257Assisted Solution

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]06/26/07 04:50 PM, ID: 19368705Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06/26/07 05:48 PM, ID: 19368921Assisted Solution

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]06/26/07 06:12 PM, ID: 19369004Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091021-EE-VQP-81