简体   繁体   中英

Php and MySQL for a recipe database

I'm building a food recipe website databases. Have a few problems inserting on to 2 tables with one function. I'm building in with baby steps to get it right little by little. I have got it to work one time but it is when I add more code it stops.

Getting a SQL syntax error when I add ingredient 2 to my html. It worked with one input box.

function insert_recipe($recipe){
    global $db; 

    $sql = "INSERT INTO Recipe ";
    $sql .= "(name, description) ";
    $sql .= "VALUES (";
    $sql .= "'" . $recipe['name'] . "',";
    $sql .= "'" . $recipe['description'] . "'";
    $sql .= "); ";
    $sql .= "INSERT INTO Ingredient ";
    $sql .= "(name) ";
    $sql .= "VALUES (";
    $sql .= "'" . $recipe['ingredient1'] . "'";
    $sql .= ");";   
    $result = mysqli_query($db, $sql);
    // For INSERT statements, $result is true/false
    if ($result){
        return true;
    } else {
        //INSERT failed
        echo mysqli_error($db);
        db_disconnect($db);
        exit;   
    }
}

There is a measurement table I don't have a picture of that I'm not working on that part yet. I'm thinking it may have something to do with each and input box for ingredients has the same name <input name="ingredient" type="text" class="form-control trowlgray" placeholder="Suger" aria-describedby="sizing-addon1"> going to need to be able to have an ID for every ingredient.

I believe that mysqli_query() only support single statements, not multiples.

The good news is that I know that mysqli_multi_query() supports multiple statements separated by a semi-colon in a single call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM