4 Replies - 278 Views - Last Post: 20 April 2013 - 12:11 AM Rate Topic: -----

#1 rooster_bob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 21-March 13

PDO Help

Posted 18 April 2013 - 12:23 PM

I'm trying to do something that should be simple.

I have an online shopping store application in which I am tryin' to allow certain things to happen. I want the user to be able to enter the quantity into a box and have it update accordingly. If the user enters a 0, the item will be deleted.

I have two SQL tables: one for the shopping_cart and one for the stuffed_animals.
<code>
foreach ($my_array as $the_key => $the_val) {
  
     if ($the_val < = 0) {
		$my_query = "delete from shopping_cart where my_cookie=? and my_id=id";
		$statement = $db->prepare($my_query);
		$statement->execute(array($my_cookie));
     }
     else {
  		$my_query = 'update shopping_cart set quantity_ordered=? where my_cookie=? and my_id=id';      
  		$statement = $db->prepare($my_query);
		$statement->execute(array($quantity_ordered, $my_cookie));
     }
  }
}
</code>

My shopping_cart table has the my_item and my_cookie fields as well as the quantity+ordered fields and the id that my_id is set equal to is from my stuffed_animals table in which the ids equal ('1', '2', '3', and so on)

If you can help I'd greatly appreciate it!

Thank y'all

This post has been edited by modi123_1: 18 April 2013 - 12:25 PM
Reason for edit:: highlight the text and just click the 'code' button in the text editor


Is This A Good Question/Topic? 0
  • +

Replies To: PDO Help

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2836
  • View blog
  • Posts: 9,741
  • Joined: 08-August 08

Re: PDO Help

Posted 18 April 2013 - 01:56 PM

You didn't say what isn't working, so I'll guess that it isn't deleting when you want it to since my_id=id will never be true. You probably want to use my_id= ? and add the parameter to your execute array.

This post has been edited by CTphpnwb: 18 April 2013 - 01:58 PM

Was This Post Helpful? 1
  • +
  • -

#3 rooster_bob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 21-March 13

Re: PDO Help

Posted 18 April 2013 - 02:10 PM

View PostCTphpnwb, on 18 April 2013 - 01:56 PM, said:

You didn't say what isn't working, so I'll guess that it isn't deleting when you want it to since my_id=id will never be true. You probably want to use my_id= ? and add the parameter to your execute array.


Thank ya! Yes, it will not delete with the code posted above. I've made the following changes:

 
$sth = $db->prepare("delete from shopping_cart where my_cookie=? and my_id=?");
$sth->execute(array($my_cookie, $my_id));





And this code also does not work. Any suggestions?
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2836
  • View blog
  • Posts: 9,741
  • Joined: 08-August 08

Re: PDO Help

Posted 18 April 2013 - 02:16 PM

What are the values of $my_cookie and $my_id? More importantly, are they together in the database?
Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3402
  • View blog
  • Posts: 9,621
  • Joined: 08-June 10

Re: PDO Help

Posted 20 April 2013 - 12:11 AM

it might be something with the data types. if any of my_id or my_cookie is not a string (type-wise), you should rather use the bindValue() method than passing the data into the execute() method (that reliably works only for strings).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1