0

I've created two php files with code for an order form for a cafeteria and the correspronding lines of code for inserting the posted values in my database. Here I present them.

CODE FOR addorder_form.php

**<?php 

       db_connect();

       $cats=array("Kafedes", "Rofhmata", "Pota", "Snack/Glyka"); 
       $arrlength=count($cats);

    for($i=0;$i<$arrlength;$i++) {  
        $sql = mysql_query('SELECT title FROM products WHERE cname="'.$cats[$i].'"') or die(mysql_error());
        echo '<div id="main_content">';
        echo "<h4 style=color:#800000> ".$cats[$i]."</h4>"; 
        echo "<br />"; 


        while($row = mysql_fetch_array($sql, MYSQL_BOTH)){
            echo "<div id='center' style='align:center'>";
            echo "<input style='text-align:right;' type='checkbox' action='addorder.php' name='products[]' value='".$row["title"]."'>".$row["title"];
            echo '</div>';

            echo ' <div id="center_side" style="float:right"><form "method="post" action="addorder.php"><input type="text" size="4" padding-left="0.2em" name="quantity"/>';
            echo '</div>';
        echo '</div>';  
        echo '<br />';  
        }
    }
    echo '<form name="addorder" method="" action="addorder.php" onclick="addorder.php">';
                echo '<input type="submit" value="Add order" style="float: right;"><br/>';
                echo '</form>';   
    ?>**

CODE FOR addorder.php

**<?php
        include_once("buzzcafe_fns.php");
        do_html_header("");




$quantity = '';
$title = '';

if (isset($_POST['quantity']) && isset($_POST['products'])) {
    if(isset($_POST["Submit"])) {
    $quantity = $_POST['quantity'];
$title = $_POST['products'];
if($_POST["Submit"] == "Submit")
{   
for ($i=0; $i<sizeof($title); $i++) {
db_connect();
$insertOrder = mysql_query("INSERT INTO orders VALUES('".$title[i]."','".$quantity."')")or die(mysql_error());

    }
echo "Record inserted"; 

}
}
}
?>**

When I run them I have non any syntax errors, though it does not work. As far as the db_connect() is set in the buzzcafe_fns.php file I've included and it is checked that works properly. How can I make my "INSERT INTO" work?

1 Answer 1

1
You can use another insert query
First You will check if $title variable array or not.
Next Using For loop
<?php 
$con=mysql_connect("localhost","root","");
$db=mysql_select_db("yourdatabasename here" ,$con);
$count=count($title);
for($i=0;$i<$count;$i++){
mysql_query("INSERT INTO orders SET title='".$title[$i]."', quantity='".$quantity."'");
}
?>
Sign up to request clarification or add additional context in comments.

1 Comment

I didn't help a lot .

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.