0

I'm currently working on a database where I would like the order_ID's to be put into two tables. This works but the issue I have is that the loop is only iterating once. If anyone could help and explain where I have gone wrong it would be greatly appreciated.

session_start();
echo $_SESSION['shop_id'];
$shopid = $_SESSION['shop_id'];
$username = $_SESSION['username'];
$con = mysqli_connect('localhost', 'root', '', 'aurora');
if (!isset($con)) {
    echo "Connection to Aurora System failed.";
}

if (isset($_POST['items'])) {
    echo "True";
} else {
    echo "false";
}

$valid = true;
$date =  date('l jS \of F Y h:i:s A');
$sql2 = "INSERT INTO orders_new (user_submitted, order_date, customer_ID) VALUES ('$username', '$date', '$shopid')";
if ($valid == true) {
    $ordersubmit2 = mysqli_query($con, $sql2);
    echo "Success!";
} 

$count = $_POST['items'];
for ($i = 1; $i <= $count; $i++) {
    $idinsert = mysqli_insert_id($con);
    $product = $_POST['product'.$i];
    $nicotine = $_POST['nicotine'.$i];
    $qty = $_POST['qty'.$i];
    echo $product;
    $sql = "INSERT INTO orders_detail (orders_id ,product, variant, quantity) VALUES ('$idinsert', '".$product."', '".$nicotine."', '".$qty."')";
    $ordersubmit = mysqli_query($con, $sql);
}
8
  • 2
    You are wide open to SQL Injections and should really use Prepared Statements instead of concatenating your queries. Specially since you're not escaping the user inputs at all. Commented Jan 19, 2017 at 13:33
  • 3
    what is the value of $count? just echo it before the loop. Commented Jan 19, 2017 at 13:33
  • 1
    Does this have anything to do with your previous question? stackoverflow.com/q/41675307/1415724 - Plus, we don't know what the HTML form / inputs look like, so you should probably include it in your question and probable JS etc. My guess; the element(s) is/are probably not treated as an array. Commented Jan 19, 2017 at 13:35
  • 1
    another tip: if you connect to root , with or without password, maybe you shouldn't show it to everybody (you could replace them by **** in the shown code) Commented Jan 19, 2017 at 13:37
  • Yeah it is in to regards of my previous question, The SQL injection protection will come later, im well aware but for the moment locally it's just more speed rather than precision. Commented Jan 19, 2017 at 13:39

1 Answer 1

1

Was my own fault, the order_id was a primary key hence the loop stopping due to only allowing unique values.

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.