Link to home
Start Free TrialLog in
Avatar of Member_2_8007715
Member_2_8007715Flag for Côte d'Ivoire

asked on

PHP MySQL UPDATE array

Hello,

Sorry for my english, I have a problem with my code :
$Fr_Valider = "1";
$Fr_Pseudo = $_SESSION["Connexion"]["user_log"]['Pseudo'];
$array_lib = !empty($_POST['Fr_Libelle']) ? $_POST['Fr_Libelle'] : NULL;
$array_qte = !empty($_POST['Fr_Quantite']) ? $_POST['Fr_Quantite'] : 0;
$array_unt = !empty($_POST['Fr_PrixUnit']) ? $_POST['Fr_PrixUnit'] : 0;
$array_tot = !empty($_POST['Fr_PrixTotal']) ? $_POST['Fr_PrixTotal'] : 0;
$FR_PDO = $pdo->prepare("UPDATE tb_user_voucher_test SET Libelle=:array_lib, Quantite=:array_qte, PrixUnit=:array_unt, PrixTotal=:array_tot, Valider=:Fr_Valider WHERE Pseudo = '" . $Fr_Pseudo . "' ");

foreach($array_lib as $key => $value)
	{
	if ($array_qte[$key] <> 0)
		{
		$FR_PDO->bindParam(":array_lib", $value);
		$FR_PDO->bindParam(":array_qte", $array_qte[$key]);
		$FR_PDO->bindParam(":array_unt", $array_unt[$key]);
		$FR_PDO->bindParam(":array_tot", $array_tot[$key]);
		$FR_PDO->bindParam(":Fr_Valider", $Fr_Valider);
		$FR_PDO->execute();
		}
	}

Open in new window

According to my tests, the value of the last record and update all the rest of that value... Normal, because on $Fr_Pseudo... I tried with Id but I can not.

Can you help me please ?

Cordially.
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Thank you for the effort you are making regarding having to post in English. Could you maybe explain your question a bit more - it is not clear what the problem is.

I am not sure what it is you are asking here
the value of the last record and update all the rest of that value... Normal, because on $Fr_Pseudo... I tried with Id but I can not.
Avatar of Member_2_8007715

ASKER

Thank you for interest in my problem.

This is my table :
User generated image
This is my datas :
User generated image
And this is my Update Code :
try
	{
$FR_PDO = $pdo->prepare("UPDATE tb_user_voucher_test SET NumId=:array_Id, Libelle=:array_lib, Quantite=:array_qte, PrixUnit=:array_unt, PrixTotal=:array_tot, Valider=:Fr_Valider WHERE Pseudo = '".$Fr_Pseudo."' ");
foreach($array_lib as $key => $value){if($array_qte[$key] <> 0) {
$FR_PDO->bindParam(":array_lib", 	$value);
$FR_PDO->bindParam(":array_Id", 	$array_Id[$key]);
$FR_PDO->bindParam(":array_qte", 	$array_qte[$key]);
$FR_PDO->bindParam(":array_unt", 	$array_unt[$key]);
$FR_PDO->bindParam(":array_tot", 	$array_tot[$key]);
$FR_PDO->bindParam(":Fr_Valider", 	$Fr_Valider);
$FR_PDO->execute();
}}

}
catch(PDOException $e)
	{
	echo "Erreur: " . $e->getMessage();
	}

Open in new window


And this my interface website :
User generated image
I have this error :
Erreur: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1' for key 'PRIMARY'

Open in new window


How can I update the Quantite, PrixTotal and Valider considering that the edited product is linked to the Pseudo ?
I guess using NumId can fix the problem, but I'm stuck.
I suspect your problem is here in your UPDATE
SET NumId=:array_Id

Open in new window


Your NumId should be an Autonumber - it is set by the DB and there is no need to included it in the SET part of an update.
Thank you for your answer.

I removed the Id and I launched an upload:
He recovered the last recording and applied it to the entire loop.

try
	{
$FR_PDO = $pdo->prepare("UPDATE tb_user_voucher_test SET Libelle=:array_lib, Quantite=:array_qte, PrixUnit=:array_unt, PrixTotal=:array_tot, Valider=:Fr_Valider WHERE Pseudo = '".$Fr_Pseudo."' ");
foreach($array_lib as $key => $value){if($array_qte[$key] <> 0) {
$FR_PDO->bindParam(":array_lib", 	$value);
$FR_PDO->bindParam(":array_qte", 	$array_qte[$key]);
$FR_PDO->bindParam(":array_unt", 	$array_unt[$key]);
$FR_PDO->bindParam(":array_tot", 	$array_tot[$key]);
$FR_PDO->bindParam(":Fr_Valider", 	$Fr_Valider);
$FR_PDO->execute();
}}

}
catch(PDOException $e)
	{
	echo "Erreur: " . $e->getMessage();
	}

Open in new window


Do you think the worry comes from Pseudo? or else if I have to tell him to update this or that Id, how should I proceed?
For example, I initially:
2 |  500 |   800
2 | 1500 |  2400
3 | 6500 | 15600
3 | 1000 |  2400

Open in new window


I want to change in:
1 |  500 |   400
2 | 1500 |  2400
3 | 6500 | 15600
4 | 1000 |  3200

Open in new window


After the upload I have:
4 | 1000 | 3200
4 | 1000 | 3200
4 | 1000 | 3200
4 | 1000 | 3200

Open in new window

Ok I don't think I am understanding your data so lets go step by step.
What is this?
2 |  500 |   800
2 | 1500 |  2400
3 | 6500 | 15600
3 | 1000 |  2400

Open in new window

I am assuming that the first column is your Numid - if so I am confused as to why it is not unique - or is that what you are trying to do - update it to be unique?

Also the results you posted seem to suggest you are running a LOOP - but you have not posted any code showing a loop.
This is all my code :
<?php
$Fr_Pseudo = $_SESSION["Connexion"]["user_log"]['Pseudo'];
$Fr_Date = strftime("%Y-%m-%d");
$Fr_Valider = "1";
$Fr_NumId = !empty($_POST['Fr_NumId']) ? $_POST['Fr_NumId'] : NULL;
$array_lib = !empty($_POST['Fr_Libelle']) ? $_POST['Fr_Libelle'] : NULL;
$array_qte = !empty($_POST['Fr_Quantite']) ? $_POST['Fr_Quantite'] : 0;
$array_unt = !empty($_POST['Fr_PrixUnit']) ? $_POST['Fr_PrixUnit'] : 0;
$array_tot = !empty($_POST['Fr_PrixTotal']) ? $_POST['Fr_PrixTotal'] : 0;
try
	{
	$FR_PDO = $pdo->prepare("UPDATE tb_user_voucher_test SET Libelle=:array_lib, Quantite=:array_qte, PrixUnit=:array_unt, PrixTotal=:array_tot, Valider=:Fr_Valider WHERE Pseudo = '" . $Fr_Pseudo . "' ");
	foreach($array_lib as $key => $value)
		{
		if ($array_qte[$key] <> 0)
			{
			$FR_PDO->bindParam(":array_lib", $value);
			$FR_PDO->bindParam(":array_qte", $array_qte[$key]);
			$FR_PDO->bindParam(":array_unt", $array_unt[$key]);
			$FR_PDO->bindParam(":array_tot", $array_tot[$key]);
			$FR_PDO->bindParam(":Fr_Valider", $Fr_Valider);
			$FR_PDO->execute();
			}
		}
	}

catch(PDOException $e)
	{
	echo "Erreur: " . $e->getMessage();
	}

Open in new window

Quantite | PrixUnit | PrixTotal
   2     |      500 |       800
   2     |     1500 |      2400
   3     |     6500 |     15600
   3     |     1000 |      2400

Open in new window

I put a lot of details in my post above hoping that it can help you understand my problem (sorry).
The user can only edit the quantity, the Total Price is calculated automatically and updated at the same time.
I think my problem comes from the WHERE condition.
I have to put the NumId instead of Pseudo ... only it tells me that it is a table ... and here I block :
$FR_PDO = $pdo->prepare("UPDATE tb_user_voucher_test SET Libelle=:array_lib, Quantite=:array_qte, PrixUnit=:array_unt, PrixTotal=:array_tot, Valider=:Fr_Valider WHERE NumId = '" . $Fr_NumId . "' ");

Open in new window

You still have not explained what it is you are trying to do - for instance how you get from this
2 |  500 |   800
2 | 1500 |  2400
3 | 6500 | 15600
3 | 1000 |  2400

Open in new window

To This
1 |  500 |   400
2 | 1500 |  2400
3 | 6500 | 15600
4 | 1000 |  3200

Open in new window


Here is what you have not told me
a) What this code is supposed to be doing - is it
  i) Supposed to be updating ONE record
  ii) Supposed to be updating multiple records
  iii) What is the data that is being sent
  iv) What function is driving this form

For instance I can see from your query and your data that this
UPDATE tb_user_voucher_test 
	SET 
		Libelle=:array_lib, 
		Quantite=:array_qte, 
		PrixUnit=:array_unt, 
		PrixTotal=:array_tot, 
		Valider=:Fr_Valider 
	WHERE 
	Pseudo = '{$Fr_Pseudo}'
QUERY;

Open in new window

The above query (based on your sample data) and your WHERE clause (Psuedo = 'NAME') is going to target
5 records in the case of John Smith
1 in the case of Emm Granger

I suspect based on your screen grab you are wanting to update an item in a cart in which you would need to be updating the item based on its unique ID.

But you have not given me enough information to help you more. For instance - when is your code called?

a) Is it called for each item in the cart when that item changes
OR
b) Is it called once after all items in the card have been updated

The first allows for updating of one record at a time
The second updates potentially more than one record at a time.

Let me help get you started by rewriting your question as I understand it and you can fill in the blanks

I am implementing a shopping cart.
In a cart there can be one more items each on their on line.
Each item has its own quantity box
When the user updates a quantity for a specific item my page makes a call to the server to update the cart stored in the database for that user.
I send through the user name, the quantity, the price per unit and the total (note this is not the right way of doing this but we can discuss - only the cart item id and quantity should be sent)

My code is supposed to update only the changed item - but it is changing all items in the cart.

Here is my code

That is what I am needing to see - a description of your process and then you can explain what the problem is.

From what I can tell your query is using Pseudo instead of Numid.
only it tells me that it is a table ... and here I block :
I don't know what you mean when you say "it tells me it is a table"
I am really sorry if I can not make myself understood ... certainly the language.
I start from the beginning.

When I am connected to my user area of my website, I go to a product sheet where I can choose the quantity to order.
Quantite | PrixUnit | PrixTotal

Open in new window

It is saved in my table after validation as follows:
NumId | Pseudo | Libelle | Quantite | PrixUnit | PrixTotal | Valider

Open in new window


Then comes the moment after adding all the desired products in my basket, to validate this one.
So I go to the final basket and there I can edit my basket by deleting or changing my quantities.
Quantite | PrixTotal

Open in new window


Once the quantity has been modified, I confirm the basket and the value 1 is added to Valid while taking into account the published quantities as well as the Total Prices.

In simplified example:

I add to my cart the first wording:
NumId | Pseudo    | Libelle         | Quantite | PrixUnit | PrixTotal | Valider
75    | JohnSmith | Eau de toilette |     3    |     6500 |     15600 |    NULL

Open in new window


Then I want to change the quantity and then when I confirm:
NumId | Pseudo    | Libelle         | Quantite | PrixUnit | PrixTotal | Valider
75    | JohnSmith | Eau de toilette |     5    |     6500 |     26000 |    1

Open in new window


My concern was that I was editing Pseudo and so it was changing all the lines of the nickname ... but I should rather edit by NumId (which is my unique key) ... and here I encounter difficulties.
and here I encounter difficulties.
What difficulties?

When you update your cart you should be passing through only the ID of the cart item and the Quantity. The price per unit and total you should calculate on the server.
Why?
Because someone can easily change those values before they are sent to the server - you should not trust the data coming from the client.
You can implement that fix another time for now you want to do this

UPDATE tb_user_voucher_test 
	SET 
		Libelle=:array_lib, 
		Quantite=:array_qte, 
		PrixUnit=:array_unt, 
		PrixTotal=:array_tot, 
		Valider=:Fr_Valider 
	WHERE 
	NumId = '{$Fr_NumId}'

Open in new window


The above should work if it does not then there is something else you have not mentioned.

One thing that I am not clear about - your variables all start $array (Example: $array_lib) - why is that?
Initially I made a mistake by adding NumId [] to my html
Something that I corrected and that does not send me any more error.
Now it's the last record that does not update the others.

If I have 4 lines to update, it is only the 4th that is updated.
Please show my your cart HTML code - not the code that generates it - View Source in the browser - cut and paste the cart FORM and paste it here.
you speak well of the source code of the browser is not it ?
<tbody>


<tr>
<td width="50"><img src="../images/produits/DFSOH15N" width="50px"></td>
<td>
<input type="text" name="Fr_Libelle[]" id="Fr_Libelle_0" style="width:100%; text-align:left; margin-bottom: 0;" readonly value="Crème défrisante SOFT-HAIR noir - Pot de 150ml" >
<input type="text" name="Fr_NumId" id="Fr_NumId" style="display: none" value="1" >

	</td>
<td width="30">
<a href="#" class="btn btn-danger btn-primary LienBlanc" onclick="javascript: if(confirm('Êtes-vous sûr de vouloir supprimer cet article ?')) document.location='delete_cart.php?action=delete&NumId=1';" title="Supprimer"><span class="icon-purge icon-white"></span></a>
</td>
<td width="100">
<input type="number" name="Fr_Quantite[0]" onClick="this.select();" onKeypress="return valid_Numeric(event);" id="Fr_Quantite_0" style="width:100%;margin-bottom: 0;" max="99" min="1" value="2" oninput="calcul('0')">
	</td>
<td width="100">
	<input type="text" name="Fr_PrixUnit[0]" id="Fr_PrixUnit_0" style="width:100%; text-align:right;margin-bottom: 0;" readonly value="500" oninput="calcul('0')">
	</td>
<td width="100">
	<input type="text" name="Fr_PrixTotal[0]" id="Fr_PrixTotal_0" style="width:100%; text-align:right;margin-bottom: 0;" readonly value="800">
	</td>


<tr>
<td width="50"><img src="../images/produits/DO9AVI20A" width="50px"></td>
<td>
<input type="text" name="Fr_Libelle[]" id="Fr_Libelle_1" style="width:100%; text-align:left; margin-bottom: 0;" readonly value="Déodorant AVIATOR Authentic - Flacon spray de 200ml" >
<input type="text" name="Fr_NumId" id="Fr_NumId" style="display: none" value="3" >

	</td>
<td width="30">
<a href="#" class="btn btn-danger btn-primary LienBlanc" onclick="javascript: if(confirm('Êtes-vous sûr de vouloir supprimer cet article ?')) document.location='delete_cart.php?action=delete&NumId=3';" title="Supprimer"><span class="icon-purge icon-white"></span></a>
</td>
<td width="100">
<input type="number" name="Fr_Quantite[1]" onClick="this.select();" onKeypress="return valid_Numeric(event);" id="Fr_Quantite_1" style="width:100%;margin-bottom: 0;" max="99" min="1" value="3" oninput="calcul('1')">
	</td>
<td width="100">
	<input type="text" name="Fr_PrixUnit[1]" id="Fr_PrixUnit_1" style="width:100%; text-align:right;margin-bottom: 0;" readonly value="1500" oninput="calcul('1')">
	</td>
<td width="100">
	<input type="text" name="Fr_PrixTotal[1]" id="Fr_PrixTotal_1" style="width:100%; text-align:right;margin-bottom: 0;" readonly value="3600">
	</td>


<tr>
<td width="50"><img src="../images/produits/PA9AVI10C" width="50px"></td>
<td>
<input type="text" name="Fr_Libelle[]" id="Fr_Libelle_2" style="width:100%; text-align:left; margin-bottom: 0;" readonly value="Eau de Toilette AVIATOR Code - Vaporisateur de 100ml" >
<input type="text" name="Fr_NumId" id="Fr_NumId" style="display: none" value="5" >

	</td>
<td width="30">
<a href="#" class="btn btn-danger btn-primary LienBlanc" onclick="javascript: if(confirm('Êtes-vous sûr de vouloir supprimer cet article ?')) document.location='delete_cart.php?action=delete&NumId=5';" title="Supprimer"><span class="icon-purge icon-white"></span></a>
</td>
<td width="100">
<input type="number" name="Fr_Quantite[2]" onClick="this.select();" onKeypress="return valid_Numeric(event);" id="Fr_Quantite_2" style="width:100%;margin-bottom: 0;" max="99" min="1" value="4" oninput="calcul('2')">
	</td>
<td width="100">
	<input type="text" name="Fr_PrixUnit[2]" id="Fr_PrixUnit_2" style="width:100%; text-align:right;margin-bottom: 0;" readonly value="6500" oninput="calcul('2')">
	</td>
<td width="100">
	<input type="text" name="Fr_PrixTotal[2]" id="Fr_PrixTotal_2" style="width:100%; text-align:right;margin-bottom: 0;" readonly value="20800">
	</td>


<tr>
<td width="50"><img src="../images/produits/CHSIVR15" width="50px"></td>
<td>
<input type="text" name="Fr_Libelle[]" id="Fr_Libelle_3" style="width:100%; text-align:left; margin-bottom: 0;" readonly value="Crème de rasage SIVODERM - Tube de 150g" >
<input type="text" name="Fr_NumId" id="Fr_NumId" style="display: none" value="7" >

	</td>
<td width="30">
<a href="#" class="btn btn-danger btn-primary LienBlanc" onclick="javascript: if(confirm('Êtes-vous sûr de vouloir supprimer cet article ?')) document.location='delete_cart.php?action=delete&NumId=7';" title="Supprimer"><span class="icon-purge icon-white"></span></a>
</td>
<td width="100">
<input type="number" name="Fr_Quantite[3]" onClick="this.select();" onKeypress="return valid_Numeric(event);" id="Fr_Quantite_3" style="width:100%;margin-bottom: 0;" max="99" min="1" value="5" oninput="calcul('3')">
	</td>
<td width="100">
	<input type="text" name="Fr_PrixUnit[3]" id="Fr_PrixUnit_3" style="width:100%; text-align:right;margin-bottom: 0;" readonly value="1000" oninput="calcul('3')">
	</td>
<td width="100">
	<input type="text" name="Fr_PrixTotal[3]" id="Fr_PrixTotal_3" style="width:100%; text-align:right;margin-bottom: 0;" readonly value="4000">
	</td>
</tr>
</tbody>

Open in new window

Ok this is what I was referring to earlier by not giving the full picture. This was a CRUCIAL bit of information you did not provide which would have made it much easier to determine what the problem was.
<input type="number" name="Fr_Quantite[1]" onClick="this.select();"

Open in new window

Your form is posting an array to the server - which completely changes how you handle the request on the server side.

You will need to iterate over the Fr_Quantite values and do an UPDATE for EACH ONE YOU FIND.

BUT you have another problem because you are using identical names for NumId (and duplicate ID's which is not allowed)
<input type="text" name="Fr_NumId" id="Fr_NumId" style="display: none" value="1" >
<input type="text" name="Fr_NumId" id="Fr_NumId" style="display: none" value="2" >
<input type="text" name="Fr_NumId" id="Fr_NumId" style="display: none" value="3" >

Open in new window

You need to make these arrays as well i.e.
<input type="text" name="Fr_NumId[3]" id="Fr_NumId" style="display: none" value="3" >

Open in new window


On the server you then have to iterate over the arrays and create an UPDATE query for each set of values in each case using the NumId for those values.

I am pressed for time at the moment but try the above and post back here if you get stuck.
So if I understand correctly:
<input type="text"     name="Fr_Libelle[0]"    id="Fr_Libelle_0"     value="Crème défrisante SOFT-HAIR noir" >
<input type="text"     name="Fr_NumId[0]"      id="Fr_NumId_0"       value="1" >
<input type="number"   name="Fr_Quantite[0]"   id="Fr_Quantite_0"    value="2">
<input type="text"     name="Fr_PrixUnit[0]"   id="Fr_PrixUnit_0"    value="500">
<input type="text"     name="Fr_PrixTotal[0]"  id="Fr_PrixTotal_0"   value="800">

<input type="text"     name="Fr_Libelle[1]"    id="Fr_Libelle_1"     value="Déodorant AVIATOR Authentic" >
<input type="text"     name="Fr_NumId[1]"      id="Fr_NumId_1"       value="3" >
<input type="number"   name="Fr_Quantite[1]"   id="Fr_Quantite_1"    value="3">
<input type="text"     name="Fr_PrixUnit[1]"   id="Fr_PrixUnit_1"    value="1500">
<input type="text"     name="Fr_PrixTotal[1]"  id="Fr_PrixTotal_1"   value="3600">

<input type="text"     name="Fr_Libelle[2]"    id="Fr_Libelle_2"     value="Eau de Toilette AVIATOR Code" >
<input type="text"     name="Fr_NumId[2]"      id="Fr_NumId_2"       value="5" >
<input type="number"   name="Fr_Quantite[2]"   id="Fr_Quantite_2"    value="4">
<input type="text"     name="Fr_PrixUnit[2]"   id="Fr_PrixUnit_2"    value="6500">
<input type="text"     name="Fr_PrixTotal[2]"  id="Fr_PrixTotal_2"   value="20800">

<input type="text"     name="Fr_Libelle[3]"    id="Fr_Libelle_3"     value="Crème de rasage SIVODERM" >
<input type="text"     name="Fr_NumId[3]"      id="Fr_NumId_3"       value="7" >
<input type="number"   name="Fr_Quantite[3]"   id="Fr_Quantite_3"    value="5">
<input type="text"     name="Fr_PrixUnit[3]"   id="Fr_PrixUnit_3"    value="1000">
<input type="text"     name="Fr_PrixTotal[3]"  id="Fr_PrixTotal_3"   value="4000">

Open in new window

would be the right way to handle the situation ?

Now, I have to make a loop that will edit my table based on the value of my Numid? ...
But this is my problem, make an UPDATE of each NumId :s
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I finally found the solution by talking with you a long time and your last code inspired me enough to finally say houra!
I really thank you so much for your help!
$Fr_Pseudo  = $_SESSION["Connexion"]["user_log"]['Pseudo'];
$Fr_Date    = strftime("%Y-%m-%d");
$Fr_Valider = "1";
$Fr_NumId   = !empty($_POST['Fr_NumId']) ? $_POST['Fr_NumId'] : array();
$array_lib  = !empty($_POST['Fr_Libelle']) ? $_POST['Fr_Libelle'] : array();
$array_qte  = !empty($_POST['Fr_Quantite']) ? $_POST['Fr_Quantite'] : array();
$array_tot  = !empty($_POST['Fr_PrixTotal']) ? $_POST['Fr_PrixTotal'] : array();
try{
  $FR_PDO = $pdo->prepare("UPDATE tb_user_voucher_test SET Libelle=:array_lib, Quantite=:array_qte, PrixTotal=:array_tot, Valider=:Fr_Valider WHERE NumId =:Fr_NumId");
  foreach($array_lib as $key => $value){
    if($array_qte[$key] <> 0){
    $FR_PDO->bindParam(":array_lib", $value);
    $FR_PDO->bindParam(":array_qte", $array_qte[$key]);
    $FR_PDO->bindParam(":array_tot", $array_tot[$key]);
    $FR_PDO->bindParam(":Fr_Valider", $Fr_Valider);
    $FR_PDO->bindParam(":Fr_NumId", $Fr_NumId[$key]);
    $FR_PDO->execute();
    }
  }
}catch(PDOException $e){
  echo "Erreur: " . $e->getMessage();
}

Open in new window

I finally found the solution by talking with you a long time and your last code inspired me enough to finally say houra!
I really thank you so much for your help!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial