0

as you see in this code i made a table by selecting models from table in my database.. however i posted the return of the select query to be like the primary column for this table and put it into the while loop so it keeps generating rows till the models which came with the select query be finished now i got a a problem when i'm trying to get this models in a $_Post[''] supergloble it keeps send me only the last value it gets from the loop my question is how to get each and every value from the this loop to use it in a single insert query in my DB?

and sorry for the bad English :S !!

<form class="form-signin" action="<?php $_SERVER['PHP_SELF'];?>" method="Post">
<?php
$models = mysql_query("SELECT `Model_Name` FROM `models` WHERE `Brand` = 20");
while($row = mysql_fetch_array($models))
    {
    echo '
    <tr>
    <td><input type="text" name="mode[]" value="'.$row['Model_Name'].'"></td>
    <td><input type="text" name="sellout[]" value=""></td>
    <td><input type="text" name="shelfshare[]" value=""></td>
    <td><input type="text" name="price[]" value=""></td>
    <td><input type="text" name="Shortage[]" value=""></td>
    <td><input type="text" name="Inventory[]" value=""></td>
     </tr>

    ';
    }

    ?>

</form>

the inserting script

$date = date("Y-m-d");
foreach($_POST['mode'] as $key => $mode){
$sellout = $_POST['sellout'][$key];
$shelfshare = $_POST['shelfshare'][$key];
$price = $_POST['price'][$key];
$shortage = $_POST['shortage'][$key];
$inventory = $_POST['inventory'][$key];
mysql_query("INSERT INTO `smartdailyreport`(`SFO_Code`, `Model`, `Sell_Out`, `Shelf_Share`, `Price`, `Shortage`, `Inventory`, `Date`) VALUES ('".mysql_real_escape_string($_SESSION['idd'])."','".mysql_real_escape_string($mode)."','".mysql_real_escape_string($sellout)."','".mysql_real_escape_string($shelfshare)."','".mysql_real_escape_string($price)."','".mysql_real_escape_string($shortage)."','".mysql_real_escape_string($inventory)."','".mysql_real_escape_string($date)."')") or die(mysql_error());
}
4
  • $POST might throw an error for undefined variable - better fix it: $_POST Commented Oct 29, 2013 at 2:11
  • also, put your insert query inside the foreach loop, otherwise you're just overwriting your values each time it iterates Commented Oct 29, 2013 at 2:12
  • Before you write any more SQL code, you must read up on SQL injection bugs and proper escaping. mysql_query is also being removed from PHP in future versions because it's so easy to abuse like this. PDO is not hard to learn and makes it easier to write properly escaped queries. Commented Oct 29, 2013 at 4:22
  • about mysql_query i know it had been changed to mysqli_query but my webhost using old version of php so i have to use the old one :S Commented Oct 29, 2013 at 11:43

2 Answers 2

1

Make the name of those inputs an array :

 <tr>
<td><div class="col3" align="center"><input type="text" name="mode[]" class="form-control" value="'.$row['Model_Name'].'"></div></td>
<td><div class="col3" align="center"><input type="text" name="sellout[]" class="form-control" value=""></div></td>
<td><div class="col3" align="center"><input type="text" name="shelfshare[]" class="form-control" value=""></div></td>
<td><div class="col3" align="center"><input type="text" name="price[]" class="form-control" value=""></div></td>
<td><div class="col3" align="center"><input type="text" name="Shortage[]" class="form-control" value=""></div></td>
<td><div class="col3" align="center"><input type="text" name="Inventory[]" class="form-control" value=""></div></td>
 </tr>

Then when you process the form:

foreach($_POST['mode'] as $key=>$mode){
    $thisIsOne = $_POST['mode'][$key];
    $alsoThisOne = $_POST['sellout'][$key];
    etc...
}
Sign up to request clarification or add additional context in comments.

3 Comments

it still not working and now it isn't sending any data to the insert script even the model Skip a line and indent eight spaces. That's four spaces for the list and four to trigger the code block.
@MahmoudAhmed That's because this hasn't been changed/fixed $mode = $POST['mode'][$key]; notice $POST? it needs be $_POST
ok i fixed it but for now i have got the model values into the DB but i didn't get the other values it keeps sent me nothing :S
0

I'll put my comments into an answer for you...

You've got $POST as your $mode value, fix that up. (or remove it, $mode is already defined from the foreach)

Put your query inside your foreach loop, otherwise you just overwrite those variables each time you iterate, then insert the last one at the end

Put mysql_error into the die callback of mysql_query to show you an error if there is one (if you want to)

$date = date("Y-m-d");
foreach($_POST['mode'] as $key => $mode){
    $sellout = $_POST['sellout'][$key];
    $shelfshare = $_POST['shelfshare'][$key];
    $price = $_POST['price'][$key];
    $shortage = $_POST['shortage'][$key];
    $inventory = $_POST['inventory'][$key];
    mysql_query("INSERT INTO `smartdailyreport`(`SFO_Code`, `Model`, `Sell_Out`, `Shelf_Share`, `Price`, `Shortage`, `Inventory`, `Date`) VALUES ('".mysql_real_escape_string($_SESSION['idd'])."','".mysql_real_escape_string($mode)."','".mysql_real_escape_string($sellout)."','".mysql_real_escape_string($shelfshare)."','".mysql_real_escape_string($price)."','".mysql_real_escape_string($shortage)."','".mysql_real_escape_string($inventory)."','".mysql_real_escape_string($date)."')") or die(mysql_error());
}

Lastly, use mysqli_* instead of mysql as mysql has been deprecated for some time now. And also, use mysqli_real_escape_string or similar to escape your POST variables and save you from SQL Injection

2 Comments

PDO is preferable to mysqli because of named placeholders. Even better is a proper ORM like Propel or Doctrine.
thanks man for your comments the only problem for me now is how to the dynamic values from the while loop now after i fix the problems i got the model in the DB but but i didn't get the other values so any suggestions ??

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.