php - SQL query failing to INSERT new row into table -


i have chunk of php code triggers if button pressed user. expect happen checks see if user has skill assigned them, , if not performs insert. if nothing. , if checkbox next skill un-checked checks see if have skill assigned , delete if found.

the code deleting skills user no matter condition of checkboxes. im sure must missing after starring @ code hours cannot see it.

can suggest resolution?

php code:

if(isset($_post['update']))     {          $default = 0;              foreach($skills_array $skills_id=>$skills_name)             {                 if (isset($_post[$skills_name]))                 {                     if (empty($_post[$skills_name.'exp']))                     {                         $exp = $default;                     }                     else                     {                         $exp = $_post[$skills_name.'exp'];                     }                      $sql = $con->query("select count(`userid`) total `userskills` `userid` = '$user' , `skillid` = ".$skills_id)                      or die(mysqli_error($con));                      if ($row = mysqli_fetch_assoc($sql))                     {                         $sql = $con->query("insert `userskills` ( `userid`, `skillid`, `experience`) values  ('$user', '$skills_id', '$exp')")                         or die(mysqli_error($con));                         //if checkbox not checked check see if skill skill assigned user. if delete it. if not ignore.                        }                     else                     {                         $sql = $con->query("update `userskills` set `experience` = '$exp' `userid` = '$user' , `skillid` = ".$skills_id)                         or die(mysqli_error($con));                     }                 }                  else                 {                     $sql = $con->query("delete `userskills` `userid` = '$user' , `skillid` = ".$skills_id)                     or die(mysqli_error($con));                 }             }              header('location: account.php');             die();         }         else         {             echo 'incorrect password please try again.';         }     } 

html code:

<div class="rightbody">             <form id="form2" name="form2" method="post" enctype="multipart/form-data">                     <p><h3>skills:</h3>     <?php      $result1 = $con->query("select skills.`skillid`, skills.`description`, count(userskills.`skillid`) skilluserhas, max(`experience`) experience                             `skills`                             left outer join userskills                             on skills.`skillid` = userskills.`skillid` , userskills.`userid` = '$user'                             group skills.`skillid`, skills.`description`                             order field(skills.`skillid`, 1, 7, 9, 3, 4, 5, 6, 8)")                              or die(mysqli_error($con));        while ($skillrow = $result1->fetch_assoc())      {     ?>                     <div class="checkboxtext">                     <?php                         echo '<label>';                         echo '<input type="checkbox" name="'.$skillrow['description'].'" id="checkboxgroup1_'.$skillrow['skillid'].'" class="skillselect" value="yes" '.(($skillrow['skilluserhas'] > 0) ? 'checked' : '').'>';                         echo $skillrow['description'].'</label>';                         echo '<input type="number" name="'.$skillrow['description'].'exp" class="expnumber" placeholder="enter experience in years." value="'.$skillrow['experience'].'">';                         echo '<br />';                         echo '<br />';                       }                       ?>                     </div>                     </p>             </form>     </div> 

im not familiar php, yesterday in similar problem

here create query count return row, , if not skill value 0

$sql = $con->query("select count(`userid`) total `userskills` `userid` = '$user' , `skillid` = ".$skills_id)                      or die(mysqli_error($con)); 

so instead of if ($row = mysqli_fetch_assoc($sql)) need like

 $row = mysqli_fetch_assoc($sql);  $skill = $row['total'];  if ($skill == 0 ) 

but doesnt solve error describe, delete skills or just 1 selected?

any way have check if branch sending skill delete.

 if (isset($_post[$skills_name])) 

this mean $skills_name isnt defined. maybe should check value inside array $skills_array?

my second guess check code create on webpage. right click page , selece see source code


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -