Updating a form entry in php/mysql with checkboxes? -


how can allow user submitting form, update entry on "re-submission" example

  • 12345678910 (unique id) , submitted form selections,

  • 12345678910 , re-submitted new selections

what's function responsible "automatically" updating such kind of form entries.

i know can use check if entry exists, how update if exists , insert in new row if doesn't ...

function checkstudentid($studentid) {     $con = connectvar();     mysql_select_db("database1", $con);     $result = mysql_query(         "select * table studentid='$studentid' limit 1");      if(mysql_fetch_array($result) !== false)         ....     // want add entry here since doesn't exist...with checkboxes   // else , want update if exists     } 

now i'm not positive if above code work...but have starters, if there other way or if method i'm using "wrong" , appreciate heads up...or if i'm trying possible (the way i'm doing it)...

notes

  • i have 1 php file form submits to.
  • i not using login/registration system
  • i not want display data in table using html, "automatic" update if studentid exists in table

if using deprecated method interact database, this:

<?php     function checkstudentid($studentid) {         $con = connectvar();         mysql_select_db("database1", $con);         $result = mysql_query(             "select * table studentid='$studentid' limit 1");          $query = '';         if (mysql_num_rows($result) > 0) {             $query = "update table set column1='$value_one', column2='$value_two' studentid='$studentid'";         } else {             $query = "insert table values('$new_id', '$value_one', '$value_two')";         }          if (mysql_query($query)) {             return true;         } else {             return false;         }     } ?> 

but again, use pdo interact db.

here simple pdo example (you have write function return connection):

<?php     function checkstudentid($studentid) {          $update = false;          $dbh = formpdoconnection();         $query = "select studentid table studentid=:id";         $stmt = $dbh->prepare($query);         $stmt->bindvalue(':id', $studentid, pdo::param_str);         if ($stmt->execute()) {             if ($stmt->rowcount()) {                 $update = true;             }         } else {             return 'failure execute query';         }          // if need update         if ($update) {             $update = "update table set value1=:v1,                  value2=:v2 studentid=:id";             $stmt = $dbh->prepare($update);             $stmt->bindvalue(':id', $studentid, pdo::param_str);             $stmt->bindvalue(':v1', $value_one, pdo::param_str);             $stmt->bindvalue(':v2', $value_two, pdo::param_str);         } else {             $insert = "insert table values(:id,:v1,v2)";             $stmt = $dbh->prepare($insert);             $stmt->bindvalue(':id', $new_id, pdo::param_str);             $stmt->bindvalue(':v1', $value_one, pdo::param_str);             $stmt->bindvalue(':v2', $value_two, pdo::param_str);         }          return $stmt->execute();     } ?> 

save headache , stop using mysql_*


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -