-1

I'm trying to update a table of dishes with a new entry and cross reference it to an existing table of ingredients. For each dish added, the user is required to assign existing ingredients and the volume required on multiple lines. On submission, the Dish should be entered into the table 'Dishes' and the assigned ingredients should be entered into the 'DishIng' linked tabled.

My tables are set like this:

Table: "Dishes" Columns: DishID, DishName, Serves, etc... Table: "DishIng" Columns: DishID, IngID, Volume Table: "Ingredients" Columns: IngID, IngName, Packsize etc...

HTML:

  • DishID:
  • Name:
  • Catagory :
  • Serving:
  • SRP:
  • Method :
  • Source :

  • IngID:
  • Volume:
  • <li>IngID: <input type="text" name="IngID"></li>
    <li>Volume: <input type="text" name="Volume"></li>
    
    <li>IngID: <input type="text" name="IngID"></li>
    <li>Volume: <input type="text" name="Volume"></li>
    
    </ul>
    <input type="submit">
    </form>
    

    Any suggestions for dymanically adding a row of ingredients in HTML would be very welcome.

    PHP:

    <?php
    
    require_once('db_connect.php');
    
    $DishID = mysqli_real_escape_string($con, $_POST['DishID']);
    $DishName = mysqli_real_escape_string($con, $_POST['DishName']);
    $DishCatID = mysqli_real_escape_string($con, $_POST['DishCatID']);
    $Serving = mysqli_real_escape_string($con, $_POST['Serving']);
    $SRP = mysqli_real_escape_string($con, $_POST['SRP']);
    $Method = mysqli_real_escape_string($con, $_POST['Method']);
    $SourceID = mysqli_real_escape_string($con, $_POST['SourceID']);
    $IngID = mysqli_real_escape_string($con, $_POST['IngID']);
    $Volume = mysqli_real_escape_string($con, $_POST['Volume']);
    
    $array = array('$DishID', '$IngID', '$Volume');
    
    
    $sql="INSERT INTO Dishes (DishID, DishName, DishCatID, Serving, SRP, Method, SourceID)
    VALUES ('$DishID', '$DishName', '$DishCatID', '$Serving', '$SRP', '$Method', '$SourceID')";
    
    $sql2 = "INSERT INTO DishIng (DishID, IngID, Volume) VALUES ('$DishID', '$IngID', '$Volume')";
    
    $it = new ArrayIterator ( $array );
    
    $cit = new CachingIterator ( $it );
    
    foreach ($cit as $value)
    {
      $sql2 .= "('".$cit->key()."','" .$cit->current()."')";
    
        if( $cit->hasNext() )
        {
            $sql2 .= ",";
        }
    }
    
    if (!mysqli_query($con,$sql)) {
      die('Error: ' . mysqli_error($con));
    }
    echo "1 record added";
    
    
    if (!mysqli_query($con,$sql2)) {
      die('Error: ' . mysqli_error($con));
    }
    echo "records added";
    
    
    require_once('db_disconnect.php');
    php?>
    

    Currently on submit, it only updates the 'Dishes' table and gives me this message: '1 record addedError: 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 '('0','$DishID'),('1','$IngID'),('2','$Volume')' at line 1'

    1
    • The original question has been edited. Commented Sep 23, 2014 at 7:26

    1 Answer 1

    1

    A high level of how you do this (although there are plenty of ways, this is just one with straight DB/PHP/HTML):

    1. Your php will create a form to input the dish fields from the user.
    2. Your php will then pull all of the ingredients from the ingredient table for that dish
    3. Your php will iterate through the results returned from that query and for each result will:
    4. Create a checkbox type input for the ingredient, and
    5. Create a Text field type input for the ingredient and.
    6. Create a Hidden field with the IngID

    Once the user submits the form:

    1. Your php will insert the to the dish table based on the dish fields on the form submitted.
    2. Your php will iterate through the ingredients fields from the form submission and with each one will:
    3. Determine if that ingredients checkbox is checked. If it is it will:
    4. Insert into the DishIng table using the Hidden IngID field and the Volume field

    Essentially, there is are two FOR loops. One to loop through the initial list of ingredients to make your form, and a second to loop through the form that is submitted. Each ingredient with a check mark will need it's own SQL INSERT statement to be added into the DishIng table.

    How to iterate through SQL results in php: Iterate through Mysql Rows in PHP

    How to iterate through Form fields in php: PHP loop through array of HTML textboxes

    Because you are taking in user input and sticking it into a MySQL insert query, you'll also want to make sure you sanitize the inputs before submitting the query so you avoid some evil-doer from pulling some SQL injection and killing your DB.

    Lastly: This is a pretty vague question so I anticipate it will be downvoted, and my response is also pretty vague. I only wrote it because it touches on an overall idea that is pretty common and is difficult to ask in a succinct way if you are just getting started with web development. You will probably get stuck quite a few times while writing this. Try narrowing down your problem to a single issue and take that issue/question to stackoverflow. You can also hit up Google, since everything you will need to do here has been written about on forums, blogs, wikis, and Q&A sites by a gajillion other folks.

    Sign up to request clarification or add additional context in comments.

    Comments

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.