Loop table to INSERT back to MySQL - php

I have a PHP page that queries a database, and populates multiple tables with values. Those values need to be modified and placed in a separate table.
I know how to look through the database to show all the tables, but I can't find anything about how to loop through the html tables to reinsert the new values. I can't simply use an id and a ton of functions because there is an indeterminate number of rows.
Here is a list of the columns in the table I am accessing:
Here is a snippet of my code that I am using to access the database and populate the tables:
function tableAddData( $tableNum )
{
// Access Global Variables
global $noteId, $itemId, $quantityId, $costId, $laborHoursId, $totalHoursId, $materialCostId, $laborCostId, $totalCostId, $itemNumberId, $servername, $username, $password, $dbname, $quantityChangeId, $qtyRow;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "select
`note` as `Note`,
`item` as `Item`,
`qty` as `Quantity`,
`cost` as `Cost`,
`hrsLabor` as `Labor Hours`,
`rate` as `Labor Rate`,
`qty` * `hrsLabor` as `Total Hours`,
FORMAT(`qty` * `cost`, 2) as `Material Cost`,
FORMAT(`qty` * `rate` * `hrsLabor`, 2) as `Labor Cost`,
FORMAT(( `qty` * `cost` ) + ( `qty` * `hrsLabor` * `rate` ), 2) as `Total Cost`,
`itemNo` as `Item Number`
from
`test_newtable`,
labor_rates
where
test_newtable.laborRate = labor_rates.id AND test_newtable.tableNo = " . $tableNum . ";";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
if ($row["Note"] != "" ) {
$noteHtml = '<td id="Note" class="text-center">
<span data-toggle="tooltip" data-placement="top" title="' . $row["Note"] . '">
<span class="glyphicon glyphicon-comment text-danger"></span>
</span>
</td>';
}
else {
$noteHtml = '<td></td>';
};
echo '
<tr>'
. $noteHtml . '
<td id="Item" >' . '<input class="text-left" value="' . $row["Item"] . '" disabled/></td>
<td id="Quantity">' . '<input class="text-center qty" id="qty-' . $qtyRow . '" value="' . $row["Quantity"] . '" /></td>
<td class="dollarRight" style="padding-left:0;padding-right:0; width:20px;">' . '<input class="text-center text-muted" value="$" disabled/></td>
<td id="Cost" class="dollarLeft">' . '<input class="text-right" id="cost-' . $qtyRow . '" value="' . $row["Cost"] . '" /></td>
<td id="Labor Hours">' . '<input class="text-right" id="laborHours-' . $qtyRow . '" value="' . $row["Labor Hours"] . '" /></td>
<td id="Total Hours">' . '<input class="text-right totalHours-' . $tableNum . '" id="totalHours-' . $qtyRow . '" value="' . $row["Total Hours"] . '" disabled/></td>
<td class="dollarRight" style="padding-left:0;padding-right:0; width:20px;">' . '<input class="text-center text-muted" value="$" disabled/></td>
<td id="Material Cost" class="dollarLeft">' . '<input class="text-right" id="materialCost-' . $qtyRow . '" value="' . $row["Material Cost"] . '" disabled/></td>
<td class="dollarRight" style="padding-left:0;padding-right:0; width:20px;">' . '<input class="text-center text-muted" value="$" disabled/></td>
<td id="Labor Cost" class="dollarLeft">' . '<input class="text-right" id="laborCost-' . $qtyRow . '" value="' . $row["Labor Cost"] . '" disabled/></td>
<td class="success dollarRight" width="1%" style="padding-left:0">' . '<input class="text-muted" value="$" disabled/></td>
<td id="Total Cost" class="success dollarLeft">' . '<input class="text-right totalCost-' . $tableNum . '" id="totalCost-' . $qtyRow . '" value="' . $row["Total Cost"] . '" disabled/></td>
<td id="Item Number">' . '<input class="text-left" value="' . $row["Item Number"] . '" disabled /></td>
</tr>
';
update($tableNum);
$qtyRow = $qtyRow + 1;
}
}
}
Here is an example of what the page looks like now:

You need to update the database with news values from a form in html?
you could use the name field in the input like name="tablename-columnname-rownumber", then in post php
foreach($_POST as $key=> $value){
$array = preg_split("/-/",$key);
if(count($array)==3) // if had 3 values
{
// update table $array[0] set $array[1] = $value where id = $array[2]
// update table (table name) set (column name) = (value from post) where id = (row number)
}
}
but is better if you put all in an array and just do one update by row
$arraytemp = array();
foreach($_POST as $key=> $value){
$array = preg_split("/-/",$key);
if(count($array)==3) // if had 3 values
{
$arraytemp[$array[0]][$array[2]][$array[1]]=$value;
}
}
foreach($arraytemp as $tablename => $tabledata){
$query = "update ".$tablename;
foreach($tabledata as $id => $columndata){
foreach($columndata as $columnname => $value){
$query .=' set '.$columnname'='.$value.';
}
$query.=' where id='.$id;
//exceute query
}
}
(i don't try it, just is what came to my mind, hope it works)

Related

How can I display all the results of a row in a loop within a loop?

What I have currently looks like this
What I want is that with section name "asd" you can also select "Home" and any other option which might be in there, but for some reason it isn't working.
<table class="table">
<tr>
<td style="width: 60%;">Section name</td>
<td>Edit section</td>
<td>Category</td>
<td>Order</td>
</tr>
<form method="post">
<?php
$query = $handler->query('SELECT * FROM section ORDER BY sorder');
$cquery = $handler->query('SELECT * FROM category');
if($query->rowCount()){
while($fetch = $query->fetch(PDO::FETCH_ASSOC)){
echo'
<tr>
<td><strong>' . $fetch['secname'] . '</strong><br />' . $fetch['secdesc'] . '</td>
<td><a class="btn btn-warning" href="index.php?p=edit&id=' . $fetch['sc_id'] . '"><i class="fa fa-times fa-fw"></i> Edit</a>
<a class="btn btn-danger" href="index.php?p=del&id=' . $fetch['sc_id'] . '"><i class="fa fa-times fa-fw"></i> Delete</a></td>
<td><select name="' . $fetch['c_id'] . '" class="form-control" size="1">
';
while($cfetch = $cquery->fetch(PDO::FETCH_ASSOC)){
echo'<option value="' . $cfetch['c_id'] . '" ' . (($cfetch['c_id'] == $fetch['c_id'])? 'selected' : '') . '>' . $cfetch['categoryname'] . '</option>';
}
echo'
</select></td>
<td><input type="number" class="form-control" name="' . $fetch['sc_id'] . '" value="' . $fetch['sorder'] . '" style="width: 60px;" /></td>
</tr>
';
}
}
else{
echo '<tr><td colspan="4">' . $noResultsDisplay . '</td></tr>';
}
?>
<tr><td colspan="4"><input type="submit" name="section" class="btn btn-primary pull-right" value="Submit" /></td></tr>
</form>
</table>
The SQL that goes with it:
CREATE TABLE IF NOT EXISTS category(
`c_id` int(10) NOT NULL auto_increment,
`categoryname` varchar(250) NOT NULL,
`corder` int(10) NOT NULL,
PRIMARY KEY `c_id` (`c_id`),
UNIQUE KEY (`categoryname`)
)Engine=InnoDB;
INSERT INTO category (`categoryname`, `corder`) VALUES ('Home', 1);
CREATE TABLE IF NOT EXISTS section(
`sc_id` int(10) NOT NULL auto_increment,
`c_id` int(10) NOT NULL,
`secname` varchar(250) NOT NULL,
`secdesc` varchar(500),
`secimage` varchar(500),
`sorder` int(10) NOT NULL,
PRIMARY KEY `sc_id` (`sc_id`),
UNIQUE KEY (`secname`)
)Engine=InnoDB;
I am using PDO and $handler is my connection point.
I can see that I am on the right track, but for some reason the second loop is only looping once even though it should loop multiple times.
After sending the form, I want to set the category in the database, this is my current code:
if(isset($_POST['section'])){
$fetch = $query->fetch(PDO::FETCH_ASSOC);
$category = $_POST['category'];
foreach($_POST as $data => $value){
if($data != 'section'){
$sorder = (int)$value;
if($sorder != $fetch['c_id']){
echo"UPDATE section SET sorder = :sorder, c_id = :cid WHERE sc_id = :id', [':sorder' => $sorder, ':id' => $data, ':cid' => $category]";
//echo perry('UPDATE section SET sorder = :sorder, c_id = :cid WHERE sc_id = :id', [':sorder' => $sorder, ':id' => $data, ':cid' => $category], null);
}
}
}
}
But for some reason I can't get the options of the select menu to get send to the database.
No need to run this second loop nested (it always produces the same output). I would first get this output in a string:
while($cfetch = $cquery->fetch(PDO::FETCH_ASSOC)){
$option .= '<option value="' . $cfetch['c_id'] . '" ' . (($cfetch['c_id'] == $fetch['c_id'])? 'selected' : '') . '>' . $cfetch['categoryname'] . '</option>';
}
Then just echo it in the loop:
while($fetch = $query->fetch(PDO::FETCH_ASSOC)){
echo'
<tr>
<td><strong>' . $fetch['secname'] . '</strong><br />' . $fetch['secdesc'] . '</td>
<td><a class="btn btn-warning" href="index.php?p=edit&id=' . $fetch['sc_id'] . '"><i class="fa fa-times fa-fw"></i> Edit</a>
<a class="btn btn-danger" href="index.php?p=del&id=' . $fetch['sc_id'] . '"><i class="fa fa-times fa-fw"></i> Delete</a></td>
<td><select name="' . $fetch['c_id'] . '" class="form-control" size="1">
';
echo $option;
echo'
</select></td>
<td><input type="number" class="form-control" name="' . $fetch['sc_id'] . '" value="' . $fetch['sorder'] . '" style="width: 60px;" /></td>
</tr>
';
}

Unknown column 'xxxxxx' in 'field list'

I know there are a lot of topics on this, and I've looked at them all, and they don't help me. My table name is correct, no spaces or anything out of the ordinary. I've checked 100 times and checked 100 more. I'll post both bits of my code, and hopefully someone can help.
I get this error when I try to use the submit button:
Error updating odds: Unknown column 'homeOdds' in 'field list'
POST:
if ($_POST['action'] == 'Update') {
foreach($_POST['game'] as $game) {
$homeScore = ((strlen($game['homeScore']) > 0) ? $game['homeScore'] : 'NULL');
$homeOdds = (str_replace("\xBD", ".5", $homeScore));
$visitorScore = ((strlen($game['visitorScore']) > 0) ? $game['visitorScore'] : 'NULL');
$visitorOdds = (str_replace("\xBD", ".5", $visitorScore));
$sql = "update " . $db_prefix . "schedule ";
$sql .= "set homeOdds = '" . $homeOdds . "', visitorOdds = '" . $visitorOdds . "' ";
$sql .= "where gameID = " . $game['gameID'];
mysql_query($sql) or die('Error updating odds: ' . mysql_error());
}
header('Location: index.php');
}
Table/Form & Update button:
<form id="scoresForm" name="scoresForm" action="odds.php" method="post">
<input type="hidden" name="week" value="<?php echo $week; ?>" />
<?php
$sql = "select s.*, ht.city, ht.team, ht.displayName, vt.city, vt.team, vt.displayName ";
$sql .= "from " . $db_prefix . "schedule s ";
$sql .= "inner join " . $db_prefix . "teams ht on s.homeID = ht.teamID ";
$sql .= "inner join " . $db_prefix . "teams vt on s.visitorID = vt.teamID ";
$sql .= "where weekNum = " . $week . " ";
$sql .= "order by gameTimeEastern";
$query = mysql_query($sql);
if (mysql_num_rows($query) > 0) {
echo '<table cellpadding="4" cellspacing="0" class="table1">' . "\n";
echo ' <tr><th colspan="6" align="left">Week ' . $week . '</th></tr>' . "\n";
$i = 0;
while ($result = mysql_fetch_array($query)) {
$homeTeam = new team($result['homeID']);
$visitorTeam = new team($result['visitorID']);
$rowclass = (($i % 2 == 0) ? ' class="altrow"' : '');
echo ' <tr' . $rowclass . '>' . "\n";
echo ' <td><input type="hidden" name="game[' . $result['gameID'] . '][gameID]" value="' . $result['gameID'] . '" />' . date('D n/j g:i a', strtotime($result['gameTimeEastern'])) . ' ET</td>' . "\n";
echo ' <td align="right"><input type="hidden" name="gameID[' . strtolower($visitorTeam->team) . ']" value="' . $result['gameID'] . '" />' . $visitorTeam->teamName . '</td>' . "\n";
echo ' <td><input type="text" name="game[' . $result['gameID'] . '][visitorScore]" id="game[' . $result['gameID'] . '][visitorScore]" value="' . $result['visitorOdds'] . '" size="3" /></td>' . "\n";
echo ' <td align="right"><input type="hidden" name="gameID[' . strtolower($homeTeam->team) . ']" value="' . $result['gameID'] . '" />at ' . $homeTeam->teamName . '</td>' . "\n";
echo ' <td><input type="text" name="game[' . $result['gameID'] . '][homeScore]" id="game[' . $result['gameID'] . '][homeScore]" value="' . $result['homeOdds'] . '" size="3" /></td>' . "\n";
echo ' </tr>' . "\n";
$i++;
}
echo '</table>' . "\n";
}
?>
<br><input type="submit" name="action" value="Update" />
</form>
Any help is appreciated.
For debugging this, echo (or var_dump) the dynamically generated SQL contained in the $sql variable, before you submit it to the database.
Then take that statement to another client to test it.
MySQL is telling you that the table schedule which you are referencing doesn't contain a column named homeOdds.
We don't see the contents of all the variables that are being incorporated into the SQL text. (The code appears to be vulnerable to SQL Injection.

How to know if a radiobutton is checked?

I'm working with an examination page using php connected to a MySQL database. I'm currently on the part where i need to check if the the answers checked on the radiobutton is correct.
QUESTION: How will i know which radiobutton is checked, and how will i know if the answer on that radiobutton is equal to the data in the database? I want to display the "CORRECT" if it's the same or "INCORRECT" if it's wrong in a different page.
Here is my PHP code for the radiobuttons:
$sql = 'SELECT q_question, q_correct, q_answer2, q_answer3, q_answer4 FROM tblquestions WHERE q_category = "' . $legend3 . '" ORDER BY rand() LIMIT ' . $limit3;
$retval = mysql_query($sql,$conn);
if(!$retval)
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
$span1 = $row['q_question'];
$obj1 = $row['q_correct'];
$obj2 = $row['q_answer2'];
$obj3 = $row['q_answer3'];
$obj4 = $row['q_answer4'];
$my_array = array($obj1, $obj2, $obj3, $obj4);
shuffle($my_array);
$rad1 = $my_array[0];
$rad2 = $my_array[1];
$rad3 = $my_array[2];
$rad4 = $my_array[3];
echo '<table width="100%" border="0" cellpadding="1" cellspacing="1">';
echo '<tbody><tr><td colspan="2" width="300" height="40"><span id="question1">' . $count . '. ' . $span1 . '</span></td></tr>';
echo '<tr><tbody><tr><td width="50%"><input name="q' . $nameCTR1 . '" type="radio" class="validate[required] checkbox" id="a[I1A]" value="'. $rad1 .'">A. ' . $rad1 . '</td>';
echo '<td width="50%"><input name="q' . $nameCTR1 . '" type="radio" class="validate[required] checkbox" id="a[I1B]" value="' . $rad2 . '">B. ' . $rad2 . '</td></tr></tbody></tr></tbody>';
echo '<tr><tbody><tr><td width="50%"><input name="q' . $nameCTR1 . '" type="radio" class="validate[required] checkbox" id="a[I1C]" value="' . $rad3 . '">C. ' . $rad3 . '</td>';
echo '<td width="50%"><input name="q' . $nameCTR1 . '" type="radio" class="validate[required] checkbox" id="a[I1D]" value="' . $rad4 . '">D. ' . $rad4 . '</td></tr></tbody></tr></tbody></table>';
echo '<hr width="100%"></hr>';
$nameCTR1 = $nameCTR1 + 1;
Checkbox and radio type filed does not get posted when they are unchecked or not-selected.
So in your action script(the one which you have set in action attribute of form) you can check whether their value has been set in $_POST (if your form method is POST) or not.
If it exist then it implies that fields are checked or selected else not.
I made a function for you. Just call the function getRadio(name of radiobutton here) and it will return the value if it was checked.
function getRadio( $radioName ) {
$radio = $_POST[ $radioName ]; // RADIO NAME IS DEFINED IN HTML
if( isset( $radio ) ) {
return $radio;
}
return $radioName . " is not set";
}
*Also this assumes you are using the post method

There is no data in database but return an error from the if statement [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
<?php
include("Header.php");
echo "<br>";
$stockCode = "";
$stockItem = "";
$stockLeft = "";
$stockOut = "";
$minimum = "";
$pricePI = "";
$location = "";
// Check if posts are set
if(isset($_POST['stockCode'])){
$stockCode = $_POST['stockCode'];
}
if(isset($_POST['stockItem'])){
$stockItem = $_POST['stockItem'];
}
if(isset($_POST['stockLeft'])){
$stockLeft = $_POST['stockLeft'];
}
if(isset($_POST['stockOut'])){
$stockOut = $_POST['stockOut'];
}
if(isset($_POST['minimum'])){
$minimum = $_POST['minimum'];
}
if(isset($_POST['pricePI'])){
$pricePI = $_POST['pricePI'];
}
if(isset($_POST['location'])){
$location = $_POST['location'];
}
// Do validation before adding to database
if(
($stockCode == null)||
($stockItem == null)||
($stockLeft == null)||
($stockOut == null)||
($pricePI == null)||
($location == null)||
($location == "Select Location")
){
echo "<div class='font3'>Fields Stock Code, Stock Item, Stock Left, Stock Out, Price Per Item and location must be filled.</div>";
} else {
// Write to database
$connection = mysqli_connect($host,$user,$pass,$dbnm);
// Check to see if the Stock Code already exists in the database
$checkQ = "SELECT COUNT(*) FROM Ekhaya_Inventory WHERE ((ekhaya_inventory_stock_code = '" . $stockCode . "') AND (ekhaya_inventory_location = '" . $location . "'))";
$result = mysqli_query($connection,$checkQ);
while($row[0] = mysqli_fetch_array($result)){
echo $row[0];
if($row[0] == 0){
$quick_cal = $stockLeft * $pricePI;
$formated_cal = number_format($quick_cal,2);
$myquery =
"INSERT INTO Ekhaya_Inventory
(
ekhaya_inventory_stock_code,
ekhaya_inventory_stock_item,
ekhaya_inventory_quantity_stock_left,
ekhaya_inventory_quantity_stock_out,
ekhaya_inventory_quantity_minimum,
ekhaya_inventory_price_per_item,
ekhaya_inventory_value_of_stock_left,
ekhaya_inventory_location,
ekhaya_inventory_date_time_modified
)
VALUES
(
'" . $stockCode . "',
'" . $stockItem . "',
'" . $stockLeft . "',
'" . $stockOut . "',
'" . $minimum . "',
'" . $pricePI . "',
'" . $formated_cal . "',
'" . $location . "',
'" . $date . "'
)
";
mysqli_query($connection,$myquery);
echo "<div class='font3'>New Item Added Successfully</div>";
// Add a log to database
$query =
"INSERT INTO Ekhaya_Logs
(
ekhaya_logs_name,
ekhaya_logs_surname,
ekhaya_logs_username,
ekhaya_logs_activity,
ekhaya_logs_ip_address,
ekhaya_logs_date_time
) VALUES (
'" . $_SESSION['SECURE#NAME'] . "',
'" . $_SESSION['SECURE#SURNAME'] . "',
'" . $_SESSION['SECURE#USERNAME'] . "',
'" . $_SESSION['SECURE#NAME'] . " " . $_SESSION['SECURE#SURNAME'] . " added [ITEM] Stock Code: " . $stockCode . " to " . $location . "'s inventory',
'" . $_SERVER['REMOTE_ADDR'] . "',
'" . $date . "')";
mysqli_query($connection,$query);
} else {
echo "<div style='color:red' class='font3'>Stock Code " . $stockCode . " already exists for " . $location . "</div>";
}
}
mysqli_close($connection);
$stockCode = "";
$stockItem = "";
$stockLeft = "";
$stockOut = "";
$minimum = "";
$pricePI = "";
$location = "";
}
?>
</br>
<form action="AddItem.php" method="post">
<table class="table_mod">
<tr>
<td class="td_mod">
Stock Code:
</td>
<td class="td_mod">
<input type="text" name="stockCode">
</td>
</tr>
<tr>
<td class="td_mod">
Stock Item:
</td>
<td class="td_mod">
<input type="text" name="stockItem">
</td>
</tr>
<tr>
<td class="td_mod">
Stock Left:
</td>
<td class="td_mod">
<input type="text" name="stockLeft">
</td>
</tr>
<tr>
<td class="td_mod">
Stock Out:
</td>
<td class="td_mod">
<input type="text" name="stockOut">
</td>
</tr>
<tr>
<td class="td_mod">
Minimum:
</td>
<td class="td_mod">
<input type="text" name="minimum">
</td>
</tr>
<tr>
<td class="td_mod">
Price Per Item:
</td>
<td class="td_mod">
<input type="text" name="pricePI">
</td>
</tr>
<tr>
<td class="td_mod">
Location:
</td>
<td class="td_mod">
<select class="textbox_login" name="location">
<option>Select Location</option>
<option value="Tech Stud">Tech Stud</option>
</select>
</td>
</tr>
<tr>
<td>
<input type="Submit" value="Submit New Item">
</td>
</tr>
</table>
</form>
Can someone please tell me why this is not working ? The main if statement pushes out this item already exists but the data does note exist in the database. And when i use the mysqli count function it always returns 1.
There is a bug in your while loop assignment. Change this line:
while($row[0] = mysqli_fetch_array($result)){
To this:
while($row = mysqli_fetch_array($result)){
You might consider generally using var_dump instead of echo when debugging. It can provide more insight into what's actually going on.

insert multiple post with one request in table opencart

Trying to insert a query multiple records in the database, but get an error:
Warning: mysql_real_escape_string() expects parameter 1 to be string, array given in
Below I attach the code type and model
Please show where the error or where and in what direction the engine
view
<tr class="green_table">
<td class="td">
<input type="text" name="forma[]" />
<?php if ($error_forma) { ?>
<span class="error"><?php echo $error_forma; ?></span>
<?php } ?>
</td>
<td class="td">
<input type="text" name="linkto[]" />
</td>
<td class="td">
<input type="text" name="description[]" />
<?php if ($error_description) { ?>
<span class="error"><?php echo $error_description; ?></span>
<?php } ?>
</td>
<td class="td">
<input type="text" name="cvet[]" />
<?php if ($error_cvet) { ?>
<span class="error"><?php echo $error_cvet; ?></span>
<?php } ?>
</td>
<td class="td">
<input type="text" name="sizes[]" />
<?php if ($error_sizes) { ?>
<span class="error"><?php echo $error_sizes; ?></span>
<?php } ?>
</td>
<td class="td">
<input type="text" name="counts[]" />
<?php if ($error_counts) { ?>
<span class="error"><?php echo $error_counts; ?></span>
<?php } ?>
</td>
<td class="td">
<input type="text" name="tcena[]" />
<?php if ($error_tcena) { ?>
<span class="error"><?php echo $error_tcena; ?></span>
<?php } ?>
</td>
</tr>
model:
foreach($data as $key => $value){
$query = $this->db->query("INSERT INTO `" . DB_PREFIX
. "order` SET customer_id = '" . (int)$data['customer_id']
."',forma = '" . $this->db->escape($data['forma'])
. "', linkto = '" . $this->db->escape($data['linkto'])
. "', description = '" . $this->db->escape($data['description'])
. "', cvet = '" . $this->db->escape($data['cvet'])
. "', sizes = '" . $this->db->escape($data['sizes'])
. "', counts = '" . (int)$data['counts']
. "', tcena = '" . (int)$data['tcena']
. "', sposob = '" . $this->db->escape($data['sposob'])
. "', delivery_usa = '" . $this->db->escape($data['delivery_usa'])
. "', hint = '" . $this->db->escape($data['hint'])
. "', novapochta = '" . $this->db->escape($data['novapochta'])
. "', customer_group_id = '" . (int)$data['customer_group_id']
. "', firstname = '" . $this->db->escape($data['firstname'])
. "', lastname = '" . $this->db->escape($data['lastname'])
. "', email = '" . $this->db->escape($data['email'])
. "', telephone = '" . $this->db->escape($data['telephone'])
. "', date_added = '" . $this->db->escape(date('Y-m-d H:i:s'))
."', order_status_id =' 1"
."'");
$new_order_id = $this->db->getLastId();
}
Thanks!
This has nothing to do with OpenCart itself, it is about basic PHP. You need to loop through all the posted values and insert one set of values at a time.
It should be like this:
$values_count = count($data['forma']);
for ($i = 0; $i < $values_count; $i++) {
$query = $this->db->query("INSERT INTO `" . DB_PREFIX . "order` SET"
. " customer_id = " . (int)$data['customer_id']
. ",forma = '" . $this->db->escape($data['forma'][$i])
. "', linkto = '" . $this->db->escape($data['linkto'][$i])
. "', description = '" . $this->db->escape($data['description'][$i])
. "', cvet = '" . $this->db->escape($data['cvet'][$i])
. "', sizes = '" . $this->db->escape($data['sizes'][$i])
. "', counts = " . (int)$data['counts'][$i]
. ", tcena = " . (int)$data['tcena'][$i]
. ", sposob = '" . $this->db->escape($data['sposob'][$i])
. "', delivery_usa = '" . $this->db->escape($data['delivery_usa'][$i])
. "', hint = '" . $this->db->escape($data['hint'][$i])
. "', novapochta = '" . $this->db->escape($data['novapochta'][$i])
. "', customer_group_id = " . (int)$data['customer_group_id'] [$i]
. ", firstname = '" . $this->db->escape($data['firstname'][$i])
. "', lastname = '" . $this->db->escape($data['lastname'][$i])
. "', email = '" . $this->db->escape($data['email'][$i])
. "', telephone = '" . $this->db->escape($data['telephone'][$i])
. "', date_added = NOW()"
. ", order_status_id = 1");
$new_order_id = $this->db->getLastId();
}
Integer values does not need to be escaped with '1' when You typecast them to (int).
You form fields are arrays like : forma[], linkto[] which cannot be used with $this->db->escape().
Please echo the variable $value within the loop foreach($data as $key => $value){ and update your code.
Have a nice day !!

Categories