Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP] Problem z kupowaniem przedmiotow w sklepie
Rafiks1992
post
Post #1





Grupa: Zarejestrowani
Postów: 28
Pomógł: 0
Dołączył: 27.06.2017

Ostrzeżenie: (0%)
-----


Siemka ponownie (IMG:style_emoticons/default/wink.gif) dotarlem do kolejnego etapu, wczoraj dodalem nowa tabele o nazwie shop w ktorej umiescilem 3 kolumny id, cena, nazwa a takze dodalem tam 4 losy do kupienia: mikrolos, minilos, megalos, hiperlos ustawiajac oczywiscie id na A.I a takze ceny tych losow i nazwy. W tabeli users dodalem takze kolumny: mikrolos, minilos, megalos, hiperlos aby miec mozliwosc wyswietlenia ile dany uzytkownik posiada tych losow. Poogarnialem wszystko i zmienilem w mysql losowe dane dla kazdego goscia aby sprawdzic czy dziala i faktycznie wyswietla ilosc losów w Twój profil. Tylko teraz mam problem ze skryptem kupowania kolejnych losów. Stworzylem nowy plik i nazwie shop.php z takim kodem:

  1. <?php
  2.  
  3. $title = "Sklep";
  4. include_once 'header.php';
  5.  
  6. if (!$session->is_logged_in()) {
  7. $session->message("Musisz sie zalogowac!", "success");
  8. redirect_to("login.php");
  9. }
  10. ?>
  11.  
  12. <h1>Sklep</h1>
  13.  
  14. <?php
  15. echo 'Witamy w sklepie, mozesz tutaj kupic losy.';
  16. ?> <br><br><br>
  17.  
  18. <?php
  19. $userdata = User::getUser($session->user_name);
  20. echo 'Masz aktualnie: ' .$userdata->money. 'zl.';
  21. ?> <br>
  22.  
  23. <?php
  24. echo 'Kup:';
  25. ?> <br>
  26.  
  27. <?php
  28.  
  29. if(isset($_POST['kup_los'])) {
  30. if(!empty($_POST['mikrolos'])) {
  31. $mikrolos = ($_POST['mikrolos']*$cost);
  32. if($money >= $mikrolos) {
  33. mysql_query("UPDATE users SET mikrolos='".$_POST['mikrolos']."' WHERE id=".$pokaz['id']);
  34. mysql_query("UPDATE users SET money=money-".$cost." WHERE id=".$pokaz['id']);
  35. } else {
  36. echo "Nie stać cie";
  37. }
  38. }
  39. }
  40.  
  41.  
  42.  
  43. ?>
  44.  
  45. <form action='' method='post' class='form'>
  46. Mikrolos: <input type='text' name='mikrolos' value=''>
  47. <button name='kup_los' type='submit'>Kup</button>
  48. </form>
  49.  
  50. <?php
  51.  
  52. include_once 'footer.php';
  53.  
  54. ?>



Zrobilem narazie tylko ten pierwszy los czyli mikrolos do kupienia, jednak gdy na stronie w formularzu wpisze chocby 1 - a stac mnie na ten los to nic sie nie dzieje, moglby mi ktos wyjasnic dlaczego?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Rafiks1992
post
Post #2





Grupa: Zarejestrowani
Postów: 28
Pomógł: 0
Dołączył: 27.06.2017

Ostrzeżenie: (0%)
-----


Jak to nie ma w php (IMG:style_emoticons/default/tongue.gif) ? masz na mysli ze wrzucam komendy np z php 4.5 czy jakiegos tam a teraz trzeba 7.1 (IMG:style_emoticons/default/biggrin.gif) ? Cholercia to bardzo niedobrze robie jesli faktycznie tak jest, co do tego user $database wrzucam plik userclass.php aby to przyblizyc poniewaz szczerze mowiac jest tutaj nawalone tyle kodu ze nie ogarnalem polowy

  1. <?php
  2.  
  3. class User {
  4.  
  5. protected static $table_name = "users";
  6. protected static $db_fields = array('id', 'username', 'password', 'email', 'status', 'actcode' ,'created_at' ,'money');
  7.  
  8. public $id;
  9. public $username;
  10. public $password;
  11. public $email;
  12. public $status = "pending";
  13. public $actcode;
  14. public $created_at;
  15. public $money;
  16.  
  17. public static function authenticate($username = "", $password = "") {
  18. global $database;
  19.  
  20. $username = $database->escape_value($username);
  21. $password = $database->escape_value($password);
  22. $password = secure_string($password);
  23.  
  24. $sql = "SELECT * FROM " . self::$table_name . " users WHERE username = '{$username}' AND password = '{$password}' LIMIT 1";
  25. $result_array = self::find_by_sql($sql);
  26. return !empty($result_array) ? array_shift($result_array) : false;
  27. }
  28.  
  29. public static function activate($username, $actcode) {
  30. global $database;
  31. global $session;
  32.  
  33. $username = $database->escape_value($username);
  34. $actcode = $database->escape_value($actcode);
  35.  
  36. $sql = "SELECT status FROM users WHERE username = '{$username}' AND actcode = '{$actcode}' AND status = 'pending' LIMIT 1";
  37. $database->query($sql);
  38. if($database->affected_rows() == 1) {
  39. $sql = "UPDATE users SET status = 'confirmed' WHERE username = '{$username}' AND actcode = '{$actcode}'";
  40. $result = $database->query($sql);
  41.  
  42. if($result) {
  43. return true;
  44. } else {
  45. return false;
  46. }} else {
  47. return false;
  48. }
  49. }
  50.  
  51. public static function getUser($username) {
  52. global $database;
  53.  
  54. $username = $database->escape_value($username);
  55.  
  56. $sql = "SELECT * FROM users WHERE username = '{$username}' LIMIT 1";
  57. $result_array = self::find_by_sql($sql);
  58. // var_dump($result_array);
  59. return !empty($result_array) ? array_shift($result_array) : false;
  60.  
  61. }
  62.  
  63. public static function user_email($username, $email) {
  64. global $database;
  65.  
  66. $username = $database->escape_value($username);
  67. $email = $database->escape_value($email);
  68.  
  69. $sql = "SELECT username FROM users WHERE username = '{$username}' AND email = '{$email}' LIMIT 1";
  70. $database->query($sql);
  71. if($database->affected_rows() == 1) {
  72. return true;
  73. } else
  74. return false;
  75. }
  76.  
  77. public static function exists($where,$username) {
  78. global $database;
  79.  
  80. $username = $database->escape_value($username);
  81. $where = $database->escape_value($where);
  82.  
  83. $sql = "SELECT '{$where}' FROM users WHERE {$where} = '{$username}' LIMIT 1";
  84. $database->query($sql);
  85. if($database->affected_rows() == 1) {
  86. return true;
  87. } else
  88. return false;
  89. }
  90.  
  91.  
  92. public static function find_all() {
  93. return self::find_by_sql("SELECT * FROM " . self::$table_name);
  94. }
  95.  
  96. public static function find_by_id($id = 0) {
  97. $result_array = self::find_by_sql("SELECT * FROM " . self::$table_name . " WHERE id={$id} LIMIT 1");
  98. return !empty($result_array) ? array_shift($result_array) : false;
  99. }
  100.  
  101. public static function find_by_sql($sql = "") {
  102. global $database;
  103. $result_set = $database->query($sql);
  104. $object_array = array();
  105. while($row = $database->fetch_array($result_set)) {
  106. $object_array[] = self::instantiate($row);
  107. }
  108. return $object_array;
  109. }
  110.  
  111. public static function count_all() {
  112. global $database;
  113. $sql = "SELECT COUNT(*) FROM " . self::$table_name;
  114. $result_set = $database->query($sql);
  115. $row = $database->fetch_array($result_set);
  116. return array_shift($row);
  117. }
  118.  
  119. private static function instantiate($record) {
  120. $object = new self;
  121. foreach($record as $attribute => $value) {
  122. if($object->has_attribute($attribute)) {
  123. $object->$attribute = $value;
  124. }
  125. }
  126. return $object;
  127. }
  128.  
  129. private function has_attribute($attribute) {
  130. return array_key_exists($attribute, $this->attributes());
  131. }
  132.  
  133. protected function attributes() {
  134. $attributes = array();
  135. foreach(self::$db_fields as $field) {
  136. if(property_exists($this, $field)) {
  137. $attributes[$field] = $this->$field;
  138. }
  139. }
  140. return $attributes;
  141. }
  142.  
  143. protected function sanitized_attributes() {
  144. global $database;
  145. $clean_attributes = array();
  146. foreach($this->attributes() as $key => $value) {
  147. $clean_attributes[$key] = $database->escape_value($value);
  148. }
  149. return $clean_attributes;
  150. }
  151.  
  152. public function create() {
  153. global $database;
  154. $attributes = $this->sanitized_attributes();
  155. $sql = "INSERT INTO " . self::$table_name . " (";
  156. $sql .= join(", ", array_keys($attributes));
  157. $sql .= ") VALUES ('";
  158. $sql .= join("', '", array_values($attributes));
  159. $sql .= "')";
  160. if($database->query($sql)) {
  161. $this->id = $database->insert_id();
  162. return true;
  163. } else {
  164. return false;
  165. }
  166. }
  167.  
  168. public function update() {
  169. global $database;
  170. $attributes = $this->sanitized_attributes();
  171. $attribute_pairs = array();
  172. foreach($attributes as $key => $value) {
  173. $attribute_pairs[] = "{$key}='{$value}'";
  174. }
  175. $sql = "UPDATE " . self::$table_name . " SET ";
  176. $sql .= join(", ", $attribute_pairs);
  177. $sql .= " WHERE id=" . $database->escape_value($this->id);
  178. $database->query($sql);
  179. return ($database->affected_rows() == 1) ? true : false;
  180. }
  181.  
  182. public function delete() {
  183. global $database;
  184. $sql = "DELETE FROM " . self::$table_name;
  185. $sql .= " WHERE id=" . $database->escape_value($this->id);
  186. $sql .= " LIMIT 1";
  187. $database->query($sql);
  188. return ($database->affected_rows() == 1) ? true : false;
  189. }
  190.  
  191. }
  192.  
  193. ?>



Z tego co tutaj czytam zmienna $database daje nam chyba mozliwosc laczenia z baza lub sprawdzenia uzytkownika bo jest wszedzie username, useremail = database
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 14.10.2025 - 21:22