Search code examples
sqlformsauto-populate

SQL/PHP make an array with column name / value for specific unique id


I have an sql table named clients where each client is stored in a row, with a unique (auto increment) id and the column names are: clientname, clientlastname, clientmobile and so on.

I want to make an array that will retrieve the contents of a specific client (row) and will be like : clientname => lalala, clientlasthname=>lalalala... and so on.

Then I want to display the results in an html form (non submitable, just display) each cell in the form has a unique id and name same as the column names of the sql db.

I just new at it and cant seem to get it. Here's what I've done so far:

<?php
   header("Content-Type:text/html; charset=utf-8");
   if ($_POST) {
       $link = mysql_connect("localhost","userid","pw");
       if (!$link) { die("Database Connection Failed". mysql_error()); }
       mysql_set_charset('utf8',$link);
       $db_select = mysql_select_db("form2",$link);
       if(!$db_select){ die("Database Selection Failed " . mysql_error()); }
    }  else { echo "Search:"; }
?>

<html>
 <head> </head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

 <link rel="stylesheet" href="css/dsearch.css" />
 <link rel="stylesheet" href="css/base.css" />
 <link type="text/css" href="css/selected/jquery-ui-1.8.18.custom.css" rel="stylesheet" />
 <script language="javascript" type="text/javascript" src="js/jquery.min.js"></script>
 <script language="javascript" type="text/javascript" src="js/jquery-ui.min.js"></script>
 <script language="javascript" type="text/javascript" src="js/dropdowndata.sql/dropdowndata.clients.js"></script>
 <script language="javascript" type="text/javascript" src="js/buttons.js"></script>

 <body>
   <form class="form" id="dsearch" name="dsearch" action="dsearch.php" method="POST" enctype="application/x-www-form-urlencoded">
      <div class="dsearch"> 
        <li class="ui-widget"><label for="clients">client id: </label><input class="client" name="searchcid" style="width:100px" /></li>
        <li class="cell3"><input type="submit" value="Αναζήτηση" style="color:green; width:210px;  " /></li>
      </div>
   </form>
   <div class="results">


<?php
   $result1 = mysql_query("SELECT * FROM clients WHERE id='$_POST[searchcid]'", $link);
   if(!$db_select) { die("No Data found in db " . mysql_error()); };

   $items=array();
   while($row = mysql_fetch_array($result1)){
     $items[$row['id']] = $row['dlastname'];    

/* here is where I need the help I guess, 
   I need to fill the $items array with the data formatted as mentioned above.
   And after that I want to populate a form that I haven't already written with the
   values ,any help or maybe any suggestions on some tutorials I could go study
   would be appreciated.
   It goes on like this: */

   }
   print_r ($items);

?>

   </div>

  </body>
</html>
<?php
   if ($_POST) {
        mysql_close($link);
   }

?>

Edit: figured out how to get the array

$clinfo=array();
$clinfo=mysql_fetch_assoc($result1);            
print_r ($clinfo);

Now I need a way to go put the data where the field id equals the id of the docinfo array


Solution

  • figured out how to do it with a little php and jquery, here it is:

    $(document).ready(function(){
    
         <?php
                if($_GET){
                           foreach($docinfo as $key=>$val) { 
                           echo "$('#" . $key . "').val('" . htmlspecialchars($val) ."');";
                           }
                   }
        ?>
    });
    

    This creates a jquery line for each cell id and fills it with the appropriate value

    Any comments or improvements are extremely appreciated