How to use implode

I can’t get my implode line to work and when i use, i don’t get any errors but it isn’t inserting into the database… i read somewhere that you can’t use implode with a foreach loop from stackflow… just wondering if this could be my problem

Also… if i don’t implode it, i can insert two rows instead of one… i guess that if using foreach loop, it will loop through one by one and doesn’t make sense to use it with implode…

You have been on here enough to know you need to post your code attempt to get proper help. We cannot see your computer screen.

2 Likes

You know what’s an awesome resource for PHP? Not YouTube, not stackoverflow, certainly not mmtuts, but the PHP manual!

See, it’s got a whole page on just implode, with examples and everything :wink:
https://secure.php.net/manual/en/function.implode.php

3 Likes

Thanks but i know how to use implode… just wondering if i can or can’t use it with a foreach loop?

Sure you can

1 Like

My apologies. . I was super excited and not on my pc just then… cheers

It would help to know exactly what it is you are attempting to do with implode.

I managed to get the implode function to work for a few minutes or so and was over the moon about it but it just suddenly stop inserting information into the memberships table… Here is my code:

<?php
   
   if (!isset($_POST['submit'])) {
      header("Location: ../signup.php");
      exit();
   } else {
   	    
   	   include_once 'dbh.php';

   	   // information from users table

   	   $first = mysqli_real_escape_string($conn, $_POST['first']);
   	   $last = mysqli_real_escape_string($conn, $_POST['last']);
   	   $email = mysqli_real_escape_string($conn, $_POST['email']) ;
   	   $uid = mysqli_real_escape_string($conn, $_POST['uid']);
   	   $password = mysqli_real_escape_string($conn, $_POST['pwd']);
   	   $user_permission = mysqli_real_escape_string($conn, $user_permission = 'Standard User');
   	   $freelesson = mysqli_real_escape_string($conn, $_POST['freelesson']);
   	   $datejoined =  mysqli_real_escape_string($conn, $datejoined = date('Y-m-d H:i:s', strtotime('+1 day')));
   	   $user_activate = mysqli_real_escape_string($conn, $user_activate = 0);
   	   $premium = mysqli_real_escape_string($conn, $premium = 0);



      // information from memberships table

        
   	   $subscriptionplan = $_POST['subscriptionplan'];
   	   $subscriptionplan = array_map('mysqli_real_escape_string', $subscriptionplan);
   	  
   	   $subscriptionplandate =  mysqli_real_escape_string($conn, $subscriptionplandate = date('Y-m-d H:i:s', strtotime('+1 day')));
   	   $subscriptionplandate2 =  mysqli_real_escape_string($conn, $subscriptionplandate2 = date('Y-m-d H:i:s', strtotime('+1 day')));
   	   $subscriptionplandate3 =  mysqli_real_escape_string($conn, $subscriptionplandate3 = date('Y-m-d H:i:s', strtotime('+1 day')));
   	   $fees = mysqli_real_escape_string($conn, $fees = 0);
       $fees2 = mysqli_real_escape_string($conn, $fees2 = 0);
       $fees3 = mysqli_real_escape_string($conn, $fees3 = 0);
       $totalfees = mysqli_real_escape_string($conn, $totalfees = 0);
       $paid = mysqli_real_escape_string($conn, $paid = 0);
       $paid2 = mysqli_real_escape_string($conn, $paid2 = 0);
       $paid3 = mysqli_real_escape_string($conn, $paid3 = 0);
       $expirydate = mysqli_real_escape_string($conn, $expirydate = date('Y-m-d H:i:s', strtotime('+1 day')));
       $expirydate2 = mysqli_real_escape_string($conn, $expirydate2 = date('Y-m-d H:i:s', strtotime('+1 day')));
       $expirydate3 = mysqli_real_escape_string($conn, $expirydate3 = date('Y-m-d H:i:s', strtotime('+1 day')));
       $paidbydate = mysqli_real_escape_string($conn, $paidbydate = date('Y-m-d H:i:s', strtotime('+1 day')));
       $paidbydate2 = mysqli_real_escape_string($conn, $paidbydate2 = date('Y-m-d H:i:s', strtotime('+1 day')));
       $paidbydate3 = mysqli_real_escape_string($conn, $paidbydate3 = date('Y-m-d H:i:s', strtotime('+1 day')));
       $overdue = mysqli_real_escape_string($conn, $overdue = 0);
       $overdue2 = mysqli_real_escape_string($conn, $overdue2 = 0);
       $overdue3 = mysqli_real_escape_string($conn, $overdue2 = 0);
   	   
   	  // $fees = mysqli_real_escape_string($conn, $_POST['fees']);
   	  // $totalfees = mysqli_real_escape_string($conn, $_POST['totalfees']);
   	   
      
       
       $activate = mysqli_real_escape_string($conn, $activate = 0);
       
      
   	   //Error handlers...

   	   if (empty($first) || empty($last) || empty($email) || empty($uid)|| empty($password)) {
   	   	   header("Location: ../signup.php?signup=empty");
   	   	   exit();
   	   } else {
          //Check if input characters are valid
   	   	    if (!preg_match("/^[a-zA-Z]*$/", $first) || !preg_match("/^[a-zA-Z]*$/", $last)) {
                header("Location: ../signup.php?signup=invalid");
                exit();
   	   	    } else {
   	   	    	
   	   	    	//Checking for valid emails
                 if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                     header("Location: ../signup.php?signup=email");
                     exit();
                 } else {
                   if (Strlen($password) < 5) {
                   header("Location: ../signup.php?signup=invalidlength");
                   exit();
                   } else {
                      if (!preg_match('/^(?=.*\d)(?=.*[A-Za-z])[0-9A-Za-z!@#$%]{8,20}$/', $password)) {
                       header ("Location: ../signup.php?signup=notalphanumeric");
                       exit();
                      } else {
                      	
                      		

                      		

                           
                      $sql = "SELECT * FROM users WHERE user_uid = $uid'";
                      $result = mysqli_query($conn, $sql);
                      $resultCheck = mysqli_num_rows($result);

                      if($resultCheck > 0) {
                         header("Location: ../signup.php?signup=usertaken");
                         exit();
                      } else {

                    
  								
                                           
  										
   							            
                            



           // Select subscription 

          


                                     
       

                         // Supply a random generated token for email activation
                        

                      	$token = 'qqewreqreqwsdfdfdafcbvcQERFGHFGHGFHRETERTDF!@#$%^^()';
                      	$token = str_shuffle($token);
                      	$token = substr($token, 0, 10);

                      	$token2 = 'qqewreqreqwsdfdfdafcbvcQERFGHFGHGFHRETERTDF!@#$%^^()';
                        $token2 = str_shuffle($token);
                      	$token2 = substr($token, 0, 10);
                        $paid = 0;
                      	
                      	$activate = 0;

                      	//Hashing the password
                      	$hashedPwd = password_hash($password, PASSWORD_DEFAULT);
                      	//Insert the user into the database
                      	$sql ="INSERT INTO users (user_first, user_last, user_email, user_uid, user_password, user_permission, freelesson, datejoined, user_token, user_activate, premium) VALUES (?,?,?,?,?,?,?,?,?,?,?);";

                      	$stmt = mysqli_stmt_init($conn);
                      	if(!mysqli_stmt_prepare($stmt, $sql)) {
                      		 echo "SQL error";
                      	} else {
                      		mysqli_stmt_bind_param($stmt, "sssssssssss", $first, $last, $email, $uid, $password, $user_permission, $freelesson,$datejoined, $token, $user_activate, $premium);
                      	  mysqli_stmt_execute($stmt);
                      	}

                       // insert into user_lessonsubscription table

                       
                       
                         
                       

                          
                     

                  //    $subscriptionplan  = implode(',',$subscriptionplan);

                        
                        
                       
                  

                      $sql ="INSERT INTO memberships (user_uid, subscriptionplan, subscriptionplandate, subscriptionplandate2, subscriptionplandate3, fees, fees2, fees3, totalfees, paid, paid2, paid3, expirydate, expirydate2, expirydate3, paidbydate, paidbydate2, paidbydate3, overdue, overdue2, overdue3, token, activate) VALUES (???????????????????????);";

                      $stmt = mysqli_stmt_init($conn);
                      	if(!mysqli_stmt_prepare($stmt, $sql)) {
                      		 echo "SQL error";
                      	} else {
                      		mysqli_stmt_bind_param($stmt, "sssssssssssssssssssssss", $uid, $subscriptionplan, $subscriptionplandate, $subscriptionplandate2, $subscriptionplandate3, $fees, $fees2, $fees3, $totalfees, $paid, $paid2, $paid3, $expirydate, $expirydate2, $expirydate3, $paidbydate, $paidbydate2, $paidbydate3, $overdue, $overdue2, $overdue3, $token2, $activate);
                      	  mysqli_stmt_execute($stmt);
                      	}

                      


                        // Send an email to the user:
                        $company = "pianocourse101@hotmail.com";
                        $subject = "Activate your email account";
                        $mailTo = "piano0011@hotmail.com";
                        $headers = "From: ".$company;
                       $txt = "Thank you for registering with pianocourse101! At pianocourse101, your child can now learn how to play the piano right from the comfort of your own home! \n\nOur lessons are based from the Bastien Piano Basics series because it is both fun and educational for your child. \n\nHowever, you must activate your FREE membership account by clicking on the link below: \n\n http://localhost/loginsystem/includes/activate.php?email=".$mailTo."&activatetoken=".$token."" ;

                        mail($mailTo, $subject, $txt, $headers);
                        





                      	header("Location: ../signup.php?signup=success");
                      	exit();
                        }
                 }
             }
         }
     
   	   }
   	   }

   	   
  } 	 
   
$token = 'qqewreqreqwsdfdfdafcbvcQERFGHFGHGFHRETERTDF!@#$%^^()';
$token = str_shuffle($token);
$token = substr($token, 0, 10);

$token2 = 'qqewreqreqwsdfdfdafcbvcQERFGHFGHGFHRETERTDF!@#$%^^()';
$token2 = str_shuffle($token);  // *** IS THIS CORRECT?
$token2 = substr($token, 0, 10);

I have changed it but it still doesn’t work…

Topics merged.

??????????????????????? is incorrect, it should be ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?.

Also, now that you’re using prepared statements (yay!), you no longer need to apply mysqli_real_escape_string anymore. It’s even better when you don’t because it can cause some annoying side effects with quotes being escaped.

2 Likes

Apologies… I can’t believe that i made that last mistake because the first one was fine… but it still doesn’t work…

No problem. Happy to see you making a commitment to learn. We will all be really proud of you when you start using PDO and Prepared Statements. That is an automatic induction into the Big Boys Club with Honors.

1 Like

Thanks for the encouragement! I will get there soon!

How about today? This excellent PDO Tutorial will get you going. https://phpdelusions.net/pdo

mysqli_stmt_execute returns a boolean (true/false) that indicates whether the query was successful or not, so you can use the following construct to get any errors that happen on the screen:

$sql = /* Your query here */;
if (!mysqli_stmt_execute($conn, $sql)) {
    printf('Query "%s" failed for the following reason: %s', $sql, mysqli_error($conn));
}

For testing purposes only, you should never include such output on production websites, you should use a logger for that that logs to a file or database or whatever, like Monolog

Thanks but I am more of a visual learner, so prefer to use youtube or videos…

I have to wonder if the question shouldn’t be “how to use implode” but rather “do I want to use implode”.

I have been there and speaking from experience putting comma separated values into a single field was not the best decision. Though it allowed for a simpler database design, it created the need for much more complex code to untangle the values and was a bottleneck that could have been avoided had I used relational tables instead. I have since learned the lesson.

I’m not saying you should become a database expert, but I believe it would be in your best interest to rethink your database structure now before working up your code much further.

3 Likes

Wait a minute. I may have missed a post somewhere. OP, if you are storing multiple values in the same column then stop what you are doing right now and fix your database. Learn about “Database Normalization”.

2 Likes