Save multiple rows with multiple arrays - PHP - php

I have a big problem on saving multiple rows with multiple arrays into MYSQL. For example row 1 contains "name" and "share percentage". Then they add another 2 rows which contains same attributes as mentioned. So how do I save these data into DB. Below was my unsuccessful code:
foreach($_POST['name_members'] as $dir){ // array 1
$directorID = run_num('director_id','proc_director'); // generate running number for each row
foreach($_POST['share_percentage'] as $share) { //array 2
$insDirector = "INSERT INTO
proc_director(director_id, vendor_cd, director_name, director_percentage)
VALUES
('$directorID','$vendorID','".trim(addslashes($dir))."','$share')";
$db->query($insDirector); // save the array value into DB
}
}
I made demo interface, so that you can get the picture what I want. Here the hyperlink: http://softboxkid.com/blog/code/add_row/

Thank for your respond. I already found the solution for my problem. Here is my code:
/* save partnerhip information */
$count_director = count(array_trim($_POST['name_members']));
for($i=0; $i<$count_director; $i++) {
$directorID[] = run_num('director_id','proc_director'); // generate running number for each row
$insDirector = "INSERT INTO proc_director(director_id, vendor_cd, director_name, director_percentage)
VALUES('".$directorID[$i]."','".$vendorID."','".$_POST['name_members'][$i]."','".intval($_POST['share_percentage'][$i])."')";
$db->query($insDirector); // save the array value into DB
}

Related

PHP Query to UPDATE/DELETE a dynamically generated input field in a table

I have a Table where user can create row with adding input fields dynamically (combination with jquery). I'm successfully able to insert it into the mysql database.
If users want to edit the added already existing fields, I have an edit page where the values are fetched from the mysql DB and populated again into the dynamically creatable table.
Now there are the below probabilities:-
User only makes minor changes on the existing values. In that case
the table has to be UPDATED with the changed values
User Deletes one/multiple row(randomly selected and as per users wish). So when form submitted the php query should only DELETE that perticular row/s in the DB.
User ADDS another row to the previous existing row values, in that case the php query should UPDATE the previous values and INSERT the newly added row values.
The above sequence is not necessarilly restricted the same order. User can perform all the above three function simultaneously at the same time.
Now my problem is(only for the backend) I'm finding a hard time to frame a php & sql query so as to update to the mysql.
my php
if(isset($_POST['submit'])){
$number1 = count($_POST['item']); //
for($i=0; $i<$number1; $i++){
$item = strip_tags(trim($_POST['item'][$i]));
$description = strip_tags(trim($_POST['description'][$i]));
$unitcost = strip_tags(trim($_POST['unitcost'][$i]));
$qty = strip_tags(trim($_POST['qty'][$i])); // Quantity
$sno = strip_tags(trim($_POST['sno'][$i]));// serial number
//QUERY1 if minor updates to above variable then UPDATE (eg, qty value is changed from 3 to 4)
//QUERY2 if row is deleted then DELETE that particular row from db (eg, sno 3 deleted from the table should DELETE corresponding mysql DB values also)
//QUERY3 if row is added then that particular row values should be INSERT (eg, sno 4 is added which is not in the mysql db. So this has to be INSERTED.)
}
}
Pardon me to have asked such question. I'm wasting a whole lot of time with the above queries unable to execute properly. I only require an idea not necessarily the whole code.
Hope all of you out there would advice me some ideas on how this could be implemented. Thanks for the help in advance. Expecting a positive reply.
NB: Just to remind you again, The front end is a Dynamically ADD/DELETE Input Field table
This sounds like a frontend problem. You need to define how you tell the backend whats happening.
<input name="items[$i][name]" />
This will show up as nice array to loop through in php.
foreach($_POST[items] AS $item){
if( $item['delete'] ){
//delete code
}
}else{
//Insert/Update
}
If you want to delete something simply make the field hidden and add a flag to it.
<input type="hidden" name="items[$i][delete]" value="1" />
<input type="hidden" name="items[$i][id]" />
Thank for the reply, appreciate #ckrudelux and #codefather for their intention to help me.
Although their advise didn't help me to structure my query. So I had a long workaround and found out below solution. I'm posting the solution because I couldn't find any article online when it comes to UPDATE/DELETE a dynamically generated input table.
Hope this would be of help to someone.
So what I did basically is that I took all the values into array.
In my dynamically generated add input table code, I added an <input type="hidden" name="sno[]" value="newrow">. So this will be clubbed with the form post. I'm using the normal html post and not ajax.
now my submit.php has ben changed to below
if(isset($_POST['submit'])){
$productid = $_POST['productd'];// No striptag functions
// due to illustration purpose
// First of all, we need to fetch the querying db table.
// This is required in order to compare the existing row values
// with the posted values
$fetchproduct = $link->prepare("SELECT * FROM product WHERE productid=?");
$fetchproduct ->bind_param('s',$productid);
$fetchproduct ->execute();
$fetchresult = $fetchproduct ->get_result();
$serialnumber=array(); // Assigning array to fetch the primary key: Serial Number
while($row = $fetchresult->fetch_assoc()){
$serialnumber[] = $row["sno"];
}
//Newly Inserted Values
//$_POST['sno'] is taken from the dynamic input field defined earlier in this post.
//Basically what we are doing here is we are comparing (the values
//which have been posted from the primary page) and (values present in the db table).
//The difference will give an array of newly inserted table input field values
$insert = array_diff($_POST['sno'],$serialnumber);
//Deleted Values
// This will Difference those values in the db table and values which are
// deleted from the primary dynamic table page
$delete = array_diff($serialnumber,$_POST['sno']);
$countdelete = count($delete); // Counting how many values have been
// lined up for deleting
//Updated Values
// array_intersect will give us the common values present in both the array.
// This means that there is no deletion or insertion to the dynamic table fields.
$intersect = array_intersect($serialnumber, $_POST['sno']);
$update = array_values($intersect);
$countupdate = count($update);
//INSERT ADDED VALUES TO DB
foreach($insert as $key=>$ivalue){
// ID
if(isset($_POST['id'][$key]) && !empty($_POST['id'][$key])) {
$id = strip_tags(trim($_POST['id'][$key]));
}
// ITEM
if(isset($_POST['item'][$key]) && !empty($_POST['item'][$key])) {
$item = strip_tags(trim($_POST['item'][$key]));
}
// DESCRIPTION
if(isset($_POST['description'][$key]) && !empty($_POST['description'][$key])) {
$description = strip_tags(trim($_POST['description'][$key]));
}
// UNITCOST
if(isset($_POST['unitcost'][$key]) && !empty($_POST['unitcost'][$key])) {
$unitcost = strip_tags(trim($_POST['unitcost'][$key]));
}
// QUANTITY
if(isset($_POST['qty'][$key]) && !empty($_POST['qty'][$key])) {
$qty = strip_tags(trim($_POST['qty'][$key]));
}
// AMOUNT
if(isset($_POST['amount'][$key]) && !empty($_POST['amount'][$key])) {
$amount = strip_tags(trim($_POST['amount'][$key]));
}
// INSERT INTO THE DATABASE
$inserttable = $link->prepare("INSERT INTO product (productid, item, description, unitcost, qty, amount) VALUES(?,?,?,?,?,?)");
$inserttable->bind_param('ssssss', $id, $item, $description, $unitcost, $qty, $amount);
$inserttable->execute();
if($inserttable){
header( 'Location:to/your/redirect page.php' ) ; // NOT MANDADTORY, You can put whatever you want
$_SESSION['updatemsg'] = "Success";
}
}
//UPDATE EXISTING VALUES TO DB
for($j=0; $j<$countupdate; $j++){
// ID
if(isset($_POST['id'][$j]) && !empty($_POST['id'][$j])) {
$uid = strip_tags(trim($_POST['id'][$j]));
}
// ITEM
if(isset($_POST['item'][$j]) && !empty($_POST['item'][$j])) {
$uitem = strip_tags(trim($_POST['item'][$j]));
}
// DESCRIPTION
if(isset($_POST['description'][$j]) && !empty($_POST['description'][$j])) {
$udescription = strip_tags(trim($_POST['description'][$j]));
}
// UNITCOST
if(isset($_POST['unitcost'][$j]) && !empty($_POST['unitcost'][$j])) {
$uunitcost = strip_tags(trim($_POST['unitcost'][$j]));
}
// QUANTITY
if(isset($_POST['qty'][$j]) && !empty($_POST['qty'][$j])) {
$uqty = strip_tags(trim($_POST['qty'][$j]));
}
// AMOUNT
if(isset($_POST['amount'][$j]) && !empty($_POST['amount'][$j])) {
$uamount = strip_tags(trim($_POST['amount'][$j]));
}
// UPDATE THE DATABASE
$updatetable = $link->prepare("UPDATE product SET item=?, description=?, unitcost=?, qty=?, amount=? WHERE sno=?");
$updatetable->bind_param('ssssss', $uitem, $udescription, $uunitcost, $uqty, $uamount, $update[$j]);
$updatetable->execute();
if($updatetable){
$_SESSION['updatemsg'] = "Success";
}
}
//DELETE VALUES FROM DB
foreach($delete as $sno){
$deletetable = $link->prepare("DELETE FROM product WHERE sno=?");
$deletetable->bind_param('s', $sno);
$deletetable->execute();
if($deletetable){
$_SESSION['updatemsg'] = "Success";
}
}
}else {
$_SESSION['updatemsg'] = "Error";
}
}

Looping through multi-dimensional array to pre-populate dynamically generated form

I am trying to create an ecommerce site. I have products that have different attributes (e.g. colour) and each of these needs to have their own model number and price etc,.
I have generated a form to gather this information and save it. However, I want to be able to save this info as a $_SESSION variable while users are adding products, so that they can come back to the price section and the form will be pre-populated with what they previously entered even though they haven't actually saved the product to the DB yet.
To do this I have a string that I treat as an array of items stored as a $_SESSION variable in PHP in the following format:
'item-test,100,20,20,20,20,£,1,item-test,100,20,20,20,20,£,2'
I parse this into an actual array I can deal with like so (when it gets to actually saving the product my SQL query is inside this foreach() loop):
if(isset($_SESSION['price_array'])){
$price_array = $_SESSION['price_array'];
$result = explode("item-",$price_array);
foreach($result as $item){
if(isset($item) && $item!=""){
$itemValue = explode(",",$item);
$product_model_no = $itemValue[0];
$product_value = $itemValue[1];
$product_discount = $itemValue[2];
$product_margin = $itemValue[3];
$product_shipping_domestic = $itemValue[4];
$product_shipping_other = $itemValue[5];
$product_currency = $itemValue[6];
$product_attribute = $itemValue[7];
}
}
}
So to generate this form I've another loop that goes through all of the possible attributes (not all products might come in all colours so only the ones stored in the $_SESSION apply). If I try to pre-populate this as it is my variables above only have the values for the last item in the array.
However if I nest this inside the other loop it will get the correct data but it will generate the form a number of times depending on how many items are in the array, with each iteration of the form having the values for that item in the array.
I know this is very convoluted to try explain and I can't really provide all my code because it is very complex and most of it is generated in PHP from other information in other locations.
Edit
The basics of how the form is being generated inside the other loop is as follows:
$params = [$attribute];
$sql = "SELECT * FROM attributes WHERE id=?";
$attributeResult = DB::run($sql,$params);
foreach ($attributeResult as $value) {
for ($i = 1; $i <= 15; $i++) {
//generate form here
if($i == $product_attribute){
// pre-populate form here
}
}
}

insert array with dynamic amount of values to database

I am attempting to insert an array of image information into my database. The array consists of a ton of image information and originally i started with just allowing 3 images out of the array to be entered into the database.
The images detail goes into a table called print and each of the three image urls has a column of their own to go into, along with a caption for each image, the date the image was taken and its location. All of this info goes into one row with the users id.
// Assign images and data to appropriate variables
$image_one = $feed['0']['images']['standard_resolution']['url'];
$image_one_caption = htmlspecialchars($feed['0']['caption']['text'], ENT_QUOTES);
$image_one_date = $feed['0']['created_time'];
$image_one_location = $feed['0']['location']['name'];
$image_two = $feed['1']['images']['standard_resolution']['url'];
$image_two_caption = htmlspecialchars($feed['1']['caption']['text'], ENT_QUOTES);
$image_two_date = $feed['1']['created_time'];
$image_two_location = $feed['1']['location']['name'];
$image_three = $feed['2']['images']['standard_resolution']['url'];
$image_three_caption = htmlspecialchars($feed['2']['caption']['text'], ENT_QUOTES);
$image_three_date = $feed['2']['created_time'];
$image_three_location = $feed['2']['location']['name'];
However I now want to make it so that the number of prints entered into the table can vary from 3 up to 10 as a maximum. I'm wondering about the best way to do this.
I could simply assign the array values to more variables in the way shown above, however this is bulky and I feel unnecessarily repetitive.
I was thinking about a foreach loop, however I'm uncertain of how to insert each image and each images accompanying data into the correct columns of the users print row.
The other option would be to create a separate print_info table and store the users id and print_job number, then in the print table purely have the image info in separate rows with a print_job column to link the print_job to the users print_info row. is this the best way forward with this little problem?
What about this:
$numOfImages = 4; //the number you want
for ($i=0; $i < $numOfImages; $i++) {
$image = $feed[$i]['images']['standard_resolution']['url'];
$image_caption = htmlspecialchars($feed[$i]['caption']['text'], ENT_QUOTES);
$image_date = $feed[$i]['created_time'];
$image_location = $feed[$i]['location']['name'];
//...operation with $image
}

Passing multiple checkbox values to different columns of database

I am pretty new to PHP, but have tried searching for other questions similar to mine and been unable to find anything that is close enough to my situation to help me solve this.
I am trying to code a web page that allows users to select as many or as few items as they would like to order. The item values are identical to their Primary Key in the Item table.
Once submitted, each different item value should be input into the same row of a database table based on the date{pk}. Within that row, there are numerous columns: Item1ID, Item2ID, Item3ID, etc.
So far, the value of each item selected is assigned to a new array. However, I cannot simply input the array values into a column -- I need each array index to be placed into a sequential column. The code is below:
$date = new DateTime();
$td = $date->format('Y-m-d');
$x = 1;
$checkedItems = $_POST['Item'];
$count = count($checkedItems);
echo $count;
$foodID = "Item".$x."ID";
While($x<=$count){
if(isset($_POST['Item'])){
if (is_array($_POST['Item'])) {
foreach($_POST['Item'] as $values){
$selectedFoods = substr($values,0,4);
$addFoodOrderQuery= sprintf("UPDATE WeeklyBasketFoodOrder SET '%s' = %s WHERE `foodOrderDate` = '%s'",
$foodID, $selectedFoods, $td);
$result= mysqli_query($db, $addFoodOrderQuery);
}
}
} else {
$values = $_POST['Item'];
echo "You have not selected any items to order.";
}
$x++;
}
If you need any further clarification, please let me know. After submitting the code, the database item#ID tables are different, but they are now empty instead of "NULL."

storing array data in mysql

I am new to PHP and MySQL, please I would appreciate your help.
I have a table that generates several rows depending on the number of options selected by a user.
I want to store the content in a database table after the user presses the submit button.
Since i cannot tell how may options the user would select and how many rows will be generated, i don't know how to write the SQL code that will store it in my database table.
here is the code to generate the table
for($x=0;$x<$N; $x++) {
echo nl2br("<td><textarea name=art[] rows=10 cols=30></textarea></td><td><textarea name=science[] rows=10 cols=30></textarea></td></textarea></td><td><textarea name=method[] rows=10 cols=30></textarea></td><td><textarea name=criteria[] rows=10 cols=30></textarea></td></tr>");
}
To get the data from the table
for($i = 0; $i <$N; $i++){
$data[] = array(($art[$i]), ($science[$i]), ($method[$i]), ($criteria[$i]));
}
I would normalize this design and have a USER and OPTION table with a one-to-many relationship between them. You have as many rows as you do selections that way. You can query later to see how many USER rows chose OPTION X.
You can merge (implode) all values and store into table. split ( explode ) the values while retrive.
Store :
$optionsVal = implode(",", $data);
Retrieve :
$data = explode(",", $optionsVal);

Categories