Jump to content

php sql form submit to more than one table


Helter_Skleter

Recommended Posts

i have a form for my admin to update the products and their deails on my site. but i have decided to add stock and supplier tables to the database. so i have a products table, stock and supplier. each have an id.

 

in my admin form, id like the admin to submit once to multiple tables...

 

php code that i need to improve

<?php
// Parse the form data and add inventory item to the system

if (isset($_POST['product_name'])) {

    $product_name = $_POST['product_name'];
    $price = $_POST['price'];
    $category = $_POST['category'];
    $stock = $_POST['stock'];
    $details = $_POST['details'];
    $rating =$_POST['rating'];
    $letter = $_POST['letter'];
    $supplier = $_POST['supplier'];        

    // See if that product name is an identical match to another product in the system
    $sql = mysqli_query($con, "SELECT id FROM products WHERE product_name='$product_name' LIMIT 1");
    $productMatch = mysqli_num_rows($sql); // count the output amount
    //if product match is greater than 0
    if ($productMatch > 0) {
        echo 'Sorry you tried to place a duplicate "Product Name" into the system, <a href="inventory_list.php">click here</a>';
        exit();
    }
    // Add this product into the database now by sql query
    $sql = mysqli_query($con, "INSERT INTO products (product_name, price, details, category, stock, letter, supplier,  rating, date_added)
        VALUES('$product_name','$price','$details','$category', '$stock' ,'$letter',' $supplier' , '$rating' now())") or die (mysqli_error());        
     $pid = mysqli_insert_id($con);    
    // Place image in the folder
        $newname = "$pid.jpg";
        move_uploaded_file($_FILES['fileField']['tmp_name'], "../inventory_images/$newname"); //_FILES is a global var
    
    header("location: inventory_list.php"); //auto refresh, and it wont try to re add the item
    exit();//not necessary but its always safe to exit
}
?>

html form
<form action="inventory_list.php" enctype="multipart/form-data" name="myForm" id="myForm" method="post">
                <table width="95%" border="0" align="center" cellpadding="5" cellspacing="0" summary="this is where the admin will insert new items">
          <caption><div id="title"><a id="add_new_item"></a>Add New Inventory Item</div>
          </caption>
          <tr >
            <th width="30%" scope="col" align="left">Product Image</th>
            <th width="70%"><label class="desc">
            <input type="file" name="fileField" id="fileField"   autofocus required />
            </label></td>
          </tr>
          <tr >
            <th width="30%" scope="col" align="left">Product Name</th>
            <th width="70%" scope="col" align="left">
            <label class="desc">
                <input name="product_name" type="text" id="product_name" size="64"  autofocus required />
            </label></th>
          </tr>
          <tr>
            <th width="30%" scope="row" align="left">Letter</th>
            <td width="70%"> <label class="desc">
                <input name="letter" type="text" id="letter"  autofocus required />
            </label></td>
          </tr>
          <tr>
            <th width="30%" scope="row" align="left">Product Price</th>
            <td width="70%"> <label class="desc">
                <input name="price" type="text" id="price"  autofocus required />
            </label></td>
          </tr>
          <tr>
            <th width="30%" scope="row"align="left">Category</th>
            <td width="70%"><label class="desc">
            <select name="category" id="category">
            <option value="Fruits">Fruits</option>
            <option value="Vegetables">Vegetables</option>
            <option value="Indoor Plants">Indoor Plants</option>
            <option value="Outdoor Plants">Outdoor Plants</option>
            </select>
            </label></td>
          </tr>
          <tr>
            <th width="30%" scope="row" align="left">Suppliers</th>
            <td width="70%"> <label class="desc">
            <select name="supplier" id="supplier">
            <option value="Kenny's Organic Supplies">Kenny's Organic Supplies</option>
            <option value="Mom's Garden">Mom's Garden</option>
            <option value="Big Brother">Big Brother</option>
            </select>
            </label></td>
          </tr>
          <tr>
            <th width="30%" scope="row" align="left">Stock</th>
            <td width="70%"> <label class="desc">
                <input name="stock" type="text" id="stock"  autofocus required />
            </label></td>
          </tr>
          <tr>
            <th width="30%" scope="row"align="left">Product Details</th>
            <td width="70%"><label class="desc">
            <textarea name="details" id="details" cols="35" rows="5"  autofocus required></textarea>
            </label></td>
          </tr>
              <tr>
            <th width="30%" scope="row" align="left">Rating</th>
            <td width="70%"><label class="desc">
            <select name="rating" id="rating" >
            <option value="1">1 of 3 Stars</option>
            <option value="2">2 of 3 Stars</option>
            <option value="3">3 of 3 Stars</option>
            </select>
            </label></td>
          </tr>

          <tr>
            <th width="30%" scope="row"> </th>
            <td width="70%" align="right"><label class="desc">
            <input type="submit" name="button" id="button" value="Add This Item Now" />
            </label></td>
          </tr>
          <tr>
            <th width="30%" scope="row"> </th>
            <td width="70%"> </td>
          </tr>
      </table>
    </form>
Edited by Ch0cu3r
Link to comment
Share on other sites

php code that i need to improve

Your php is not that bad. It's your English and HTML that needs work!

 

Your html is horrific! Absolutely no rhyme nor reason for why you are writing what you have!

 

- you have a series of heading tags with input elements embedded in them.

- you have label tags with no labels in them

- you have buried your input tags inside your label tags

- you are using outdated styling in your html that really needs to be in CSS

- you have an anchor tag with no visible text to indicate that it exists.

- you have th tags that end with a td tag

And more!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.