0

I am working on a project where i need to pass values (from a list of products) of multiple checkboxes AND text boxes to a table (new_orders) on the database.

I managed to create the list retrieving the products from the table articles, but i can't insert the products that i choose with the quantities that i insert to the table new_orders on the database.

Here is the code:

foreach($A_result as $key => $value) // για χ�?η�?η όλων των αντικειμένων του πίνακα A_apotelesma
{


// shows al the products
    echo "<tr> 
              <td align=center>".$j++."</td>
              <td align=center>".$value['name']."</td>
              <td align=center>".$value['price']."</td> ";

echo"<td> // each product has a checkbox and a text box to enter the quantity

            <input type=checkbox name=\"article[]\"></input>
            <input type=text name='quantity_".$value['id_article']."' size='3' maxlength='2'>
         </td>";

    echo"<td><input type='hidden' name='id_article' value=".$value['id_article']."></td>"; 
    echo"<td><input type='hidden' name=\"code_user\" value=\"code_user\"></td>"; 

    echo"</tr>";
}
}

?>  

 // The form above redirects to the insert_order.php page:


<?php

include("conn.php");

session_start();

$checkbx=$_POST['article'];

if($_POST['article']){

for($i=0;$i<sizeof($checkbx);$i++){

$quantity=$_POST['quantity_.$id_article'];

$username=$_SESSION['logged_user_username'];

$insert_order_query= "INSERT INTO new_orders (id_article, quantity, username) VALUES (".$id_article.",".$quantity.", '".$username."')";
//echo $insert_order_query;
$insert_order=mysql_query($insert_order_query) or die('Error,query failed!!'); 
 if ($insert_order)
     echo '<script language="javascript">alert("New order created!"); document.location="logged_in_user.php?menu=1";</script>';
 else
 {
    echo '<script language="javascript">alert("The order has not been created.")</script>';
    echo '<script language="javascript"> document.location="logged_in_user.php?menu=1.php"; </script>';
    exit();
 }
}
}

?>

I ALWAYS get the error message.

5
  • tl;dr but I think it would help if you tell other users what the error message is Commented Mar 17, 2014 at 16:04
  • Obligatory use prepared statements and not mysql because it is depreciated and you are vulnerable to SQL Injection Commented Mar 17, 2014 at 16:05
  • 1
    Also in this statement $quantity=$_POST['quantity_.$id_article']; What are you trying to accomplish? should it be $quantity=$_POST['quantity_'. $id_article]; Commented Mar 17, 2014 at 16:07
  • the error message is the or die message "Error, query failed!!" Commented Mar 17, 2014 at 19:41
  • with the statement $quantity=$_POST['quantity_'. $id_article]; i want to retrieve the article number to which corresponds the quantity entered on the previous form (with the checkbox). Commented Mar 18, 2014 at 14:51

2 Answers 2

0

that is because $quantity=$_POST['quantity_.$id_article']; should be $quantity=$_POST['quantity_'.$id_article];

0

Single qoutes are missing in

$insert_order_query= "INSERT INTO new_orders (id_article, quantity, username) VALUES (".$id_article.",".$quantity.", '".$username."')";

try something like

$insert_order_query= "INSERT INTO new_orders (id_article, quantity, username) VALUES ('".$id_article."','".$quantity."', '".$username."')";

but echo that $insert_order_query

paste here what you get - debugging tells a lot:)

EDIT

and especially do echo your $quantity

2
  • I used single quotes but no luck :-( Thanks for the debugging hint! Commented Mar 17, 2014 at 19:48
  • did u echo $insert_order_query sometimes query fails too if values are not comin in mentioned fields echo $insert_order_query can show you that too Commented Mar 17, 2014 at 20:05

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.