Jump to content

macytan1988

Members
  • Posts

    10
  • Joined

  • Last visited

macytan1988's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Oh wait! It's working now! //START: Subscription if(isset($_POST['subscribe'])){ if(isset($_POST['subs'])){ $subscription_stmt = $db->prepare(' INSERT INTO member_feed SET member_id = :member_id, feed_id = :feed_id '); if (!empty($_POST['subs'])){ foreach ( $_POST['subs'] as $subscribed_feed) { $subscription_stmt->execute(array( 'member_id' => $member_id, 'feed_id' => $subscribed_feed )); } } echo '<div class="alert alert-success"><a class="close" data-dismiss="alert">×</a>Subscription updated!</div>'; } else { echo '<div class="alert alert-danger"><a class="close" data-dismiss="alert">×</a>You have not selected any subscription!</div>'; } } //END: Subscription However, there is one more error here. Let say if the user has already subscribed to feed 1, when he clicks it again, the error message reads Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '28-1' for key 'PRIMARY'' in C:\xampp\htdocs\memberpage.php:81 Stack trace: #0 C:\xampp\htdocs\memberpage.php(81): PDOStatement->execute(Array) #1 {main} thrown in C:\xampp\htdocs\memberpage.php on line 81 My question is, how can I set my own custom message like "You have already subscribed to this feed"? OR Should I recreate member_feed table and not set any of the fields as PRIMARY KEY to avoid this issue?
  2. Thanks for your reply mogosselin! I have edited the code but it still has a minor error. This is my latest version of the code. //START: Subscription if(isset($_POST['subscribe'])){ if(isset($_POST['subs'])){ $subscription_stmt = $db->prepare(' INSERT INTO member_feed SET member_id = :member_id, feed_id = :feed_id '); foreach ( (array)$subs as $subscribed_feed) { $subscription_stmt->execute(array( 'member_id' => $member_id, 'feed_id' => $subscribed_feed )); } echo '<div class="alert alert-success"><a class="close" data-dismiss="alert">×</a>Subscription updated!</div>'; } else { echo '<div class="alert alert-danger"><a class="close" data-dismiss="alert">×</a>You have not selected any subscription!</div>'; } } //END: Subscription When I tick a random checkbox and click the subscribe button, it shows this error: Notice: Undefined variable: subs in C:\xampp\htdocs\memberpage.php on line 76 (I highlighted line 76 in red colour as shown above) My HTML code for the checkboxes: <input type="checkbox" name="subs[]" value="1"> The Malaysian Insider</input> <input type="checkbox" name="subs[]" value="2"> The Malay Mail Online</input> <input type="checkbox" name="subs[]" value="3"> The Star</input> Besides, it inputs nothing into the database . By the way, my previous issue on the display of member_id has been resolved but thus far, I still can't get it stored into the database. Sorry for asking you so many questions in one go, but if you don't mind, can I get some hints on: 1. How should I get my checkboxes ticked according to the user's subscription, so when he/she logs into the system next time, he/she will be able to view it. 2. How and where should I code my DELETE part to allow users to remove the subscription by unticking the checkbox? Thank you so much in advance and have a nice day ahead!
  3. If I edited the code by including isset function, then the code runs without any error, but I'm still unable to get it work by inserting required data into the database. (When I click the button, it runs but inserts NOTHING into the database) The member_id issue as I mentioned in my last post still exists. //START: Subscription $member_id = $_SESSION['member_id']; $subscription_stmt = $db->prepare(' INSERT INTO member_feed SET member_id = :member_id, feed_id = :feed_id '); if(isset($_POST['invite'])){ foreach ( (array)$subs as $subscribed_feed) { $subscription_stmt->execute(array( 'member_id' => $_SESSION['member_id'], 'feed_id' => $subscribed_feed )); } } //END: Subscription
  4. To be honest, I don't know how to edit the login file (user.php) to make member_id available for display. Can I get some hints on that please? Also, I too don't know how to edit the HTML part for checkboxes in order for them to be "connected" to the database table, especially the feed_id. For now, I just entered the feed_id myself in this way: <input type="checkbox" name="subs[]" value="1" > The Malaysian Insider</input> <input type="checkbox" name="subs[]" value="2"> The Malay Mail Online</input> <input type="checkbox" name="subs[]" value="3"> The Star</input> My user.php is still the same as above, but I have made some changes, according to your hints, on the PHP code for subscription. //START: Subscription $member_id = $_SESSION['member_id']; $subscription_stmt = $db->prepare(' INSERT INTO member_feed SET member_id = :member_id, feed_id = :feed_id '); foreach ($_POST['subs'] as $subscribed_feed) { $subscription_stmt->execute(array( 'member_id' => $_SESSION['member_id'], 'feed_id' => $subscribed_feed )); } //END: Subscription However, these chunks of code give me two errors again Notice: Undefined index: subs in C:\xampp\htdocs\memberpage.php on line 76 Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\memberpage.php on line 76
  5. Ok, I tried again just now. No more error message like the one I mentioned in my previous post, but instead of inserting the correct id number, it inserts "0" into database. PHP code for user.php: <?php include('password.php'); class User extends Password{ private $_db; function __construct($db){ parent::__construct(); $this->_db = $db; } private function get_user_hash($username){ try { $stmt = $this->_db->prepare('SELECT password FROM members WHERE username = :username AND active="Yes" '); $stmt->execute(array('username' => $username)); $row = $stmt->fetch(); return $row['password']; } catch(PDOException $e) { echo '<p class="bg-danger">'.$e->getMessage().'</p>'; } } public function login($username,$password){ $hashed = $this->get_user_hash($username); if($this->password_verify($password,$hashed) == 1){ $_SESSION['loggedin'] = true; $_SESSION['username'] = $username; $_SESSION['member_id'] = $member_id; return true; } } public function logout(){ session_destroy(); } public function is_logged_in(){ if(isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true){ return true; } } } ?> PHP for Subscription part: //START: Subscription if(isset($_POST['subscribe'])){ try{ $checkbox_values = $_POST['subs']; for ($i=0; $i<sizeof($checkbox_values);$i++){ $stmt = $db->prepare("INSERT INTO member_feed(member_id, feed_id) VALUES (':member_id', '".$checkbox_values[$i]."')"); $stmt->execute(array( ':member_id' => $member_id )); } } catch(PDOException $e) { $error[] = $e->getMessage(); } echo '<div class="alert alert-success"><a class="close" data-dismiss="alert">×</a>Subscription updated!</div>'; } //END: Subscription
  6. Done with the editing, but I'm still unable to get the id display
  7. Hi. I have created three tables (below) based on your suggestion and renamed all the variables to avoid case-sensitive issues. Everything goes smooth until an error message reads "Notice: Undefined index: member_ID in C:\xampp\htdocs\memberpage.php on line 17" pops up. It seems that I'm unable to retrieve the user ID but I'm able to do so for username. Can you teach me how to edit this file to allow the display of member_ID? My user file is as follows: <?php include('password.php'); class User extends Password{ private $_db; function __construct($db){ parent::__construct(); $this->_db = $db; } private function get_user_hash($username){ try { $stmt = $this->_db->prepare('SELECT password FROM members WHERE username = :username AND active="Yes" '); $stmt->execute(array('username' => $username)); $row = $stmt->fetch(); return $row['password']; } catch(PDOException $e) { echo '<p class="bg-danger">'.$e->getMessage().'</p>'; } } public function login($username,$password){ $hashed = $this->get_user_hash($username); if($this->password_verify($password,$hashed) == 1){ $_SESSION['loggedin'] = true; $_SESSION['username'] = $username; //This addition works and it allows me to output the username. $_SESSION['member_ID'] = $member_ID; //I'm trying to add this line, but it fails to display the ID. return true; } } public function logout(){ session_destroy(); } public function is_logged_in(){ if(isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true){ return true; } } } ?>
  8. Hi, Jacques1 again. I have already edited my code accordingly based on your suggestion. The checkbox input is, thus far, successfully. Can you give me some guidance on how to generate the list of checkboxes and add the checked attrbute to the feeds which are already subscribed? Attached herewith are my php and HTML codes. PHP //START: Subscription if(isset($_POST['subscribe'])){ try{ $subsTMI = (isset($_POST['subsTMI'])) ? '1' : '0'; $subsTMO = (isset($_POST['subsTMO'])) ? '1' : '0'; $subsSTAR = (isset($_POST['subsSTAR'])) ? '1' : '0'; $stmt = $db->prepare("UPDATE members SET subsTMI = :subsTMI, subsTMO = :subsTMO, subsSTAR = :subsSTAR WHERE username = '$username' "); $stmt->execute(array( ':subsTMI' => $subsTMI, ':subsTMO' => $subsTMO, ':subsSTAR' => $subsSTAR )); } catch(PDOException $e) { $error[] = $e->getMessage(); } echo '<div class="alert alert-success"><a class="close" data-dismiss="alert">×</a>Subscription updated!</div>'; } //END: Subscription <input type="checkbox" name="subsTMI" value="1" > The Malaysian Insider</input> <input type="checkbox" name="subsTMO" value="1"> The Malay Mail Online</input> <input type="checkbox" name="subsSTAR" value="1"> The Star</input>
  9. Hi, Jacques1. Thanks for your reply! I will now separate different feeds with different columns in the database for easy configuration in the future according to your suggestion. I will be back soon.
  10. I'm currently developing a website which allows users to register and log into the system to read news articles in RSS form. A subscription page with a list of checkboxes (like CNN, BBC etc) is provided for the users to choose which news site they want to include in their customized RSS feed page. I successfully stored the checkbox values in array form into the database. For example, if the user ticks "CNN", "BBC", "ABC News", the values that will be stored in database will be: cnn, bbc, abc Since I'm a beginner in web coding, especially in PHP and MYSQL, I have two questions here. Q1. How can I retrieve the stored values and display it on the subscription page with the specific checkboxes checked based on user's subscription? For instance, if the user already subscribed to CNN, when he logs into the system next time, he should be able to see the already-ticked CNN checkbox. Q2. How can I retrieve the stored values and put it onto the customised RSS page? It's something like: if cnn is selected, then add http://rss.cnn.com/rss/edition_world.rss. Is there any tutorial on that? Last but not least, forgive me for asking so many questions in one go in this lengthy piece and thanks for all the replies in advance! =) Table structure: HTML for checkboxes: <input type="checkbox" name="subs[]" value="mal-malins"> The Malaysian Insider</option> <input type="checkbox" name="subs[]" value="mal-malaymo"> The Malay Mail Online</option> <input type="checkbox" name="subs[]" value="mal-star"> The Star</option> <input type="checkbox" name="subs[]" value="mal-nst"> New Straits Times</option> Code for storing the checkbox values: //START: Subscription if(isset($_POST['subscribe'])){ if(isset($_POST['subs'])){ try { $data= $_POST['subs']; $subs = implode(",", (array)$data); $stmt = $db->prepare("UPDATE members SET subs = :subs WHERE username = '$username' "); $stmt->execute(array( ':subs' => $subs )); //show success message echo '<div class="alert alert-success"><a class="close" data-dismiss="alert">×</a>Subscription updated!</div>'; //else catch the exception and show the error. } catch(PDOException $e) { $error[] = $e->getMessage(); } } else { echo '<div class="alert alert-danger"><a class="close" data-dismiss="alert">×</a>You have not selected any subscription!</div>'; } } //END: Subscription
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.