Implode and explode

Hi! i have a checkbox on my sign up form which can allow multiple boxes to be checked. I have successfully imploded them, but i cannot explode them to have their own row of data in the database since i will require that for query purposes. Kindly help me out to see what is wrong with my code. i have two tables linked with a foreign key

<?php

$a = $_POST['name'];
$b = $_POST['email'];
$c = $_POST['pass'];
$d = $_POST['gender'];
$e = $_POST['bio'];
$f = implode(',', $_POST['category']);

if($a && $b && $c && $d && $e && $f)
{
    
	
    
       if ( filter_var(($_POST["email"]), FILTER_VALIDATE_EMAIL)  == TRUE) {
       
    
			$con=mysqli_connect('localhost','root','');
			if(!$con)
			{
				die('Error in connection'.mysqli_error());
			}
			else 
			{
				mysqli_select_db($con,'ymp');
				
				$query = mysqli_query($con, "select * from mentor where email = '".$b."'");
				if (!$query)
				{
					die ("Failed to query database" . mysqli_error($con));
				}
						if(mysqli_num_rows($query) > 0)
						{
							echo "<div class='form'><center><h3>Email has already been used</h3><br/>Click <a href='mentorsignup.php'>here</a> to try again</center></div>";
						}
      			
     				else
        			{
					$sql = ("INSERT INTO mentor VALUES(DEFAULT,'$a','$b','$c','$d','$e')");
					$result = mysqli_query($con, $sql);
						// retrieve last id
					$mentorID = mysqli_insert_id($con);
					
					$result = array_map(function($val) {
   					 return explode(',', $val);
						}, $f);
						// insertion to category table
					$sql = ("INSERT INTO category VALUES (DEFAULT, '$f', '$mentorID')");
					$result = mysqli_query($con, $sql);
					
						if(!mysqli_query($con, $sql))
						{
							die('Could not register'.mysqli_error($con));
						}
						else
							{
								echo "<div class='form'><center><h3>You have successfully registered!</h3><br/>Click here to <a href='loginmentor.php'>login</a></center></div>";

        			}		}
}
        		
        		
    }

    else 
    {
        echo "<div class='form'><center><h3>Invalid email address</h3><br/>Click <a href='mentorsignup.php'>here</a> to try again</center>";
    }					
   
    
}
else
								{
								echo "<div class='form'><center><h3>Enter values in all fields</h3><br/>Click <a href='mentorsignup.php'>here</a> to try again</center></div>";
								}
   
?>

$f is a string and not an array, hence PHP will quit with a fatal error on that line.

Note: you are wide open to SQL injection.

Note: your DB is not normalised. each of the checkbox values should have its own row in the category table.

Note: when inserting, define which value goes into which column (and it makes the query easier to understand), otherwise you get hard to debug errors.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.