beginnerProgrammer_96 Posted July 2, 2015 Share Posted July 2, 2015 I am trying to insert my inputs of my profile to my database. I've some difficulties which are The php code ignores my select statement even though there is a data inside my database. My radio button group was somehow declared undefined.( and theĀ 'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'skillset' cannot be null' error') My image uploading isn't really working This is my php code: <?php //START OF ADDING FORM //connect to database require_once('Connections/database.php'); $addProfileForm = $_SERVER['PHP_SELF']; //Setup an empty array for errors $errors = array(); //check if there is an existing record in DB $query = "SELECT * FROM `userProfile` WHERE `username` in ( select username from users where id = ".($_SESSION['user_id']).")"; $statement = $database->prepare($query); if($statement === false){ print_r("Error executing the query"); exit(); } //The Primary Key of the row that we want to select. $username = 1; //Bind our value to the paramater :id. $statement->bindValue(':id', $username); //Execute our SELECT statement. $statement->execute(); //check if there is a row $number_of_rows = $statement->fetchColumn(); if ($number_of_rows > 1) { print_r ("There is an existing record. You cannot insert another profile! Either update the old one, or delete to insert again."); } else { //if (isset($_POST['Submit'])) { if ($_SERVER['REQUEST_METHOD'] == 'POST') { //check all $_POST echo '<pre>'; print_r($_POST); echo '<pre>'; //declare all variables first $name = $_POST['name']; //name of the radioGrp $specialization = $_POST['specialization']; $introduction = $_POST['introduction']; $email = $_POST['email']; $contactNo = $_POST['contactNo']; //Radio buttons // $selectedRBofISrow1 = $_POST['ISgrp1']; // $selectedRBofISrow2 = $_POST['ISgrp2']; // $selectedRBofISrow3 = $_POST['ISgrp3']; // $selectedRBofISrow4 = $_POST['ISgrp4']; // $selectedRBofISrow5 = $_POST['ISgrp5']; // $selectedRBofIMrow1 = $_POST['IMgrp1']; // $selectedRBofIMrow2 = $_POST['IMgrp2']; // $selectedRBofIMrow3 = $_POST['IMgrp3']; // $selectedRBofIMrow4 = $_POST['IMgrp4']; // $selectedRBofIMrow5 = $_POST['IMgrp5']; // $selectedRBofCNETrow1 = $_POST['CNETgrp1']; // $selectedRBofCNETrow2 = $_POST['CNETgrp2']; // $selectedRBofCNETrow3 = $_POST['CNETgrp3']; // $selectedRBofCNETrow4 = $_POST['CNETgrp4']; // $selectedRBofCNETrow5 = $_POST['CNETgrp5']; // $selectedRBofITSMrow1 = $_POST['ITSMgrp1']; // $selectedRBofITSMrow2 = $_POST['ITSMgrp2']; // $selectedRBofITSMrow3 = $_POST['ITSMgrp3']; // $selectedRBofITSMrow4 = $_POST['ITSMgrp4']; // $selectedRBofITSMrow5 = $_POST['ITSMgrp5']; //$fileImage = $_FILES['image']['tmp_name']; if ((trim($name)) == "") { $errors[] = ("Name is required!"); //exit(); } //check if introduction is blank if ((trim($introduction)) == "") { $errors[] = ("Introduction is required!"); //exit(); } //check if introduction is blank if ((trim($email)) == "") { $errors[] = ("Email is required!"); //exit(); } //check if emailis valid if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) { $errors[] = ("Email is not valid."); } //check if contactNo is blank if ((trim($contactNo)) == "") { $errors[] = ("Contact number is required!"); //exit(); } //check if contactNo is valid //checks for numeric, then check for 8 digits if(!is_numeric ($contactNo)) { $errors[] = ("Contact number is invalid! All must be in numerics!"); //exit(); } if(strlen(trim($contactNo)) != { $errors[] = ("Contact number must be of 8 digits!"); //exit(); } if (sizeof($errors) > 0) { foreach($errors as $error) { printf("<li>%s</li>", $error); //exit(); } } else { //declare specialization, it will NOT be blank as we have declare it to be checked. if (isset($_POST['specialization']) && $_POST['specialization'] == 'Specialization_1') { $specialization = "IS"; //Check for first row if($_POST['ISgrp1'] == 'C#-0') { $selectedRBofISrow1 = 'C#-0'; } else if ($_POST['ISgrp1'] == 'C#-1') { $selectedRBofISrow1 = 'C#-1'; } else if ($_POST['ISgrp1'] == 'C#-2') { $selectedRBofISrow1 = 'C#-2'; } else if ($_POST['ISgrp1'] == 'C#-3') { $selectedRBofISrow1 = 'C#-3'; } else if ($_POST['ISgrp1'] == 'C#-4') { $selectedRBofISrow1 = 'C#-4'; } else if ($_POST['ISgrp1'] == 'C#-5') { $selectedRBofISrow1 = 'C#-5'; } //Check for second row if($_POST['ISgrp2'] == 'php-0') { $selectedRBofISrow2 = 'php-0'; } else if ($_POST['ISgrp2'] == 'php-1') { $selectedRBofISrow2 = 'php-1'; } else if ($_POST['ISgrp2'] == 'php-2') { $selectedRBofISrow2 = 'php-2'; } else if ($_POST['ISgrp2'] == 'php-3') { $selectedRBofISrow2 = 'php-3'; } else if ($_POST['ISgrp2'] == 'php-4') { $selectedRBofISrow2 = 'php-4'; } else if ($_POST['ISgrp2'] == 'php-5') { $selectedRBofISrow2 = 'php-5'; } //Check for third row if($_POST['ISgrp3'] == 'HTML-0') { $selectedRBofISrow3 = 'HTML-0'; } else if ($_POST['ISgrp3'] == 'HTML-1') { $selectedRBofISrow3 = 'HTML-1'; } else if ($_POST['ISgrp3'] == 'HTML-2') { $selectedRBofISrow3 = 'HTML-2'; } else if ($_POST['ISgrp3'] == 'HTML-3') { $selectedRBofISrow3 = 'HTML-3'; } else if ($_POST['ISgrp3'] == 'HTML-4') { $selectedRBofISrow3 = 'HTML-4'; } else if ($_POST['ISgrp3'] == 'HTML-5') { $selectedRBofISrow3 = 'HTML-5'; } //Check for fourth row if($_POST['ISgrp4'] == 'Java-0') { $selectedRBofISrow4 = 'Java-0'; } else if ($_POST['ISgrp4'] == 'Java-1') { $selectedRBofISrow4 = 'Java-1'; } else if ($_POST['ISgrp4'] == 'Java-2') { $selectedRBofISrow4 = 'Java-2'; } else if ($_POST['ISgrp4'] == 'Java-3') { $selectedRBofISrow4 = 'Java-3'; } else if ($_POST['ISgrp4'] == 'Java-4') { $selectedRBofISrow4 = 'Java-4'; } else if ($_POST['ISgrp4'] == 'Java-5') { $selectedRBofISrow4 = 'Java-5'; } //Check for fifth row if($_POST['ISgrp5'] == 'MAD-0') { $selectedRBofISrow5 = 'MAD-0'; } else if ($_POST['ISgrp5'] == 'MAD-1') { $selectedRBofISrow5 = 'php-1'; } else if ($_POST['ISgrp5'] == 'MAD-2') { $selectedRBofISrow5 = 'MAD-2'; } else if ($_POST['ISgrp5'] == 'MAD-3') { $selectedRBofISrow5 = 'MAD-3'; } else if ($_POST['ISgrp5'] == 'MAD-4') { $selectedRBofISrow5 = 'MAD-4'; } else if ($_POST['ISgrp5'] == 'MAD-5') { $selectedRBofISrow5 = 'MAD-5'; } $skillset = ($selectedRBofISrow1 . $selectedRBofISrow2 . $selectedRBofISrow3 . $selectedRBofISrow4 . $selectedRBofISrow5); //put the skillsets code here? // $valueOfSkillsets = ($selectedRBofISrow1 .$selectedRBofISrow2 //.$selectedRBofISrow3 .$selectedRBofISrow4. $selectedRBofISrow5); } else if (isset($_POST['specialization']) && $_POST['specialization'] == 'Specialization_2') { $specialization = "IM"; //Check for first row if($_POST['IMgrp1'] == 'Web Design-0') { $selectedRBofIMrow1 = "Web Design-0"; } else if ($_POST['IMgrp1'] == "Web Design-1") { $selectedRBofIMrow1 = 'Web Design-1'; } else if ($_POST['IMgrp1'] == "Web Design-2") { $selectedRBofIMrow1 = 'Web Design-2'; } else if ($_POST['IMgrp1'] == "Web Design-3") { $selectedRBofIMrow1 = 'Web Design-3'; } else if ($_POST['IMgrp1'] == "Web Design-4") { $selectedRBofIMrow1 = "Web Design-4"; } else if ($_POST['IMgrp1'] == "Web Design-5") { $selectedRBofIMrow1 = "Web Design-5"; } //Check for second row if($_POST['IMgrp2'] == "3Dmodelling-0") { $selectedRBofIMrow1 = "3Dmodelling-0"; } else if ($_POST['IMgrp2'] == "3Dmodelling-1") { $selectedRBofIMrow1 = "3Dmodelling-1"; } else if ($_POST['IMgrp2'] == "3Dmodelling-2") { $selectedRBofIMrow1 = "3Dmodelling-2"; } else if ($_POST['IMgrp2'] == "3Dmodelling-3") { $selectedRBofIMrow1 = "3Dmodelling-3"; } else if ($_POST['IMgrp2'] == "3Dmodelling-4") { $selectedRBofIMrow1 = "3Dmodelling-4"; } else if ($_POST['IMgrp2'] == "3Dmodelling-5") { $selectedRBofIMrow1 = "3Dmodelling-5"; } //Check for third row if($_POST['IMgrp3'] == "Graphic Design-0") { $selectedRBofIMrow1 = "Graphic Design-0"; } else if ($_POST['IMgrp3'] == "Graphic Design-1") { $selectedRBofIMrow1 = "Graphic Design-1"; } else if ($_POST['IMgrp3'] == "Graphic Design-2") { $selectedRBofIMrow1 = "Graphic Design-2"; } else if ($_POST['IMgrp3'] == " Design-3") { $selectedRBofIMrow1 = "Graphic Design-3"; } else if ($_POST['IMgrp3'] == "Graphic Design-4") { $selectedRBofIMrow1 = "Graphic Design-4"; } else if ($_POST['IMgrp3'] == "Graphic Design-5") { $selectedRBofIMrow1 = "Graphic Design-5"; } //Check for fourth row if($_POST['IMgrp4'] == "VideoEditing-0") { $selectedRBofIMrow1 = "VideoEditing-0"; } else if ($_POST['IMgrp4'] == "VideoEditing-1") { $selectedRBofIMrow1 = "VideoEditing-1"; } else if ($_POST['IMgrp4'] == "VideoEditing-2") { $selectedRBofIMrow1 = "VideoEditing-2"; } else if ($_POST['IMgrp4'] == "VideoEditing-3") { $selectedRBofIMrow1 = "VideoEditing-3"; } else if ($_POST['IMgrp4'] == "VideoEditing-4") { $selectedRBofIMrow1 = "VideoEditing-4"; } else if ($_POST['IMgrp4'] == "VideoEditing-5") { $selectedRBofIMrow1 = "VideoEditing-5"; } //Check for fifth row if($_POST['IMgrp5'] == "motionGraphics-0") { $selectedRBofIMrow1 = "motionGraphics-0"; } else if ($_POST['IMgrp5'] == "motionGraphics-1") { $selectedRBofIMrow1 = "motionGraphics-1"; } else if ($_POST['IMgrp5'] == "motionGraphics-2") { $selectedRBofIMrow1 = "motionGraphics-2"; } else if ($_POST['IMgrp5'] == "motionGraphics-3") { $selectedRBofIMrow1 = "motionGraphics-3"; } else if ($_POST['IMgrp5'] == "motionGraphics-4") { $selectedRBofIMrow1 = "motionGraphics-4"; } else if ($_POST['IMgrp5'] == "motionGraphics-5") { $selectedRBofIMrow1 = "motionGraphics-5"; } $skillset = ($selectedRBofIMrow1 . $selectedRBofIMrow2 . $selectedRBofIMrow3 . $selectedRBofISrow4 . $selectedRBofISrow5); //put the skillsets code here? //$valueOfSkillsets =($selectedRBofIMrow1. $selectedRBofIMrow2 //.$selectedRBofIMrow3 .$selectedRBofIMrow4.$selectedRBofIMrow5); } else if (isset($_POST['specialization']) && $_POST['specialization'] == 'Specialization_3') { $specialization = "CNET"; //Check for first row if($_POST['CNETgrp1'] == "networkSecurity-0") { $selectedRBofCNETrow1 = "networkSecurity-0"; } else if ($_POST['CNETgrp1'] == "networkSecurity-1") { $selectedRBofCNETrow1 = "networkSecurity-1"; } else if ($_POST['CNETgrp1'] == "networkSecurity-2") { $selectedRBofCNETrow1 = "networkSecurity-2"; } else if ($_POST['CNETgrp1'] == "networkSecurity-3") { $selectedRBofCNETrow1 = "networkSecurity-3"; } else if ($_POST['CNETgrp1'] == "networkSecurity-4") { $selectedRBofCNETrow1 = "networkSecurity-4"; } else if ($_POST['CNETgrp1'] == "networkSecurity-5") { $selectedRBofCNETrow1 = "networkSecurity-5"; } //Check for second row if($_POST['CNETgrp2'] == "RBNetworkDesign-0") { $selectedRBofCNETrow1 = "RBNetworkDesign-0"; } else if ($_POST['CNETgrp2'] == "RBNetworkDesign-1") { $selectedRBofCNETrow1 = "RBNetworkDesign-1"; } else if ($_POST['CNETgrp2'] == "RBNetworkDesign-2") { $selectedRBofCNETrow1 = "RBNetworkDesign-2"; } else if ($_POST['CNETgrp2'] == "RBNetworkDesign-3") { $selectedRBofCNETrow1 = "RBNetworkDesign-3"; } else if ($_POST['CNETgrp2'] == "RBNetworkDesign-4") { $selectedRBofCNETrow1 = "RBNetworkDesign-4"; } else if ($_POST['CNETgrp2'] == "RBNetworkDesign-5") { $selectedRBofCNETrow1 = "RBNetworkDesign-5"; } //Check for third row if($_POST['CNETgrp3'] == "BroadbandAccess-0") { $selectedRBofCNETrow1 = "BroadbandAccess-0"; } else if ($_POST['CNETgrp3'] == "BroadbandAccess-1") { $selectedRBofCNETrow1 = "BroadbandAccess-1"; } else if ($_POST['CNETgrp3'] == "BroadbandAccess-2") { $selectedRBofCNETrow1 = "BroadbandAccess-2"; } else if ($_POST['CNETgrp3'] == "BroadbandAccess-3") { $selectedRBofCNETrow1 = "BroadbandAccess-3"; } else if ($_POST['CNETgrp3'] == "BroadbandAccess-4") { $selectedRBofCNETrow1 = "BroadbandAccess-4"; } else if ($_POST['CNETgrp3'] == "BroadbandAccess-5") { $selectedRBofCNETrow1 = "BroadbandAccess-5"; } //Check for fourth row if($_POST['CNETgrp4'] == "ICTInfrastructureManagement-0") { $selectedRBofCNETrow1 = "ICTInfrastructureManagement-0"; } else if ($_POST['CNETgrp4'] == "ICTInfrastructureManagement-1") { $selectedRBofCNETrow1 = "ICTInfrastructureManagement-1"; } else if ($_POST['CNETgrp4'] == "ICTInfrastructureManagement-2") { $selectedRBofCNETrow1 = "ICTInfrastructureManagement-2"; } else if ($_POST['CNETgrp4'] == "ICTInfrastructureManagement-3") { $selectedRBofCNETrow1 = "ICTInfrastructureManagement-3"; } else if ($_POST['CNETgrp4'] == "ICTInfrastructureManagement-4") { $selectedRBofCNETrow1 = "ICTInfrastructureManagement-4"; } else if ($_POST['CNETgrp4'] == "ICTInfrastructureManagement-5") { $selectedRBofCNETrow1 = "ICTInfrastructureManagement-5"; } //Check for fifth row if($_POST['CNETgrp5'] == "motionGraphics-0") { $selectedRBofCNETrow1 = "Technopreneurship-0"; } else if ($_POST['CNETgrp5'] == "Technopreneurship-1") { $selectedRBofCNETrow1 = "Technopreneurship-1"; } else if ($_POST['CNETgrp5'] == "Technopreneurship-2") { $selectedRBofCNETrow1 = "Technopreneurship-2"; } else if ($_POST['CNETgrp5'] == "Technopreneurship-3") { $selectedRBofCNETrow1 = "Technopreneurship-3"; } else if ($_POST['CNETgrp5'] == "Technopreneurship-4") { $selectedRBofCNETrow1 = "Technopreneurship-4"; } else if ($_POST['CNETgrp5'] == "Technopreneurship-5") { $selectedRBofCNETrow1 = "Technopreneurship-5"; } $skillset = ($selectedRBofCNETrow1 . $selectedRBofCNETrow2 . $selectedRBofCNETrow3 . $selectedRBofCNETrow4 . $selectedRBofCNETrow5); //put the skillsets code here? //$valueOfSkillsets =($selectedRBofCNETrow1. $selectedRBofCNETrow2 //.$selectedRBofCNETrow3 .$selectedRBofCNETrow4.$selectedRBofCNETrow5); } else if (isset($_POST['specialization']) && $_POST['specialization'] == 'Specialization_4') { $specialization = "ITSM"; //Check for first row if($_POST['ITSMgrp1'] == "virtualisation-0") { $selectedRBofITSMrow1 = "virtualisation-0"; } else if ($_POST['ITSMgrp1'] == "virtualisation-1") { $selectedRBofITSMrow1 = "virtualisation-1"; } else if ($_POST['ITSMgrp1'] == "virtualisation-2") { $selectedRBofITSMrow1 = "virtualisation-2"; } else if ($_POST['ITSMgrp1'] == "virtualisation-3") { $selectedRBofCNETrow1 = "networkSecurity-3"; } else if ($_POST['ITSMgrp1'] == "virtualisation-4") { $selectedRBofITSMrow1 = "virtualisation-4"; } else if ($_POST['ITSMgrp1'] == "virtualisation-5") { $selectedRBofITSMrow1 = "virtualisation-5"; } //Check for second row if($_POST['ITSMgrp2'] == "ITSM-0") { $selectedRBofITSMrow1 = "ITSM-0"; } else if ($_POST['ITSMgrp2'] == "ITSM-1") { $selectedRBofITSMrow1 = "ITSM-1"; } else if ($_POST['ITSMgrp2'] == "ITSM-2") { $selectedRBofITSMrow1 = "ITSM-2"; } else if ($_POST['ITSMgrp2'] == "ITSM-3") { $selectedRBofITSMrow1 = "ITSM-3"; } else if ($_POST['ITSMgrp2'] == "ITSM-4") { $selectedRBofITSMrow1 = "ITSM-4"; } else if ($_POST['ITSMgrp2'] == "ITSM-5") { $selectedRBofITSMrow1 = "ITSM-5"; } //Check for third row if($_POST['ITSMgrp3'] == "serverAdministration-0") { $selectedRBofITSMrow1 = "serverAdministration-0"; } else if ($_POST['ITSMgrp3'] == "serverAdministration-1") { $selectedRBofITSMrow1 = "serverAdministration-1"; } else if ($_POST['ITSMgrp3'] == "serverAdministration-2") { $selectedRBofITSMrow1 = "serverAdministration-2"; } else if ($_POST['ITSMgrp3'] == "serverAdministration-3") { $selectedRBofITSMrow1 = "serverAdministration-3"; } else if ($_POST['ITSMgrp3'] == "serverAdministration-4") { $selectedRBofITSMrow1 = "serverAdministration-4"; } else if ($_POST['ITSMgrp3'] == "serverAdministration-5") { $selectedRBofITSMrow1 = "serverAdministration-5"; } //Check for fourth row if($_POST['ITSMgrp4'] == "DatabaseAdministration&Management-0") { $selectedRBofITSMrow1 = "DatabaseAdministration&Management-0"; } else if ($_POST['ITSMgrp4'] == "DatabaseAdministration&Management-1") { $selectedRBofITSMrow1 = "DatabaseAdministration&Management-1"; } else if ($_POST['ITSMgrp4'] == "DatabaseAdministration&Management-2") { $selectedRBofITSMrow1 = "DatabaseAdministration&Management-2"; } else if ($_POST['ITSMgrp4'] == "DatabaseAdministration&Management-3") { $selectedRBofITSMrow1 = "DatabaseAdministration&Management-3"; } else if ($_POST['ITSMgrp4'] == "DatabaseAdministration&Management-4") { $selectedRBofITSMrow1 = "DatabaseAdministration&Management-4"; } else if ($_POST['ITSMgrp4'] == "DatabaseAdministration&Management-5") { $selectedRBofITSMrow1 = "DatabaseAdministration&Management-5"; } //Check for fifth row if($_POST['ITSMgrp5'] == "InternetofThingsAppDevelopment-0") { $selectedRBofITSMrow1 = "InternetofThingsAppDevelopment-0"; } else if ($_POST['ITSMgrp5'] == "InternetofThingsAppDevelopment-1") { $selectedRBofITSMrow1 = "InternetofThingsAppDevelopment-1"; } else if ($_POST['ITSMgrp5'] == "InternetofThingsAppDevelopment-2") { $selectedRBofITSMrow1 = "InternetofThingsAppDevelopment-2"; } else if ($_POST['ITSMrp5'] == "InternetofThingsAppDevelopment-3") { $selectedRBofCITSMrow1 = "InternetofThingsAppDevelopment-3"; } else if ($_POST['ITSMgrp5'] == "InternetofThingsAppDevelopment-4") { $selectedRBofITSMrow1 = "InternetofThingsAppDevelopment-4"; } else if ($_POST['ITSMgrp5'] == "InternetofThingsAppDevelopment-5") { $selectedRBofITSMrow1 = "InternetofThingsAppDevelopment-5"; } $skillset = ($selectedRBofITSMrow1 . $selectedRBofITSMrow2 . $selectedRBofITSMrow3 . $selectedRBofITSMrow4 . $selectedRBofITSMrow5); //put the skillsets code here? //$valueOfSkillsets = ($selectedRBofITSMrow1 .$selectedRBofITSMrow2 //.$selectedRBofITSMrow3 .$selectedRBofITSMrow4. $selectedRBofITSMrow5); } //value of skillsets to be put here?? //if(isset($_POST['specialization'])){ // if($_POST['specialization'] == 'Specialization_1') { // $specialization = "IS"; // //put the skillsets code here? // $valueOfSkillsets = ($selectedRBofISrow1 .$selectedRBofISrow2 // .$selectedRBofISrow3 .$selectedRBofISrow4. $selectedRBofISrow5); // } // else if($_POST['specialization'] == 'Specialization_2') { // $specialization = "IM"; // //put the skillsets code here? // $valueOfSkillsets =($selectedRBofIMrow1. $selectedRBofIMrow2 // .$selectedRBofIMrow3 .$selectedRBofIMrow4.$selectedRBofIMrow5); // } // else if($_POST['specialization'] == 'Specialization_3') { // $specialization = "ITSM"; // //put the skillsets code here? // $valueOfSkillsets =($selectedRBofCNETrow1. $selectedRBofCNETrow2 // .$selectedRBofCNETrow3 .$selectedRBofCNETrow4.$selectedRBofCNETrow5); // } // else if($_POST['specialization'] == 'Specialization_4') { // $specialization = "CNET"; // //put the skillsets code here? // $valueOfSkillsets =($selectedRBofITSMrow1. $selectedRBofITSMrow2 // .$selectedRBofITSMrow3 .$selectedRBofITSMrow4.$selectedRBofITSMrow5); // } // //} $fileError = $_FILES['image']['error']; //validate if there's any error if($fileError > 0){ // header("Location:".$returnURL."?err=imgUploadError"); $errors[] = ("Error on uploading image!"); exit; } $maxSize = 100000; $fileSize = $_FILES['image']['size']; //validate image size if($fileSize > $maxSize){ $errors[] = ("Image size is too big!"); // exit; } //get the temporary location of the file $fileTempName = $_FILES['image']['tmp_name']; $trueFileType = exif_imagetype($fileTempName); $allowedFiles = array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG); if (in_array($trueFileType, $allowedFiles)) { switch($trueFileType){ case 1 : $fileExt = ".gif"; break; case 2: $fileExt = ".jpg"; break; case 3 : $fileExt = ".png"; break; } }else{ //header("Location:".$returnURL."?err=WrongFileType"); $errors[] = ("The one you've uploaded is not supported! Please check that it is in the right format."); exit; } // Get the path to upload the image to $myPathInfo = pathinfo($_SERVER['DOCUMENT_ROOT'].$_SERVER['PHP_SELF']); $currentDir = $myPathInfo['dirname']; $imgDir = $currentDir . '/profilePic/'; // Insert the other data into the database, get the new ID and create the new filename $stmt = $database->prepare("INSERT INTO userProfile (picName, picPath, Name, introduction, Specialization,skillset, Email, ContactNo) VALUES (:picName,:picPath,:Name,:introduction,:Specialization,:skillset,:Email,:ContactNo)"); $stmt->bindValue(':picName',$_FILES['image']['name']); $stmt->bindValue(':picPath', $imgDir); $stmt->bindValue(':Name', $name); $stmt->bindValue(':introduction', $introduction); $stmt->bindValue(':Specialization', $specialization); $stmt->bindValue(':skillset', $skillset); $stmt->bindValue(':Email', $email); $stmt->bindValue(':ContactNo', $contactNo); $stmt->execute(); $newID = $stmt->insert_id; $newFileName = $newID . $fileExt; $stmt->close(); // Update the database with the new image filename $stmt = $database->prepare("UPDATE userProfile SET picName = :newFileName WHERE id = ".($_SESSION['user_id']).")"); //The Primary Key of the row that we want to select. $username = 1; //Bind our value to the paramater $statement->bindValue(':id', $username); $stmt->bindValue(':newFileName', $newFileName); $stmt->bindValue('id',$username); $stmt->execute(); if ($database->query($stmt) === TRUE) { echo "New record created successfully"; } else { echo "Error: " . $stmt . "<br>" . $database->error; } $stmt->close(); // Move the file and redirect $newImgLocation = $imgDir . $newFileName; if(move_uploaded_file($fileTempName, $newImgLocation)){ header("Location:".$returnURL); }else{ header("Location:".$returnURL."?err=UploadProb"); } } //$fileImage = $_FILES['imageField']['tmpName']; //Check if the user did upload image // if (isset($_FILES['imageField']) && $_FILES['imageField']['size'] > 0) { // // $image = addslashes(file_get_contents($_FILES['imageField']['tmpName'])); // $image_name = addslashes($_FILES['imageField']['name']); // // // Temporary file name stored on the server // $tmpName = $_FILES['imagefield']['tmp_name']; // // Read the file // $fp = fopen($tmpName, 'r'); // $data = fread($fp, filesize($tmpName)); // } // else { // try { // $msg = upload(); // function calling to upload an image // echo $msg; // } // catch(Exception $e) { // echo $e->getMessage(); // echo 'Sorry, Could not upload file'; // } // } // // function file_upload_error_message($error_code) { // switch ($error_code) { // case UPLOAD_ERR_INI_SIZE: // return 'The uploaded file exceeds the upload_max_filesize directive in php.ini'; // case UPLOAD_ERR_FORM_SIZE: // return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'; // case UPLOAD_ERR_PARTIAL: // return 'The uploaded file was only partially uploaded'; // case UPLOAD_ERR_NO_FILE: // return 'No file was uploaded'; // case UPLOAD_ERR_NO_TMP_DIR: // return 'Missing a temporary folder'; // case UPLOAD_ERR_CANT_WRITE: // return 'Failed to write file to disk'; // case UPLOAD_ERR_EXTENSION: // return 'File upload stopped by extension'; // default: // return 'Unknown upload error'; // } // } // if (!isset ($file)) { // $errors[] = "Please select an image"; // } // //check if the user has uploaded image // else if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) { // $image = addslashes(file_get_contents($_FILES['image']['tmp_name'])); // $image_name = addslashes($_FILES ['image']['name']); // $image_size = getimagesize ($_FILES ['image']['tmp_name']); // $image_path = "profilePic/".basename($image_name); // File destination // // if ($image_size == FALSE) // print "That's not an image"; // // else // { // //uploading the image file with the image file name into the directory. // if(move_uploaded_file($image_name, $image_path)) // echo "File uploaded"; // // if (!$stmt = $database->prepare("INSERT picName,picPath INTO userProfile VALUES('$image_name', '$image_path')")) { // print "Problem uploading image"; // exit(); // } // // } // // } //check if name is blank //put in the query. No need to connect to database as database alr declared at the top. // $sql = "INSERT INTO userProfile (picName, picPath, Name, introduction, Specialization,skillset, Email, ContactNo) VALUES // ($image_name,$image_path,$name,$introduction,$specialization, $skillset, $email, $contactNo)"; // //if ($database->query($sql) === TRUE) { // echo "New record created successfully"; //} else { // echo "Error: " . $sql . "<br>" . $database->error; //} // //$database->close(); //close bracket of select statement } //close bracket for request post } ?> Here's my html form: <!doctype html> <html> <head> <meta charset="utf-8"> <title>Add Profile</title> <link href="css/profile.css" rel="stylesheet" type="text/css"> <link rel="stylesheet" href="css/ajxmenu-profile.css" type="text/css"> <script src="http://code.jquery.com/jquery-latest.js"></script> <script type="text/javascript"> //For toggling the radio buttons. //Cant toggle between rows, so have to set up <tr> manually $(document).ready(function () { //start straightaway for IS rb checked $("#specialization_1").trigger("click"); //hide the other rows and show only the IS rows $('#IMrow1,#IMrow2,#IMrow3,#IMrow4,#IMrow5,#CNETrow1,#CNETrow2,#CNETrow3,#CNETrow4, #CNETrow5,#ITSMrow1,#ITSMrow2,#ITSMrow3,#ITSMrow4,#ITSMrow5').hide(); //When IS rb is clicked,only show IS $('#specialization_1').click(function () { //add 'fast' if want see action $('#ISrow1').show(); $('#ISrow2').show(); $('#ISrow3').show(); $('#ISrow4').show(); $('#ISrow5').show(); $('#IMrow1').hide(); $('#IMrow2').hide(); $('#IMrow3').hide(); $('#IMrow4').hide(); $('#IMrow5').hide(); $('#CNETrow1').hide(); $('#CNETrow2').hide(); $('#CNETrow3').hide(); $('#CNETrow4').hide(); $('#CNETrow5').hide(); $('#ITSMrow1').hide(); $('#ITSMrow2').hide(); $('#ITSMrow3').hide(); $('#ITSMrow4').hide(); $('#ITSMrow5').hide(); }); //When IM rb is clicked,only show IS $('#specialization_2').click(function () { $('#ISrow1').hide(); $('#ISrow2').hide(); $('#ISrow3').hide(); $('#ISrow4').hide(); $('#ISrow5').hide(); $('#IMrow1').show(); $('#IMrow2').show(); $('#IMrow3').show(); $('#IMrow4').show(); $('#IMrow5').show(); $('#CNETrow1').hide(); $('#CNETrow2').hide(); $('#CNETrow3').hide(); $('#CNETrow4').hide(); $('#CNETrow5').hide(); $('#ITSMrow1').hide(); $('#ITSMrow2').hide(); $('#ITSMrow3').hide(); $('#ITSMrow4').hide(); $('#ITSMrow5').hide(); }); //When CNET rb is clicked,only show IS $('#specialization_3').click(function () { $('#ISrow1').hide(); $('#ISrow2').hide(); $('#ISrow3').hide(); $('#ISrow4').hide(); $('#ISrow5').hide(); $('#IMrow1').hide(); $('#IMrow2').hide(); $('#IMrow3').hide(); $('#IMrow4').hide(); $('#IMrow5').hide(); $('#CNETrow1').show(); $('#CNETrow2').show(); $('#CNETrow3').show(); $('#CNETrow4').show(); $('#CNETrow5').show(); $('#ITSMrow1').hide(); $('#ITSMrow2').hide(); $('#ITSMrow3').hide(); $('#ITSMrow4').hide(); $('#ITSMrow5').hide(); }); //When ITSM rb is clicked,only show IS $('#specialization_4').click(function () { $('#ISrow1').hide(); $('#ISrow2').hide(); $('#ISrow3').hide(); $('#ISrow4').hide(); $('#ISrow5').hide(); $('#IMrow1').hide(); $('#IMrow2').hide(); $('#IMrow3').hide(); $('#IMrow4').hide(); $('#IMrow5').hide(); $('#CNETrow1').hide(); $('#CNETrow2').hide(); $('#CNETrow3').hide(); $('#CNETrow4').hide(); $('#CNETrow5').hide(); $('#ITSMrow1').show(); $('#ITSMrow2').show(); $('#ITSMrow3').show(); $('#ITSMrow4').show(); $('#ITSMrow5').show(); }); }); </script> </head> <body> <div id = "Wrapper"> <div id = "content"> <header id="Header Profile"> <img src="photos/download.jpg" width="40" height="40" float="left"> </header> <nav id = "sidebarProfile"> <!--For the navigation buttons--> <div class="AJXCSSMenuNDPZHYA"><!-- AJXFILE:css/ajxmenu-profile.css --> <ul> <li><a class="ajxsub" href="Profile.php" target="_parent" title="Profile">Profile</a> <ul> <li><a href="addProfile.php" target="_parent" title="addProfile">Add Profile</a></li> <li><a href="EditProfile.php" target="_parent" title="editProfile">Edit Profile</a></li> <li><a href="deleteProfile.php" target="_parent" title="deleteProfile">Delete Profile</a></li> </ul> </li> <li><a class="ajxsub" href="Portfolio.php" target="_parent" title="portfolio">Portfolio</a> <ul> <li><a href="addPortFolio.php" target="_parent" title="addPortfolio">Add Portfolio</a></li> <li><a href="editPortfolio.php" target="_parent" title="editPortfolio">Edit Portfolio</a></li> <li><a href="deletePortfolio.php" target="_parent" title="deletePortfolio">deletePortfolio</a></li> </ul> </li> </ul> </div> </nav> <aside id = "ContentProfile"> <!--For the form and other contents--> <form action="<?php $addProfileForm ?>" method="post" enctype="multipart/form-data" name="addingProfile"> <fieldset> <legend>Add Profile</legend> <table width="auto" height="auto" border="0"> <tr> <th scope="row"><div align="left">Profile Picture:</div></th> <td><input name="MAX_FILE_SIZE" type="hidden" value=""><input type="file" name="image"></td> </tr> <tr> <th scope="row"><div align="left">Name:</div></th> <td><input name="name" type="text" value=''></td> </tr> <tr> <th scope="row"><div align="left">Description:</div></th> <td><input name="introduction" type="text" value=''></td> </tr> <tr> <th scope="row"><div align="left">Email:</div></th> <td><input name="email" type="text" value=''></td> </tr> <tr> <th scope="row"><div align="left">Contact No:</div></th> <td><input name="contactNo" type="text" value=''></td> </tr> <tr> <th scope="row"><div align="left">Specialization:</div></th> <div> <td><table width="auto"> <tr> <td><input name="specialization" type="radio" id="specialization_1" value="IS" checked="CHECKED"> IS</td> <td><input type="radio" name="specialization" value="IM" id="specialization_2"> IM</td> </tr> <tr> <td><input type="radio" name="specialization" value="CNET" id="specialization_3"> CNET</td> <td><input type="radio" name="specialization" value="ITSM" id="specialization_4"> ITSM</td> </tr> </table></td> </div> </tr> <tr> <td colspan="7" align="left"><p><strong>Skillset:<br> Please rank the skillset below..<br> (5- expert, 3- average , 0-not applicable)</strong></p></td> </tr> <th height="auto" colspan="2" scope="row"><table width="auto"> <tr id="ISrow1"> <td width="auto"><div align="left">C#</div></td> <td width="auto"><label> <input name="ISgrp1" type="radio" id="IS_table_1" value="C#-0" checked="CHECKED" > 0</label></td> <td width="auto"><label> <input type="radio" name="ISgrp1" value="C#-1" id="IS_table_2"> 1</label></td> <td width="auto"><label> <input type="radio" name="ISgrp1" value="C#-2" id="IS_table_3"> 2</label> <label></label></td> <td width="auto"><label> <input type="radio" name="ISgrp1" value="C#-3" id="IS_table_4"> 3</label></td> <td width="auto"><label> <input type="radio" name="ISgrp1" value="C#-4" id="IS_table_5"> 4</label></td> <td width="auto"><label> <input type="radio" name="ISgrp1" value="C#-5" id="IS_table_6"> 5</label></td> </tr> <tr id="ISrow2"> <td><div align="left">PHP/SQL</div></td> <td><label> <input name="ISgrp2" type="radio" id="IS_table_7" value="PHP-0" checked="CHECKED"> 0</label></td> <td><input type="radio" name="ISgrp2" value="PHP-1" id="IS_table_8"> 1</td> <td><label> <input type="radio" name="ISgrp2" value="PHP-2" id="IS_table_9"> 2</label></td> <td><label> <input type="radio" name="ISgrp2" value="PHP-3" id="IS_table_10"> 3</label></td> <td><label> <input type="radio" name="ISgrp2" value="PHP-4" id="IS_table_11"> 4</label></td> <td><label> <input type="radio" name="ISgrp2" value="PHP-5" id="IS_table_12"> 5</label></td> </tr> <tr id="ISrow3"> <td><div align="left">HTML/CSS</div></td> <td><label> <input name="ISgrp3" type="radio" id="IS_table_13" value="HTML-0" checked="CHECKED"> 0</label></td> <td><input type="radio" name="ISgrp3" value="HTML-1" id="IS_table_14"> 1</td> <td><label> <input type="radio" name="ISgrp3" value="HTML-2" id="IS_table_15"> 2</label></td> <td><label> <input type="radio" name="ISgrp3" value="HTML-3" id="IS_table_16"> 3</label></td> <td><label> <input type="radio" name="ISgrp3" value="HTML-4" id="IS_table_17"> 4</label></td> <td><input type="radio" name="ISgrp3" value="HTML-5" id="IS_table_18"> 5</td> </tr> <tr id="ISrow4"> <td><div align="left">Java Enterprise Development</div></td> <td><label> <input name="ISgrp4" type="radio" id="IS_table_19" value="Java-0" checked="CHECKED"> 0</label></td> <td><label> <input type="radio" name="ISgrp4" value="Java-1" id="IS_table_20"> 1</label></td> <td><input type="radio" name="ISgrp4" value="Java-2" id="IS_table_21"> 2</td> <td><label> <input type="radio" name="ISgrp4" value="Java-3" id="IS_table_22"> 3</label></td> <td><label> <input type="radio" name="ISgrp4" value="Java-4" id="IS_table_23"> 4</label></td> <td><input type="radio" name="ISgrp4" value="Java-5" id="IS_table_24"> 5</td> </tr> <tr id="ISrow5"> <td><div align="left">Mobile Application Development</div></td> <td><label> <input name="ISgrp5" type="radio" id="IS_table_25" value="MAD-0" checked="CHECKED"> 0</label></td> <td><label> <input type="radio" name="ISgrp5" value="MAD-1" id="IS_table_26"> 1</label></td> <td><input type="radio" name="ISgrp5" value="MAD-2" id="IS_table_27"> 2</td> <td><label> <input type="radio" name="ISgrp5" value="MAD-3" id="IS_table_28"> 3</label></td> <td><label> <input type="radio" name="ISgrp5" value="MAD-4" id="IS_table_29"> 4</label></td> <td><input type="radio" name="ISgrp5" value="MAD-5" id="IS_table_30"> 5</td> </tr> <tr id="IMrow1"> <td><div align="left">Web Design</div></td> <td><label> <input name="IMgrp1" type="radio" id="IM_table_1" value="Web Design-0" checked="CHECKED"> 0</label></td> <td><label> <input type="radio" name="IMgrp1" value="Web Design-1" id="IM_table_2"> 1</label></td> <td><label> <input type="radio" name="IMgrp1" value="Web Design-2" id="IM_table_3"> 2</label></td> <td><label> <input type="radio" name="IMgrp1" value="Web Design-3" id="IM_table_4"> 3</label></td> <td><label> <input type="radio" name="IMgrp1" value="Web Design-4" id="IM_table_5"> 4</label></td> <td><label> <input type="radio" name="IMgrp1" value="Web Design-5" id="IM_table_6"> 5</label></td> </tr> <tr id="IMrow2"> <td><div align="left">3D Modelling & Animations</div></td> <td><label> <input name="IMgrp2" type="radio" id="IM_table_7" value="3Dmodelling-0" checked="CHECKED"> 0</label></td> <td><label> <input type="radio" name="IMgrp2" value="3Dmodelling-1" id="IM_table_8"> 1</label></td> <td><label> <input type="radio" name="IMgrp2" value="3Dmodelling-2" id="IM_table_9"> 2</label></td> <td><label> <input type="radio" name="IMgrp2" value="3Dmodelling-3" id="IM_table_10"> 3</label></td> <td><label> <input type="radio" name="IMgrp2" value="3Dmodelling-4" id="IM_table_11"> 4</label></td> <td><label> <input type="radio" name="IMgrp2" value="3Dmodelling-5" id="IM_table_12"> 5</label></td> </tr> <tr id="IMrow3"> <td><div align="left">Graphic Design</div></td> <td><label> <input name="IMgrp3" type="radio" id="IM_table_13" value="Graphic Design-0" checked="CHECKED"> 0</label></td> <td><label> <input type="radio" name="IMgrp3" value="Graphic Design-1" id="IM_table_14"> 1</label></td> <td><label> <input type="radio" name="IMgrp3" value="Graphic Design-2" id="IM_table_15"> 2</label></td> <td><label> <input type="radio" name="IMgrp3" value="Graphic Design-3" id="IM_table_16"> 3</label></td> <td><label> <input type="radio" name="IMgrp3" value="Graphic Design-4" id="IM_table_17"> 4</label></td> <td><label> <input type="radio" name="IMgrp3" value="Graphic Design-5" id="IM_table_18"> 5</label></td> </tr> <tr id="IMrow4"> <td><div align="left">Video Editing</div></td> <td><label> <input name="IMgrp4" type="radio" id="IM_table_19" value="videoEditing-0" checked="CHECKED"> 0</label></td> <td><label> <input type="radio" name="IMgrp4" value="videoEditing-1" id="IM_table_20"> 1</label></td> <td><label> <input type="radio" name="IMgrp4" value="videoEditing-2" id="IM_table_21"> 2</label></td> <td><label> <input type="radio" name="IMgrp4" value="videoEditing-3" id="IM_table_22"> 3</label></td> <td><label> <input type="radio" name="IMgrp4" value="videoEditing-4" id="IM_table_23"> 4</label></td> <td><label> <input type="radio" name="IMgrp4" value="videoEditing-5" id="IM_table_24"> 5</label></td> </tr> <tr id="IMrow5"> <td><div align="left">Motion Graphics & Effects</div></td> <td><label> <input name="IMgrp5" type="radio" id="IM_table_25" value="motionGraphics-0" checked="CHECKED"> 0</label></td> <td><label> <input type="radio" name="IMgrp5" value="motionGraphics-1" id="IM_table_226"> 1</label></td> <td><label> <input type="radio" name="IMgrp5" value="motionGraphics-2" id="IM_table_27"> 2</label></td> <td><label> <input type="radio" name="IMgrp5" value="motionGraphics-3" id="IM_table_28"> 3</label></td> <td><label> <input type="radio" name="IMgrp5" value="motionGraphics-4" id="IM_table_29"> 4</label></td> <td><label> <input type="radio" name="IMgrp5" value="motionGraphics-5" id="IM_table_30"> 5</label></td> </tr> <tr id="CNETrow1"> <td><div align="left">Network Security</div></td> <td><label> <input name="CNETgrp1" type="radio" id="CNET_table_1" value="networkSecurity-0" checked="CHECKED"> 0</label></td> <td><label></label> <label> <input type="radio" name="CNETgrp1" value="networkSecurity-1" id="CNET_table_2"> 1</label></td> <td><label></label> <label> <input type="radio" name="CNETgrp1" value="networkSecurity-2" id="CNET_table_3"> 2</label></td> <td><label> <input type="radio" name="CNETgrp1" value="networkSecurity-3" id="CNET_table_4"> 3</label></td> <td><label> <input type="radio" name="CNETgrp1" value="networkSecurity-4" id="CNET_table_5"> 4</label></td> <td><label> <input type="radio" name="CNETgrp1" value="networkSecurity-5" id="CNET_table_6"> 5</label></td> </tr> <tr id="CNETrow2"> <td><div align="left">RB Network Design & Implementation</div></td> <td><label> <input name="CNETgrp2" type="radio" id="CNET_table_7" value="RBNetworkDesign-0" checked="CHECKED"> 0</label></td> <td><label></label> <label> <input type="radio" name="CNETgrp2" value="RBNetworkDesign-1" id="CNET_table_8"> 1</label></td> <td><label></label> <label> <input type="radio" name="CNETgrp2" value="RBNetworkDesign-2" id="CNET_table_9"> 2</label></td> <td><label> <input type="radio" name="CNETgrp2" value="RBNetworkDesign-3" id="CNET_table_10"> 3</label></td> <td><label> <input type="radio" name="CNETgrp2" value="RBNetworkDesign-4" id="CNET_table_11"> 4</label></td> <td><label> <input type="radio" name="CNETgrp2" value="RBNetworkDesign-5" id="CNET_table_12"> 5</label></td> </tr> <tr id="CNETrow3"> <td><div align="left">Broadband Access & WAN</div></td> <td><label> <input name="CNETgrp3" type="radio" id="CNET_table_13" value="BroadbandAccess-0" checked="CHECKED"> 0</label></td> <td><label></label> <label> <input type="radio" name="CNETgrp3" value="BroadbandAccess-1" id="CNET_table_14"> 1</label></td> <td><label></label> <label> <input type="radio" name="CNETgrp3" value="BroadbandAccess-2" id="CNET_table_15"> 2</label></td> <td><label> <input type="radio" name="CNETgrp3" value="BroadbandAccess-3" id="CNET_table_16"> 3</label></td> <td><label> <input type="radio" name="CNETgrp3" value="BroadbandAccess-4" id="CNET_table_17"> 4</label></td> <td><label> <input type="radio" name="CNETgrp3" value="BroadbandAccess-5" id="CNET_table_18"> 5</label></td> </tr> <tr id="CNETrow4"> <td><div align="left">ICT Infrastructure Management</div></td> <td><label> <input name="CNETgrp4" type="radio" id="CNET_table_19" value="ICTInfrastructureManagement-0" checked="CHECKED"> 0</label></td> <td><label></label> <label> <input type="radio" name="CNETgrp4" value="ICTInfrastructureManagement-1" id="CNET_table_20"> 1</label></td> <td><label></label> <label> <input type="radio" name="CNETgrp4" value="ICTInfrastructureManagement-2" id="CNET_table_21"> 2</label></td> <td><label> <input type="radio" name="CNETgrp4" value="ICTInfrastructureManagement-3" id="CNET_table_22"> 3</label></td> <td><label> <input type="radio" name="CNETgrp4" value="ICTInfrastructureManagement-4" id="CNET_table_23"> 4</label></td> <td><label> <input type="radio" name="CNETgrp4" value="ICTInfrastructureManagement-5" id="CNET_table_24"> 5</label></td> </tr> <tr id="CNETrow5"> <td><div align="left">Technopreneurship Project</div></td> <td><label> <input name="CNETgrp5" type="radio" id="CNET_table_25" value="Technopreneurship-0" checked="CHECKED"> 0</label></td> <td><label></label> <label> <input type="radio" name="CNETgrp5" value="Technopreneurship-1" id="CNET_table_26"> 1</label></td> <td><label></label> <label> <input type="radio" name="CNETgrp5" value="Technopreneurship-2" id="CNET_table_27"> 2</label></td> <td><label> <input type="radio" name="CNETgrp5" value="Technopreneurship-3" id="CNET_table_28"> 3</label></td> <td><label> <input type="radio" name="CNETgrp5" value="Technopreneurship-4" id="CNET_table_29"> 4</label></td> <td><label> <input type="radio" name="CNETgrp5" value="Technopreneurship-5" id="CNET_table_30"> 5</label></td> </tr> <tr id="ITSMrow1"> <td><div align="left">Virtualisation & Cloud Computing</div></td> <td height="24"><label> <input name="ITSMgrp1" type="radio" id="ITSM_table_1" value="virtualisation_0" checked="CHECKED"> 0</label></td> <td><label> <input type="radio" name="ITSMgrp1" value="virtualisation_1" id="ITSM_table_2"> 1</label></td> <td><label> <input type="radio" name="ITSMgrp1" value="virtualisation_2" id="ITSM_table_3"> 2</label></td> <td><label> <input type="radio" name="ITSMgrp1" value="virtualisation_3" id="ITSM_table_4"> 3</label></td> <td><label> <input type="radio" name="ITSMgrp1" value="virtualisation_4" id="ITSM_table_5"> 4</label></td> <td><input type="radio" name="ITSMgrp1" value="virtualisation_5" id="ITSM_table_6"> 5</td> </tr> <tr id="ITSMrow2"> <td><div align="left">IT Service Operation & Management </div></td> <td height="24"><label> <input name="ITSMgrp2" type="radio" id="ITSM_table_7" value="ITSM_0" checked="CHECKED"> 0</label></td> <td><label> <input type="radio" name="ITSMgrp2" value="ITSM_1" id="ITSM_table_8"> 1</label></td> <td><label> <input type="radio" name="ITSMgrp2" value="ITSM_2" id="ITSM_table_9"> 2</label></td> <td><label> <input type="radio" name="ITSMgrp2" value="ITSM_3" id="ITSM_table_10"> 3</label></td> <td><label> <input type="radio" name="ITSMgrp2" value="ITSM_4" id="ITSM_table_11"> 4</label></td> <td><input type="radio" name="ITSMgrp2" value="ITSM_5" id="ITSM_table_12"> 5</td> </tr> <tr id="ITSMrow3"> <td><div align="left">Server Administration & Security</div></td> <td height="24"><label> <input name="ITSMgrp3" type="radio" id="ITSM_table_13" value="serverAdministration_0" checked="CHECKED"> 0</label></td> <td><label> <input type="radio" name="ITSMgrp3" value="serverAdministration_1" id="ITSM_table_14"> 1</label></td> <td><label> <input type="radio" name="ITSMgrp3" value="serverAdministration_2" id="ITSM_table_15"> 2</label></td> <td><label> <input type="radio" name="ITSMgrp3" value="serverAdministration_3" id="ITSM_table_16"> 3</label></td> <td><label> <input type="radio" name="ITSMgrp3" value="serverAdministration_4" id="ITSM_table_17"> 4</label></td> <td><input type="radio" name="ITSMgrp3" value="serverAdministration_5" id="ITSM_table_18"> 5</td> </tr> <tr id="ITSMrow4"> <td><div align="left">Database Administration & Management</div></td> <td height="24"><label> <input name="ITSMgrp4" type="radio" id="ITSM_table_19" value="DatabaseAdministration&Management_0" checked="CHECKED"> 0</label></td> <td><label> <input type="radio" name="ITSMgrp4" value="DatabaseAdministration&Management_1" id="ITSM_table_20"> 1</label></td> <td><label> <input type="radio" name="ITSMgrp4" value="DatabaseAdministration&Management_2" id="ITSM_table_21"> 2</label></td> <td><label> <input type="radio" name="ITSMgrp4" value="DatabaseAdministration&Management_3" id="ITSM_table_22"> 3</label></td> <td><label> <input type="radio" name="ITSMgrp4" value="DatabaseAdministration&Management_4" id="ITSM_table_23"> 4</label></td> <td><input type="radio" name="ITSMgrp4" value="DatabaseAdministration&Management_5" id="ITSM_table_24"> 5</td> </tr> <tr id="ITSMrow5"> <td><div align="left">Internet of Things App Development</div></td> <td height="24"><label> <input name="ITSMgrp5" type="radio" id="ITSM_table_25" value="InternetofThingsAppDevelopment_0" checked="CHECKED"> 0</label></td> <td><label> <input type="radio" name="ITSMgrp5" value="InternetofThingsAppDevelopment_1" id="ITSM_table_26"> 1</label></td> <td><label> <input type="radio" name="ITSMgrp5" value="InternetofThingsAppDevelopment_2" id="ITSM_table_27"> 2</label></td> <td><label> <input type="radio" name="ITSMgrp5" value="InternetofThingsAppDevelopment_3" id="ITSM_table_28"> 3</label></td> <td><label> <input type="radio" name="ITSMgrp5" value="InternetofThingsAppDevelopment_4" id="ITSM_table_29"> 4</label></td> <td><input type="radio" name="ITSMgrp5" value="InternetofThingsAppDevelopment_5" id="ITSM_table_30"> 5</td> </tr> </table></th> </tr> <tr> <th scope="row"></th> <th scope="row"> <input type="submit" name="submit" id="submit" value="Submit"></th> </tr> </table> </fieldset> </form> <form action="" method="post" name="LogOutForm" > <a href="<?php echo $logoutAction ?>" >LOG OUT</a> </form> </aside> </div> <footer id="footer"> <p>Footer goes here.</p> </footer> </div> </body> </html> I have to add in that I'm showing the radio buttons of skillsets based on the specialization(radio buttons)[look at the javascript]. I would like to know that if this is the right way, as i compile this from open source and in the correct format. I'm slow in learning , so i hope you can tell me in detail as to why my work hasnt been working the way i wanted to. I have set checked to every of the radio buttons in skillsets as i know that radio buttons are special cases where the form will still submit if the user didn't press it, and my database i've declared it as varchar, but I'm still wondering why would it be integrity constraint? Is it because i declared it wrongly? Im so sorry if my coding is too long( also because of the commented trial and error code) >< Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted July 2, 2015 Share Posted July 2, 2015 there are two main problems with the php code - Ā 1) it has logic problems and code that isn't using the correct variables/values/database fields. Ā 2) it is repetitive, resulting in a wall of code that makes it hard to even understand what the code is actually doing. your code tells us a story, the story of what you are trying to accomplish. if we cannot deduce from reading your code what it is actually doing, we cannot help with it. Ā for item #1 - Ā 1) the form processing code requires that the current visitor be logged in. therefore, the php code should not do anything unless there is a logged in visitor. the form page also requires that the current visitor be logged in and should not display the form unless there is a logged in visitor. Ā 2) the php code that you posted is there to process the form submission. therefore, it should be inside of a conditional statement testing if the form was submitted. the code to test if a record already exists should be inside of the form processing conditional statement. Ā 3) the userProfile table should use the user_id, not the username to associate the data in it with the user. this will simplify the SELECT query. then, just use $_SESSION['user_id'] to find if there is already a row in the userProfile table. Ā 4) the SELECT query does not contain any bound input parameter, so, there no point in using a prepared query. Ā 5) since there's no :id placeholder in the SELECT query, either the bindvalue() or the execute() statement are throwing errors for the current code. have you set the PDO error handling to use exceptions? you would be getting an uncaught exception to alert you to problems with the database statements (and you can then add try/catch logic in your code to handle database errors all in one place.) using exceptions will mean that you don't need to have conditional logic around every database statement (btw - the one place you are testing if a database statement failed, the first prepare(), the error message indicates that the there was an error executing the query. the prepare() statement doesn't execute the query, only prepares it.) Ā 6) you are setting the $username variable to a 1. even if your SELECT query was using a bound input parameter/value correctly, it is an id and the variable you are using should be named to match what is being used and the value should come from the correct place, $_SESSION['user_id'], not a fixed value in your code. Ā 7) your insert query for the userProfile table, doesn't contain a username field, so either your SELECT query is wrong or your INSERT query is wrong. the UPDATE query for the picName is also trying to use WHERE id = $_SESSION['user_id']. the id won't be the user's id. the id will actually be the $newID value. Ā $statement->fetchColumn() fetches the first column from the SELECT query. however, your query is not selecting a value that indicates how many rows the query matched, so the logic using the fetchColumn() value doesn't mean anything. for this to work, you would need to SELECT COUNT(*) in the query. this is one of the reasons why your code is ignoring the select query. you are also testing if the value is > 1. the test should be if it is > 0 to find if there already a row in the database table. Ā 9) you should trim() all the form data at once, so that you don't have to keep repeating statements in the code using the values. Ā 10) you shouldn't exit; after any of the errors have been detected. you want the code to finish so that it can display the error messages. Ā 11) $errors[] = ("some message!"); the () are not needed and in fact cause php to do extra work (evaluating an expression) and add clutter to the code. Ā 12) you have some statements starting at line 610 that don't have a query they belong with. why are those lines in your code? Ā 13) for the header() statements on lines 625 and 627, there is no $returnURL variable in the code, so there's no place for those redirects to go to and if you were redirecting to somewhere, how would you be able to print the errors that in the $errors array? Ā 14) i didn't specifically look for any problems in the all the radio button code. see the next item - Ā for item #2 (while this sounds like advanced programming, after you do it once, you can reuse it and it will simplify all the form/form-processing code that you write) - Ā when you have code (including html for form fields) that gets repeated over and over, where the only thing that changes are the values that get operated on, you need to use a different programming technique. instead of writing out all the possible lines of code yourself, you need to let php dynamically process data or dynamically produce html content. Ā when you have a form and form processing code, you can create an array that defines everything about the form fields and the validation you want for each field. then, to produce the form or validate the form data, you just loop over the array that defines everything to tell just one instance of the code what to do. Ā some of the things you need to define in the array to produce the form are - Ā 1) field type - text, radio, ... 2) field name (using the field name, which must be unique anyway, as the array index/key is helpful) 3) a display legend Ā in the case of checkbox/radio buttons, you can use an array within the array to define the information for each set of checkbox/radio buttons. Ā some of the things you need to define in the array to process the form data are validation rules, such as required - i.e. not empty, format functions/parameters - i.e. the filter_var() function, with the FILTER_VALIDATE_EMAIL parameter for the email field or the strlen function and a minimum/maximum value for fields that must meet a certain length, and error message text for each validation rule. Quote Link to comment Share on other sites More sharing options...
beginnerProgrammer_96 Posted July 2, 2015 Author Share Posted July 2, 2015 Thank you for your detailed explanation mac_gyver. I will get back to you after processing your explanation since my code is not saved in my current desktop i'm using. I would like to clarify about some things that you said. For #item 1: 1)I do have the login session for the user, but i didn't put that in here since the coding was already long and i was just wondering about the error i've gotten. Either way, i will post my code once i get to my other desktop since it's late here. 2)Yes, this form was actually in the request==$_POST (code line 32). The select statement before it was to check if the user has already have data in the database since this profile is limit to 1 per user. I'm not entirely sure as to what i should do as i wanted the user to be prompt an error message of some sort if the user landed on this add profile page when he/she already has a data inside. 3)Okay thanks, i had my database set to my username as the primary key >< i will take note of this. 4) Okay, i might have been confused with this. Am i right to say that select statement can be used as a normal query(?) because since there's no input, no one can hack (sql injection)? 5)Ahh..so that was why it didn't show the error in the first place! Thanks about this my database does have an error handling,but no try/catch. I will correct it tmr. 6)I initially thought i could just declare my $username because that was my primary key. Based on [3)], this also caused the problem. 7)I'm confused about this one. Basically, the user are not supposed to change the username, thus i leave that field out(even the form i did not declare this). The UPDATE query will have to be check tmr as I too am not sure about this one ><. 8)I see, thank you for pointing this out. So to clarify, fetchColumn does not take the column of the "WHERE....", just the first column? 9) May i ask as to where have i repeated the values? In this case, is it the radio group? 10)Okay, i'll take note of this too ^^ 11)Sorry, my bad. I used to do those brackets because it was a habit that my teacher used to say so that it will look neater in a way. I'll take note of this too! 12)I'm sorry, this was taken from an open source, but isn't the variables declared in?(line 605) For the username, it was my mistake for the primary key to be a username. 13)Thank you about that, i've forgotten to declare it >< 14)Okay, i will implement that array. Just to clarify, for this array, is it suppose to be all form fields? I have declare some error message starting from line 70.. Anyways, are the values the one that will be inserted to database for the radio buttons? I do want the radio button's value, but having a long value is quite troublesome o.o Ā Thank you once again, i'm really grateful for your help i will let you know once i've changed my codes [One last noob question: Is it better to have php codes in different pages than the html page itself or all in one page? I am writing all in one page( except for the database.php) and i'm abit confused if i were to seperate them, do i need to declare the variables again or do i just need to include the file?] Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted July 2, 2015 Share Posted July 2, 2015 (edited) as far as a user that already has a profile landing on this page, the same code can be used to edit an existing profile, by retrieving the existing data (see the get method/display code @line 57 in the following.) Ā example code that addresses or shows how it would look for the item#1 list - <?php session_start(); // access security - must be logged in to access this page if(!isset($_SESSION['user_id'])){ Ā Ā Ā // either redirect to somewhere else or output a message Ā Ā Ā header('location:your_login_page_for_example'); Ā Ā Ā exit; // prevent the rest of the code from running } $addProfileForm = htmlentities($_SERVER['PHP_SELF']); // form action try{ // start of database dependent code Ā Ā Ā require('Connections/database.php'); // assuming that both the get method/display code (form) and form processing need a database connection Ā Ā Ā // post method form processing Ā Ā Ā if ($_SERVER['REQUEST_METHOD'] == 'POST') { Ā Ā Ā Ā Ā Ā Ā $errors = array(); // array to hold errors Ā Ā Ā Ā Ā Ā Ā $data = array_map('trim',$_POST); // copy and trim all post data (if the form has any arrays, you need to use a function like array_walk_recursive() instead of array_map()) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // if you have multiple-forms, you would conditionally run the correct section of form processing code by testing for a form field or a field value that uniquely identifies which form was submitted Ā Ā Ā Ā Ā Ā Ā // addprofileform form processing code - Ā Ā Ā Ā Ā Ā Ā //check if there is an existing record in DB Ā Ā Ā Ā Ā Ā Ā $query = "SELECT COUNT(*) FROM userProfile WHERE user_id = {$_SESSION['user_id']}"; Ā Ā Ā Ā Ā Ā Ā $stmt = $database->query($query); Ā Ā Ā Ā Ā Ā Ā $number_of_rows = $stmt->fetchColumn(); Ā Ā Ā Ā Ā Ā Ā if ($number_of_rows > 0) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā $errors[] = "There is an existing record. You cannot insert another profile! Either update the old one, or delete to insert again."; Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā if(empty($errors)){ // if no errors at this point, continue with validation Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // all your form field validation code would go here, using elements in the $data array (a trimmed copy of the $_POST data) Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // done with validation, if no errors, use the data - Ā Ā Ā Ā Ā Ā Ā if(empty($errors)){ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // your code to insert the data and move the uploaded file would go here... Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // if no errors at this point, redirect to the exact same url of this page to prevent the browser from resubmitting the data by causing a get request for the page Ā Ā Ā Ā Ā Ā Ā if(empty($errors)){ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā $host = $_SERVER['HTTP_HOST']; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā $uri = $_SERVER['REQUEST_URI']; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā header("Location: http://$host$uri"); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā exit; Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā // if there were errors in any of the above form processing code, continue on this page, displaying any errors, redisplay form, (re)populate the form fields with data... Ā Ā Ā } // end of post method form processing Ā Ā Ā Ā Ā Ā // get method/display code (if any) - get/produce data that's needed to display the page Ā Ā Ā // to edit existing data, if the $data array is empty at this point, retrieve any existing data from the database table } catch(PDOException $e){ // end of database dependent code, handle any errors Ā Ā Ā $status = empty($query) ? 'Connection failed':" Query failed: $query"; Ā Ā Ā // application message Ā Ā Ā trigger_error("$status, Error: {$e->getMessage()}, File: {$e->getFile()}, Line: {$e->getLine()}"); Ā Ā Ā // user message Ā Ā Ā $errors[] = 'Sorry, this page is not working at this time.'; } // done with the database, destroy any pdostatment resource and close connection $stmt = null; $database = null; // the html document that uses any data from the above code starts here - ?>i'll get back to you on any questions you posted in your reply above, that don't seem to be addressed by this example code. Edited July 2, 2015 by mac_gyver Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted July 2, 2015 Share Posted July 2, 2015 here's the answers to your questions - Ā 7)I'm confused about this one. Basically, the user are not supposed to change the username, thus i leave that field out(even the form i did not declare this). The UPDATE query will have to be check tmr as I too am not sure about this one ><.Ā my comment was about the mismatches between the queries, as i was coming across them in the code. Ā the user id is defined in your users table (according to the sub-query you show in your original code). when you insert the row in the userProfile table, you need to have a column for the user_id, that gets the value from $_SESSION['user_id']. this is how the row in the userProfie table gets (should be) associated to the user's row in the users table (i didn't ask, but why two tables and not just have everything in the users table?)for the update query (you actually don't need to do this, you can just insert the extension of the image, since you WILL know the filename part from the id. but, if you do want to do the update, the id of the row just inserted in the userProfile table is the auto-increment id that was just assigned when that query ran. it's not the user_id.Ā after you add a user_id column to the userProfile table, you could update the row using WHERE user_id = $_SESSION['user_id'], but that's not what you have now in the code.Ā 9) May i ask as to where have i repeated the values? In this case, is it the radio group?Ā that referred to things like - if ((trim($contactNo)) == "") { in one place, and if(strlen(trim($contactNo)) != { a few lines later. in a DRY (Don't Repeat Yourself) mode, you wouldn't trim the value each place you test it and in fact since the value being inserted is not trimmed, you can end up with searches not being able to find stored values since the values can contain non-printing characters stored before/after them. trim (all) the values just once, so that every place you reference or use any of them will be the same value. Ā 12)I'm sorry, this was taken from an open source, but isn't the variables declared in?(line 605) For the username, it was my mistake for the primary key to be a username.Ā there's no query statement that these things go with. you already executed the INSERT statement. they are some kind of copy/paste fail.Ā 14)Okay, i will implement that array. Just to clarify, for this array, is it suppose to be all form fields? I have declare some error message starting from line 70.. Anyways, are the values the one that will be inserted to database for the radio buttons? I do want the radio button's value, but having a long value is quite troublesome o.oĀ the defining array would be for all the form fields, even the submit button and any hidden fields..., though you can limit it to just the fields that will be submitting data value that will get used by the code.Ā Ā One last noob question: Is it better to have php codes in different pages than the html page itself or all in one page? I am writing all in one page( except for the database.php) and i'm abit confused if i were to seperate them, do i need to declare the variables again or do i just need to include the file?Ā it's better to have one page (though you can break it up and have the form processing code in an included file and the form in an included file. having one page means that the form and the form processing code is in the same program scope, so that redisplaying the values already entered in the form fields, should there be a validation error, is easy.Ā Quote Link to comment Share on other sites More sharing options...
beginnerProgrammer_96 Posted July 3, 2015 Author Share Posted July 3, 2015 Alright, i'm processing what you say as i move along with my codes. Currently,I have made changes to my database for the id to be the primary key. This is my database.php file: <?php //Use PDO statement try { $user = "root"; $pass = ""; $database = new PDO('mysql:host=localhost;dbname=final_year_project_1;charset=utf8', $user, $pass, array(PDO::ATTR_EMULATE_PREPARES => false,PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); } catch (PDOException $ex) { echo 'Connection failed: ' . $ex->getMessage(); } ?> And the user login session/ logout session(the very top of my page before i put all the other codings) : <?php //initialize the session or import previous session session_start(); if(!isset($_SESSION['user_id']) || !isset($_SESSION['logged_in'])){ //User not logged in. Redirect them back to the login.php page. header('Location:AccountLogin.php'); exit; } else { $now = time(); // Checking the time now when home page starts. if ($now > $_SESSION['expire']) { session_destroy(); echo "<div style=\"color: red;\">Your session has expired!<br><a href = 'AccountLogin.php'>Login here</a></div>"; } } // else { // //if exist, show welcome thing // echo ("User: ".($_SESSION['MM_Username'])." ".($_SESSION['MM_UserGroup']). " has login successfully."); // } // ** Logout the current user. ** $logoutAction = $_SERVER['PHP_SELF']."?doLogout=true"; if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){ $logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']); } if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){ //to fully log out a visitor we need to clear the session varialbles $_SESSION['user_id'] = NULL; $_SESSION['logged_in'] = NULL; $_SESSION['PrevUrl'] = NULL; unset($_SESSION['user_id']); unset($_SESSION['logged_in']); unset($_SESSION['PrevUrl']); $logoutGoTo = "AccountLogin.php"; if ($logoutGoTo) { header("Location: $logoutGoTo"); exit; die; } } ?> Thanks again for your explanation. I will work on my code now and will update you what i've progressed soon. Quote Link to comment Share on other sites More sharing options...
beginnerProgrammer_96 Posted July 3, 2015 Author Share Posted July 3, 2015 Regarding this select query from your reply #4: Ā Ā Ā Ā Ā Ā Ā // addprofileform form processing code - Ā Ā Ā Ā Ā Ā Ā //check if there is an existing record in DB Ā Ā Ā Ā Ā Ā Ā $query = "SELECT COUNT(*) FROM userProfile WHERE user_id = {$_SESSION['user_id']}"; Ā Ā Ā Ā Ā Ā Ā I'm again confused myself. I have 2 tables actually, user( for login purposes), and userProfile(for the user profile). I have change my userProfile's primary key to be userProfileId, but how can this help to link to the session of my login? As far as i know, the id is auto incremented, so how can you tell if userProfileId is actually equal to the user_id of that particular user? They can be of different person, am i wrong? (This is probably also why i use username to identify). My user table has (id, name, email, username, password) while my userProfile has (userProfileId, username,picName, picPath, name, introduction,specialization, skillset, email, contactNo). Quote Link to comment Share on other sites More sharing options...
Barand Posted July 3, 2015 Share Posted July 3, 2015 As you have already been told, you would need to store the user table's id in the profile so the correct profile record can be associated with its user +----------+ +----------------+ | user | | user_profile | +----------+ +----------------+ | id |---+ | userProfileId | | name | | | username | | email | | | picName | | username | | | picPath | | password | | | name | +----------+ | | introduction | | | specialization | | | skillset | | | email | | | contactNo | +---| user_id | +----------------+ Quote Link to comment Share on other sites More sharing options...
beginnerProgrammer_96 Posted July 3, 2015 Author Share Posted July 3, 2015 Ā As you have already been told, you would need to store the user table's id in the profile so the correct profile record can be associated with its user +----------+ +----------------+ | user | | user_profile | +----------+ +----------------+ | id |---+ | userProfileId | | name | | | username | | email | | | picName | | username | | | picPath | | password | | | name | +----------+ | | introduction | | | specialization | | | skillset | | | email | | | contactNo | +---| user_id | +----------------+ Ah, thank you so much for this. I have a better picture on what's going on. It was stupid of me of not putting relations inside my database, sorry about that .-. Quote Link to comment Share on other sites More sharing options...
beginnerProgrammer_96 Posted July 6, 2015 Author Share Posted July 6, 2015 Ā Ā Ā // post method form processing Ā Ā Ā if ($_SERVER['REQUEST_METHOD'] == 'POST') { Ā Ā Ā Ā Ā Ā Ā $errors = array(); // array to hold errors Ā Ā Ā Ā Ā Ā Ā $data = array_map('trim',$_POST); // copy and trim all post data (if the form has any arrays, you need to use a function like array_walk_recursive() instead of array_map()) Ā Ā Ā Ā Ā Ā Ā Ā Pardon for my lack of knowledge( and my learning pace), but i'm still stuck on this array_map/array_walk_recursive(). i've went to the manual and even some of the forums but im still clueless as ever. Most of them uses array and declare the values, but how can i 'know' what the user will put in as value? I've been trial and error and it doesn't work.. Here are my trials: if ($_SERVER['REQUEST_METHOD'] == 'POST') { // array to hold errors $errors = array(); // copy and trim all post data (if the form has any arrays, you need to use a function like array_walk_recursive() instead of array_map()) //variables here $data = array_map('trim',$_POST); $data = array_map('trim',$_POST['name']); $data = array_map('trim',$_POST['description']); $data = array_map('trim',$_POST['email']); $data = array_map('trim',$_POST['contactNo']); $selectedRBofISrow1= $_POST['ISgrp1']; $selectedRBofISrow2= $_POST['ISgrp2']; $selectedRBofISrow3= $_POST['ISgrp3']; $selectedRBofISrow4= $_POST['ISgrp4']; $selectedRBofISrow5= $_POST['ISgrp5']; $selectedRBofIMrow1= $_POST['IMgrp1']; $selectedRBofIMrow2= $_POST['IMgrp2']; $selectedRBofIMrow3= $_POST['IMgrp3']; $selectedRBofIMrow4= $_POST['IMgrp4']; $selectedRBofIMrow5= $_POST['IMgrp5']; $selectedRBofCNETrow1= $_POST['CNETgrp1']; $selectedRBofCNETrow2= $_POST['CNETgrp2']; $selectedRBofCNETrow3= $_POST['CNETgrp3']; $selectedRBofCNETrow4= $_POST['CNETgrp4']; $selectedRBofCNETrow5= $_POST['CNETgrp5']; $selectedRBofITSMrow1= $_POST['ITSMgrp1']; $selectedRBofITSMrow2= $_POST['ITSMgrp2']; $selectedRBofITSMrow3= $_POST['ITSMgrp3']; $selectedRBofITSMrow4= $_POST['ITSMgrp4']; $selectedRBofITSMrow5= $_POST['ITSMgrp5']; array_walk_recursive($selectedRBofISrow1,"arrayValidate"); array_walk_recursive($selectedRBofISrow2,"arrayValidate"); array_walk_recursive($selectedRBofISrow3,"arrayValidate"); array_walk_recursive($selectedRBofISrow4,"arrayValidate"); array_walk_recursive($selectedRBofISrow5,"arrayValidate"); //$dataArray = array_walk_recursive('trim',$_POST); //$items = array(); foreach($key as $value) { $data[] = $name; $data[] = $description; $data[] = $email; $data[] = $contactNo; } print_r($data); //print_r($dataArray); //function validateTB() { // $name = array_map('trim', $_POST['name']); // $description = array_map('trim', $_POST['description']); // $email = array_map('trim', $_POST['email']); // $contactNo = array_map('trim', $_POST['contactNo']); // } // // $specialization = $_POST['specialization']; // $selectedRBofISrow1 = $_POST['ISgrp1']; // $selectedRBofISrow2 = $_POST['ISgrp2']; // $selectedRBofISrow3 = $_POST['ISgrp3']; // $selectedRBofISrow4 = $_POST['ISgrp4']; // $selectedRBofISrow5 = $_POST['ISgrp5']; // $selectedRBofIMrow1 = $_POST['IMgrp1']; // $selectedRBofIMrow2 = $_POST['IMgrp2']; // $selectedRBofIMrow3 = $_POST['IMgrp3']; // $selectedRBofIMrow4 = $_POST['IMgrp4']; // $selectedRBofIMrow5 = $_POST['IMgrp5']; // $selectedRBofCNETrow1 = $_POST['CNETgrp1']; // $selectedRBofCNETrow2 = $_POST['CNETgrp2']; // $selectedRBofCNETrow3 = $_POST['CNETgrp3']; // $selectedRBofCNETrow4 = $_POST['CNETgrp4']; // $selectedRBofCNETrow5 = $_POST['CNETgrp5']; // $selectedRBofITSMrow1 = $_POST['ITSMgrp1']; // $selectedRBofITSMrow2 = $_POST['ITSMgrp2']; // $selectedRBofITSMrow3 = $_POST['ITSMgrp3']; // $selectedRBofITSMrow4 = $_POST['ITSMgrp4']; // $selectedRBofITSMrow5 = $_POST['ITSMgrp5']; // $skillset = ""; // // // function arrayValidate($value,$key){ // $key = array_walk_recursive('trim', $value); // } // $selectedRBofISrow1=array("0"=>"C#-0","1"=>"C#-1","2"=>"C#-2","3"=>"C#-3","4"=>"C#-4","5"=>"C#-5"); // $selectedRBofISrow2=array("0"=>"PHP/SQL-0","1"=>"PHP/SQL-1","2"=>"PHP/SQL-2","3"=>"PHP/SQL-3","4"=>"PHP/SQL-4","5"=>"PHP/SQL-5"); // $selectedRBofISrow3=array("0"=>"HTML/CSS-0","1"=>"HTML/CSS-1","2"=>"HTML/CSS-2","3"=>"HTML/CSS-3","4"=>"HTML/CSS-4","5"=>"HTML/CSS-5"); // $selectedRBofISrow4=array("0"=>"Java Enterprise Development-0","1"=>"Java Enterprise Development-1","2"=>"Java Enterprise Development-2","3"=>"Java Enterprise Development-3","4"=>"Java Enterprise Development-4","5"=>"Java Enterprise Development-5"); // $selectedRBofISrow5=array("0"=>"Mobile Application Development-0","1"=>"Mobile Application Development-1","2"=>"Mobile Application Development-2","3"=>"Mobile Application Development-3","4"=>"Mobile Application Development-4","5"=>"Mobile Application Development-5"); // $selectedRBofISrow1=array("0"=>"Web Design-0","1"=>"Web Design-1","2"=>"Web Design-2","3"=>"Web Design-3","4"=>"Web Design-4","5"=>"Web Design-5"); // $selectedRBofIMrow2=array("0"=>"3D Modelling & Animations-0","1"=>"3D Modelling & Animations-1","2"=>"3D Modelling & Animations-2","3"=>"3D Modelling & Animations-3","4"=>"3D Modelling & Animations-4","5"=>"3D Modelling & Animations-5"); // $selectedRBofIMrow3=array("0"=>"PHP/SQL-0","1"=>"C#-1","2"=>"C#-2","3"=>"C#-3","4"=>"C#-4","5"=>"C#-5"); // $selectedRBofIMrow4=array("0"=>"PHP/SQL-0","1"=>"C#-1","2"=>"C#-2","3"=>"C#-3","4"=>"C#-4","5"=>"C#-5"); // $selectedRBofIMrow5=array("0"=>"PHP/SQL-0","1"=>"C#-1","2"=>"C#-2","3"=>"C#-3","4"=>"C#-4","5"=>"C#-5"); // $selectedRBofIMrow1=array("0"=>"PHP/SQL-0","1"=>"C#-1","2"=>"C#-2","3"=>"C#-3","4"=>"C#-4","5"=>"C#-5"); // $selectedRBofCNETrow2=array("0"=>"PHP/SQL-0","1"=>"C#-1","2"=>"C#-2","3"=>"C#-3","4"=>"C#-4","5"=>"C#-5"); // $selectedRBofCNETrow3=array("0"=>"PHP/SQL-0","1"=>"C#-1","2"=>"C#-2","3"=>"C#-3","4"=>"C#-4","5"=>"C#-5"); // $selectedRBofCNETrow4=array("0"=>"PHP/SQL-0","1"=>"C#-1","2"=>"C#-2","3"=>"C#-3","4"=>"C#-4","5"=>"C#-5"); // $selectedRBofCNETrow5=array("0"=>"PHP/SQL-0","1"=>"C#-1","2"=>"C#-2","3"=>"C#-3","4"=>"C#-4","5"=>"C#-5"); // $selectedRBofCNETrow1=array("0"=>"PHP/SQL-0","1"=>"C#-1","2"=>"C#-2","3"=>"C#-3","4"=>"C#-4","5"=>"C#-5"); // $selectedRBofITSMrow2=array("0"=>"PHP/SQL-0","1"=>"C#-1","2"=>"C#-2","3"=>"C#-3","4"=>"C#-4","5"=>"C#-5"); // $selectedRBofITSMrow3=array("0"=>"PHP/SQL-0","1"=>"C#-1","2"=>"C#-2","3"=>"C#-3","4"=>"C#-4","5"=>"C#-5"); // $selectedRBofITSMrow4=array("0"=>"PHP/SQL-0","1"=>"C#-1","2"=>"C#-2","3"=>"C#-3","4"=>"C#-4","5"=>"C#-5"); // $selectedRBofITSMrow5=array("0"=>"PHP/SQL-0","1"=>"C#-1","2"=>"C#-2","3"=>"C#-3","4"=>"C#-4","5"=>"C#-5"); // array_walk_recursive($selectedRBofISrow1,arrayValidate); // array_walk_recursive($selectedRBofISrow2,arrayValidate); // array_walk_recursive($selectedRBofISrow3,arrayValidate); // array_walk_recursive($selectedRBofISrow4,arrayValidate); // array_walk_recursive($selectedRBofISrow5,arrayValidate); // // array_walk_recursive($selectedRBofISrow1,arrayValidate); // array_walk_recursive($selectedRBofISrow2,arrayValidate); // array_walk_recursive($selectedRBofISrow3,arrayValidate); // array_walk_recursive($selectedRBofISrow4,arrayValidate); // array_walk_recursive($selectedRBofISrow5,arrayValidate); // // array_walk_recursive($selectedRBofISrow1,arrayValidate); // array_walk_recursive($selectedRBofISrow2,arrayValidate); // array_walk_recursive($selectedRBofISrow3,arrayValidate); // array_walk_recursive($selectedRBofISrow4,arrayValidate); // array_walk_recursive($selectedRBofISrow5,arrayValidate); // // array_walk_recursive($selectedRBofISrow1,arrayValidate); // array_walk_recursive($selectedRBofISrow2,arrayValidate); // array_walk_recursive($selectedRBofISrow3,arrayValidate); // array_walk_recursive($selectedRBofISrow4,arrayValidate); // array_walk_recursive($selectedRBofISrow5,arrayValidate); Did a few array on the some of the rows but i became confused.. Again, sorry if i took too long than i shouldĀ Ā Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted July 6, 2015 Share Posted July 6, 2015 (edited) i'm pretty sure that all of your form field names are not arrays (though the radio buttons should be) so, just consider $data = array_map('trim',$_POST); for now. Ā that line of code assigns the result of the array_map() function to the $data variable. the array_map() function applies the php trim() function to all elements of the $_POST array, which is an array consisting of the submitted form data. $data will be a copy of the submitted form data, that's been trimmed. Ā to reference those trimmed values in your code, you would use things like - $data['name'].Ā ultimately, your code would dynamically reference the values in $data, rather than you writing out a bunch of lines of code for each of them. the point of operating on the data as an array, using php's array functions (array_map, foreach, ...), is so that you don't have to write out lines of code for each and every form field. if you have 20 form fields, the one line $data = array_map('trim',$_POST); trims all the values at once, rather than you witting out $some_variable = trim($_POST['some_filed']) 20 times. Ā besides using $data as the source data for your validation logic and source data for the database queries, you would also use it to repopulate the form field values/radio button selections should you redisplay the form if any validation errors are detected. Edited July 6, 2015 by mac_gyver Quote Link to comment Share on other sites More sharing options...
beginnerProgrammer_96 Posted July 6, 2015 Author Share Posted July 6, 2015 Okay, i misinterpret before that i thought you said i should loop all the inputs inside the for each function. Okay so is this what you meant: $data = array_map('trim',$_POST); $name = $data[$name]; //= $_POST['name']; $description = $data[$description];// = $_POST['description']; $email = $data[$email]; // = $_POST['email']; $contactNo = $data[$contactNo]; // = $_POST['contactNo']; $dataArray = array_walk_recursive('trim', $_POST); $selectedRBofISrow1= $dataArray['ISGrp1']; //$_POST['ISgrp1']; I don't need to declare the $_POST? (Because i have put the name in the form as []). So, when i need to reference them , i just had to : if ($data['name'] == "") { $errors[] = ("Name is required!"); } could you explain me more on the array_walk_recursive? I'm not sure how to input the values in >< Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted July 6, 2015 Share Posted July 6, 2015 the following is from the sample code i posted - Ā Ā Ā Ā Ā Ā Ā if(empty($errors)){ // if no errors at this point, continue with validation Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // all your form field validation code would go here, using elements in the $data array (a trimmed copy of the $_POST data) Ā Ā Ā Ā Ā Ā Ā } the only thing you should be adding is 'your form field validation code". this is the code that's testing if the submitted form data is empty or not, if it has the correct format, and any value range checking. all the lines of code you have shown in post #10, beyond the original array_map(), and the first half of post #12 are not needed. Ā the code you posted in the second half of post #12 is how you would reference the now trimmed data that's in the $data variable. my suggestion that you can define the information about the form fields in an array and then loop over this defining array to build the form and process the form data is going to be way over your head at this point. just change your validation logic to use elements in the $data array - $data['name'], $data['description'], ... Quote Link to comment Share on other sites More sharing options...
beginnerProgrammer_96 Posted July 7, 2015 Author Share Posted July 7, 2015 Okay, thanks for your help! I've changed the codings to these: if ($_SERVER['REQUEST_METHOD'] == 'POST') { // array to hold errors $errors = array(); // copy and trim all post data (if the form has any arrays, you need to use a function like array_walk_recursive() instead of array_map()) //variables here $data= array();//array_map('trim',$_POST); array_walk_recursive($_POST,'trim'); // if you have multiple-forms, you would conditionally run the correct section of form processing code by testing for a form field or a field value that uniquely identifies which form was submitted // addprofileform form processing code - //check if there is an existing record in DB $query = "SELECT COUNT(*) FROM userProfile WHERE user_id = {$_SESSION['user_id']}"; var_dump($query); $stmt = $database->query($query); $number_of_rows = $stmt->fetchColumn(); if ($number_of_rows > 0) { $errors[] = "There is an existing record. You cannot insert another profile! Either update the old one, or delete to insert again."; } // if no errors at this point, continue with validation if(empty($errors)){ // all your form field validation code would go here, using elements in the $data array (a trimmed copy of the $_POST data) //validate here after declaring them //foreach ($_POST['specialization'] as $specialization) { // do stuff with $answerNum and $answer //} //validate the form inputs if ($data['name'] == "") { $errors[] = ("Name is required!"); } //check if introduction is blank if ($data['introduction'] == "") { $errors[] = ("Introduction is required!"); } //check if introduction is blank if ($data['email'] == "") { $errors[] = ("Email is required!"); } //check if contactNo is blank if ($data['contactNo'] == "") { $errors[] = ("Contact number is required!"); } //check if emailis valid if (filter_var($data['email'], FILTER_VALIDATE_EMAIL) === false) { $errors[] = ("Email is not valid."); } //validate if name is maxed number if (strlen($data['name']) > 50) { $errors[] = ("Name is too long!"); } //validate if name is maxed number if (strlen($data['introduction']) > 50) { $errors[] = ("Introduction is too long!"); } //validate if name is maxed number if (strlen($$data['email']) > 50) { $errors[] = ("Email is too long!"); } //check if contactNo is valid //checks for numeric, then check for 8 digits if(!is_numeric ($data['contactNo'])) { $errors[] = ("Contact number is invalid! All must be in numerics!"); } if(strlen(trim($data['contactNo'])) != { $errors[] = ("Contact number must be of 8 digits!"); } if (isset($data['specialization']) && $data['specialization'] == 'Specialization_1') { $data['specialization'] = "IS"; $skillset = ($selectedRBofISrow1.",".$selectedRBofISrow2.",".$selectedRBofISrow3.",".$selectedRBofISrow4.",".$selectedRBofISrow5); } if (isset($_POST['specialization']) && $_POST['specialization'] == 'Specialization_2') { $data['specialization'] = "IM"; $skillset = ($selectedRBofIMrow1.",".$selectedRBofIMrow2.",".$selectedRBofIMrow3.",".$selectedRBofIMrow4.",".$selectedRBofIMrow5); } if (isset($_POST['specialization']) && $_POST['specialization'] == 'Specialization_3') { $data['specialization']= "CNET"; $skillset = ($selectedRBofCNETrow1.",".$selectedRBofCNETrow2.",".$selectedRBofCNETrow3.",".$selectedRBofCNETrow4.",".$selectedRBofCNETrow5); } if (isset($_POST['specialization']) && $_POST['specialization'] == 'Specialization_4') { $data['specialization'] = "ITSM"; $skillset = ($selectedRBofITSMrow1.",".$selectedRBofITSMrow2.",".$selectedRBofITSMrow3.",".$selectedRBofITSMrow4.",".$selectedRBofITSMrow5); } //image $fileError = $_FILES['image']['error']; //validate if there's any error if($fileError > 0){ // header("Location:".$returnURL."?err=imgUploadError"); $errors[] = ("Error on uploading image!"); exit; } $data['maxSize'] = 100000; $fileSize = $_FILES['image']['size']; //validate image size if($fileSize > $data['maxSize']){ $errors[] = ("Image size is too big!"); // exit; } //get the temporary location of the file $fileTempName = $_FILES['image']['tmp_name']; $trueFileType = exif_imagetype($fileTempName); $allowedFiles = array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG); if (in_array($trueFileType, $allowedFiles)) { switch($trueFileType){ case 1 : $fileExt = ".gif"; break; case 2: $fileExt = ".jpg"; break; case 3 : $fileExt = ".png"; break; } }else{ //header("Location:".$returnURL."?err=WrongFileType"); $errors[] = ("The one you've uploaded is not supported! Please check that it is in the right format."); exit; } // Get the path to upload the image to $myPathInfo = pathinfo($_SERVER['DOCUMENT_ROOT'].$_SERVER['PHP_SELF']); $currentDir = $myPathInfo['dirname']; $imgDir = $currentDir . '/profilePic/'; } I've tried to submit using an existing profile and it does show the error from the select query. However, when i log in to another user(no profile), the error "Undefined index:"Ā for all the data[] is shown. I'm confused, hadn't i declare them? Or was that only validation? You pointed out that i shouldn't declare the variables on my post #12?Ā Quote Link to comment Share on other sites More sharing options...
beginnerProgrammer_96 Posted July 7, 2015 Author Share Posted July 7, 2015 Okay, thanks for your help! I've changed the codings to these: if ($_SERVER['REQUEST_METHOD'] == 'POST') { // array to hold errors $errors = array(); // copy and trim all post data (if the form has any arrays, you need to use a function like array_walk_recursive() instead of array_map()) //variables here $data= array();//array_map('trim',$_POST); array_walk_recursive($_POST,'trim'); // if you have multiple-forms, you would conditionally run the correct section of form processing code by testing for a form field or a field value that uniquely identifies which form was submitted // addprofileform form processing code - //check if there is an existing record in DB $query = "SELECT COUNT(*) FROM userProfile WHERE user_id = {$_SESSION['user_id']}"; var_dump($query); $stmt = $database->query($query); $number_of_rows = $stmt->fetchColumn(); if ($number_of_rows > 0) { $errors[] = "There is an existing record. You cannot insert another profile! Either update the old one, or delete to insert again."; } // if no errors at this point, continue with validation if(empty($errors)){ // all your form field validation code would go here, using elements in the $data array (a trimmed copy of the $_POST data) //validate here after declaring them //foreach ($_POST['specialization'] as $specialization) { // do stuff with $answerNum and $answer //} //validate the form inputs if ($data['name'] == "") { $errors[] = ("Name is required!"); } //check if introduction is blank if ($data['introduction'] == "") { $errors[] = ("Introduction is required!"); } //check if introduction is blank if ($data['email'] == "") { $errors[] = ("Email is required!"); } //check if contactNo is blank if ($data['contactNo'] == "") { $errors[] = ("Contact number is required!"); } //check if emailis valid if (filter_var($data['email'], FILTER_VALIDATE_EMAIL) === false) { $errors[] = ("Email is not valid."); } //validate if name is maxed number if (strlen($data['name']) > 50) { $errors[] = ("Name is too long!"); } //validate if name is maxed number if (strlen($data['introduction']) > 50) { $errors[] = ("Introduction is too long!"); } //validate if name is maxed number if (strlen($$data['email']) > 50) { $errors[] = ("Email is too long!"); } //check if contactNo is valid //checks for numeric, then check for 8 digits if(!is_numeric ($data['contactNo'])) { $errors[] = ("Contact number is invalid! All must be in numerics!"); } if(strlen(trim($data['contactNo'])) != { $errors[] = ("Contact number must be of 8 digits!"); } if (isset($data['specialization']) && $data['specialization'] == 'Specialization_1') { $data['specialization'] = "IS"; $skillset = ($selectedRBofISrow1.",".$selectedRBofISrow2.",".$selectedRBofISrow3.",".$selectedRBofISrow4.",".$selectedRBofISrow5); } if (isset($_POST['specialization']) && $_POST['specialization'] == 'Specialization_2') { $data['specialization'] = "IM"; $skillset = ($selectedRBofIMrow1.",".$selectedRBofIMrow2.",".$selectedRBofIMrow3.",".$selectedRBofIMrow4.",".$selectedRBofIMrow5); } if (isset($_POST['specialization']) && $_POST['specialization'] == 'Specialization_3') { $data['specialization']= "CNET"; $skillset = ($selectedRBofCNETrow1.",".$selectedRBofCNETrow2.",".$selectedRBofCNETrow3.",".$selectedRBofCNETrow4.",".$selectedRBofCNETrow5); } if (isset($_POST['specialization']) && $_POST['specialization'] == 'Specialization_4') { $data['specialization'] = "ITSM"; $skillset = ($selectedRBofITSMrow1.",".$selectedRBofITSMrow2.",".$selectedRBofITSMrow3.",".$selectedRBofITSMrow4.",".$selectedRBofITSMrow5); } //image $fileError = $_FILES['image']['error']; //validate if there's any error if($fileError > 0){ // header("Location:".$returnURL."?err=imgUploadError"); $errors[] = ("Error on uploading image!"); exit; } $data['maxSize'] = 100000; $fileSize = $_FILES['image']['size']; //validate image size if($fileSize > $data['maxSize']){ $errors[] = ("Image size is too big!"); // exit; } //get the temporary location of the file $fileTempName = $_FILES['image']['tmp_name']; $trueFileType = exif_imagetype($fileTempName); $allowedFiles = array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG); if (in_array($trueFileType, $allowedFiles)) { switch($trueFileType){ case 1 : $fileExt = ".gif"; break; case 2: $fileExt = ".jpg"; break; case 3 : $fileExt = ".png"; break; } }else{ //header("Location:".$returnURL."?err=WrongFileType"); $errors[] = ("The one you've uploaded is not supported! Please check that it is in the right format."); exit; } // Get the path to upload the image to $myPathInfo = pathinfo($_SERVER['DOCUMENT_ROOT'].$_SERVER['PHP_SELF']); $currentDir = $myPathInfo['dirname']; $imgDir = $currentDir . '/profilePic/'; } I've tried to submit using an existing profile and it does show the error from the select query. However, when i log in to another user(no profile), the error "Undefined index:"Ā for all the data[] is shown. I'm confused, hadn't i declare them? Or was that only validation? You pointed out that i shouldn't declare the variables on my post #12?Ā Sorry about that, i realised i do need to declare, just that not in the place you mentioned on your previous post. $data['name'] = $_POST['name']; $data['introduction'] = $_POST['description']; $data['email'] = $_POST['email']; $data['contactNo'] = $_POST['contactNo']; I've now gotten another error but i'm trying to fix it first before i post again(i want to learn too ><). Thanks for your help, i really really thankful and grateful having you helping me out ^^ Quote Link to comment Share on other sites More sharing options...
beginnerProgrammer_96 Posted July 7, 2015 Author Share Posted July 7, 2015 Okay, after a few hours trying to fix those errors, there's just this one error i just couldn't solve. The error was that I didn't define $skillset(It's the same problem i had at the very first part of this topic too). I declare this $skillset inside the if statement in validation, so i'm guessing that's why it won't read my variable? if(empty($errors)){ // all your form field validation code would go here, using elements in the $data array (a trimmed copy of the $_POST data) //validate here after declaring them $data['name'] = $_POST['name']; $data['introduction'] = $_POST['description']; $data['email'] = $_POST['email']; $data['contactNo'] = $_POST['contactNo']; $data['specialization'] = $_POST['specialization']; $data['ISGrp1'] = $_POST['ISgrp1']; $data['ISGrp2'] = $_POST['ISgrp2']; $data['ISGrp3'] = $_POST['ISgrp3']; $data['ISGrp4'] = $_POST['ISgrp4']; $data['ISGrp5'] = $_POST['ISgrp5']; $data['IMGrp1'] = $_POST['IMgrp1']; $data['IMGrp2'] = $_POST['IMgrp2']; $data['IMGrp3'] = $_POST['IMgrp3']; $data['IMGrp4'] = $_POST['IMgrp4']; $data['IMGrp5'] = $_POST['IMgrp5']; $data['CNETGrp1'] = $_POST['CNETgrp1']; $data['CNETGrp2'] = $_POST['CNETgrp2']; $data['CNETGrp3'] = $_POST['CNETgrp3']; $data['CNETGrp4'] = $_POST['CNETgrp4']; $data['CNETGrp5'] = $_POST['CNETgrp5']; $data['ITSMGrp1'] = $_POST['ITSMgrp1']; $data['ITSMGrp2'] = $_POST['ITSMgrp2']; $data['ITSMGrp3'] = $_POST['ITSMgrp3']; $data['ITSMGrp4'] = $_POST['ITSMgrp4']; $data['ITSMGrp5'] = $_POST['ITSMgrp5']; //check if name is blank if ($data['name'] == "" || !$data['name']) { $errors[] = ("Name is required!"); } //check if introduction is blank if ($data['introduction'] == "" ||!$data['introduction']) { $errors[] = ("Introduction is required!"); } //check if introduction is blank if ($data['email'] == "" || !$data['email']) { $errors[] = ("Email is required!"); } //check if contactNo is blank if ($data['contactNo'] == "" || !$data['contactNo']) { $errors[] = ("Contact number is required!"); } //check if emailis valid if (filter_var($data['email'], FILTER_VALIDATE_EMAIL) === false) { $errors[] = ("Email is not valid."); } //validate if name is maxed number if (isset($data['name']) && strlen($data['name']) > 50) { $errors[] = ("Name is too long!"); } //validate if name is maxed number if (isset( $data['introduction']) && strlen($data['introduction']) > 50) { $errors[] = ("Introduction is too long!"); } //validate if name is maxed number if (isset($data['email']) && strlen($data['email']) > 50) { $errors[] = ("Email is too long!"); } //check if contactNo is valid //checks for numeric, then check for 8 digits if(isset($data['contactNo']) && !is_numeric ($data['contactNo'])) { $errors[] = ("Contact number is invalid! All must be in numerics!"); } if(strlen(trim($data['contactNo'])) != { $errors[] = ("Contact number must be of 8 digits!"); } if (isset($data['specialization']) && $data['specialization'] == 'Specialization_1') { $data['specialization'] = "IS"; $skillset = ($data['ISgrp1'].",".$data['ISgrp2'].",".$data['ISgrp3'].",".$data['ISgrp4'].",".$data['ISgrp5']); } if (isset($data['specialization']) && $data['specialization'] == 'Specialization_2') { $data['specialization'] = "IM"; $skillset = ($data['IMgrp1'].",".$data['IMgrp2'].",".$data['IMgrp3'].",".$data['IMgrp4'].",".$data['IMgrp5']); } if (isset($data['specialization']) && $data['specialization'] == 'Specialization_3') { $data['specialization']= "CNET"; $skillset = ($data['CNETgrp1'].",".$data['CNETgrp2'].",".$data['CNETgrp3'].",".$data['CNETgrp4'].",".$data['CNETgrp5']); } if (isset($data['specialization']) && $data['specialization'] == 'Specialization_4') { $data['specialization'] = "ITSM"; $skillset = ($data['ITSMgrp1'].",".$data['ITSMgrp2'].",".$data['ITSMgrp3'].",".$data['ITSMgrp4'].",".$data['ITSMgrp5']); } //image $fileError = $_FILES['image']['error']; //validate if there's any error if($fileError > 0){ // header("Location:".$returnURL."?err=imgUploadError"); $errors[] = ("Error on uploading image!"); exit; } $data['maxSize'] = 100000; $fileSize = $_FILES['image']['size']; //validate image size if($fileSize > $data['maxSize']){ $errors[] = ("Image size is too big!"); // exit; } //get the temporary location of the file $fileTempName = $_FILES['image']['tmp_name']; $trueFileType = exif_imagetype($fileTempName); $allowedFiles = array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG); if (in_array($trueFileType, $allowedFiles)) { switch($trueFileType){ case 1 : $fileExt = ".gif"; break; case 2: $fileExt = ".jpg"; break; case 3 : $fileExt = ".png"; break; } }else{ //header("Location:".$returnURL."?err=WrongFileType"); $errors[] = ("The one you've uploaded is not supported! Please check that it is in the right format."); exit; } // Get the path to upload the image to $myPathInfo = pathinfo($_SERVER['DOCUMENT_ROOT'].$_SERVER['PHP_SELF']); $currentDir = $myPathInfo['dirname']; $imgDir = $currentDir . '/profilePic/'; } // done with validation, if no errors, use the data - if(empty($errors)){ // your code to insert the data and move the uploaded file would go here... $stmt = $database->prepare("INSERT INTO userProfile (picName, picPath, Name, introduction, Specialization,skillset, Email, ContactNo) VALUES (:picName,:picPath,:Name,:introduction,:Specialization,:skillset,:Email,:ContactNo)"); $stmt->bindValue(':picName',$_FILES['image']['name']); $stmt->bindValue(':picPath', $imgDir); $stmt->bindValue(':Name', serialize($data['name'])); $stmt->bindValue(':introduction',serialize($data['introduction'])); $stmt->bindValue(':Specialization', serialize($data['specialization'])); $stmt->bindValue(':skillset', $skillset); $stmt->bindValue(':Email',serialize($data['email'])); $stmt->bindValue(':ContactNo',serialize($data['contactNo'])); $stmt->execute(); } To make the matter worse, after a few tries of submitting, the page just gives a blank page(same page though) without no errors. Did the <pre> tag but all values fit perfectly..what had i done? I'm so confusedĀ Quote Link to comment Share on other sites More sharing options...
beginnerProgrammer_96 Posted July 7, 2015 Author Share Posted July 7, 2015 im not sure why i can't edit my post but i just figure out that if i don't upload file, then it will give me blank page(sorry about that!) Now, i just need to get my $skillset working to do my image part ;-; Quote Link to comment Share on other sites More sharing options...
Barand Posted July 7, 2015 Share Posted July 7, 2015 Given the structure of the data in your form, I would suggest that that your DB structure should be +----------+ +-----------------------+ | user | | specialization | +----------+ +-----------------------+ | id (PK) | | specialization_id(PK) | | name | | description | | email | +-----------------------+ | username | | | password | +-------------------+ +-------------------+ | +----------+ | user_profile | | skillset | | | +-------------------+ +-------------------+ | | | userProfileId(PK) | | skillset_id(PK) | | | | user_id | | specialization_id |>---------------+ +-----------------| picname | | description | | introduction | +-------------------+ | contactno | | +-------------------+ | | | | | | | | +-----------------------+ | | | user_skillset | | | +-----------------------+ | +--------<| user_id(PK) |>-----+ | skillset_id(PK) | | level | +-----------------------+ Quote Link to comment Share on other sites More sharing options...
beginnerProgrammer_96 Posted July 7, 2015 Author Share Posted July 7, 2015 Ā Given the structure of the data in your form, I would suggest that that your DB structure should be +----------+ +-----------------------+ | user | | specialization | +----------+ +-----------------------+ | id (PK) | | specialization_id(PK) | | name | | description | | email | +-----------------------+ | username | | | password | +-------------------+ +-------------------+ | +----------+ | user_profile | | skillset | | | +-------------------+ +-------------------+ | | | userProfileId(PK) | | skillset_id(PK) | | | | user_id | | specialization_id |>---------------+ +-----------------| picname | | description | | introduction | +-------------------+ | contactno | | +-------------------+ | | | | | | | | +-----------------------+ | | | user_skillset | | | +-----------------------+ | +--------<| user_id(PK) |>-----+ | skillset_id(PK) | | level | +-----------------------+ Will this complicate my database? It seems i will need to reference alotĀ Ā (I'm not saying that this is not good or takes too much time to implement, because i know that I'm a newbie/lack of knowledge). I have another table which is portfolio, so basically i have 3 tables(user, userProfile, portfolio) as of right now. I would have to retrieve every user's picture in portfolio to show to the website so that it will link to the user's profile/portfolio. I hope i did not make it more complexĀ Quote Link to comment Share on other sites More sharing options...
Barand Posted July 8, 2015 Share Posted July 8, 2015 It may look more complicated but if your coding follows the db structure it's easier and requires far less code. I have attached a sample form and update using the proposed structure. As you can see it saves you hundreds of lines of repetitive coding. I don't what you want in the portfolio table but it sounds as though you treat it the same way as have with the user_skillset records (one image name per row, each with the user_id). Ā For brevity, I omitted the picture upload. beginner_profile.php beginner_update.php Quote Link to comment Share on other sites More sharing options...
beginnerProgrammer_96 Posted July 9, 2015 Author Share Posted July 9, 2015 It may look more complicated but if your coding follows the db structure it's easier and requires far less code. I have attached a sample form and update using the proposed structure. As you can see it saves you hundreds of lines of repetitive coding. I don't what you want in the portfolio table but it sounds as though you treat it the same way as have with the user_skillset records (one image name per row, each with the user_id). Ā For brevity, I omitted the picture upload. I see, I am implementing the database structure while looking through the codes. Could you explain more on the javascript on the skillset and the getSkillset function? I don't have a visual to see how this should works o.o It also seems that that piece of coding is to retrieve the data of skillsets and put it into the radiobuttons // GET PROFILE DATA IF EXISTS $sql = "SELECT u.name , IFNULL(up.userprofileid, 0) , up.introduction , up.contactno FROM user u LEFT JOIN user_profile up ON u.id = up.user_id WHERE u.id = $loggeduser"; $res = $database->query($sql); list($user_name, $userprofileid, $user_intro, $user_contactno) = $res->fetch_row(); $action = $userprofileid==0 ? 'Add' : 'Edit';Ā What does the line in bold do? Is that an add/edit button? I realised that this is a profile to show, rather than to add? the beginner_update.php is where i believe it is to add/edit/update? Also, the user_skillset table, I think you meant those two PK is the foreign key am i right? There should be another id(for that table) for the PK isn't it?Ā Quote Link to comment Share on other sites More sharing options...
Barand Posted July 9, 2015 Share Posted July 9, 2015 The section of code you posted above selects the profile record matching the user record. If there is no matching profile then those fields are null in the results. So if the userprofileid is found, the current data is displayed for editing. If not found, a blank form is displayed to add a profile. Line 11 is just to display "Add Profile" or "Edit Profile" in the fieldset legend. Ā The getSkillset() function runs a query to get all the skillset descriptions and matches the skillset_id against the user_skillset to get the current levels for the user for each skillset. It then loops through the results creating a new div for each specialization, and these divs contain the list of skillsets for the specialisation with their radio buttons (the buttons checked according to the users current level) Ā The javascript is to hide/show the div relating to the selected specialization. Ā I am attaching a dump of my test database Dump20150709.txt Quote Link to comment Share on other sites More sharing options...
beginnerProgrammer_96 Posted July 10, 2015 Author Share Posted July 10, 2015 The section of code you posted above selects the profile record matching the user record. If there is no matching profile then those fields are null in the results. So if the userprofileid is found, the current data is displayed for editing. If not found, a blank form is displayed to add a profile. Line 11 is just to display "Add Profile" or "Edit Profile" in the fieldset legend. Ā The getSkillset() function runs a query to get all the skillset descriptions and matches the skillset_id against the user_skillset to get the current levels for the user for each skillset. It then loops through the results creating a new div for each specialization, and these divs contain the list of skillsets for the specialisation with their radio buttons (the buttons checked according to the users current level) Ā The javascript is to hide/show the div relating to the selected specialization. Ā I am attaching a dump of my test database Thanks for this, but seeing the codes, it doesn't have those error handling, which I would like to put in. Also, about the line 11, the html fieldset you put is located outside the form. This caused warning from dreamweaver. I usually put the fieldset inside the form tag, will this change anything? Also, this <?=$example?>Ā , is this a short form tag of php? Or is it the combined comparison? I searched up on this and i found out that (if it is the combined comparison), it is available for php version 7, and my version is 5.6.8. Will this affect too? I have tried using the codes, and i'm getting this undefined offset error which I am confused about.Ā while (list($id,$descrip) = $res->fetch(PDO::FETCH_ASSOC)) { //conditional statement: if id ===1, then set to check, else, leave empty $chk = $id==1 ? 'checked="checked"' : ''; $btns .= "<div class='spec'><label><input $chk type='radio' name='spec' value='$id' class='rbspec' data-spec='$id'>$descripĀ Ā </label></div>"; if ($i%2==0) { $btns .= '<br>'; } $i++; } That error shows it to line 1, but I think it is more to the coding inside. I am not sure how to implement the checking like the one found in thisĀ http://stackoverflow.com/questions/17456325/php-notice-undefined-offset-1-with-array-when-reading-dataĀ I hope i could learn fast >< Quote Link to comment Share on other sites More sharing options...
Barand Posted July 10, 2015 Share Posted July 10, 2015 Sorry about the form/fieldset. I spotted that after posting and moved the form tags. (It worked as it was but I noticed a warning when I viewed the html source in my IDE) Ā <?=$example?> is shortcode for <?php echo $example;?> and is available from 5.4 Ā list() requires a numerically indexed array, not an associative array. Ā And yes, I omitted the error handling as my objective with the example was to show you how to handle the data. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.