title: PHP form MySql EDIT / UPDATE - Can't get form to populate with data
body: <p>I'm very new to PHP and MySQL. I've created a form which creates rows in my MySQL tables, I created a page that shows all those entries. Now I'm trying to edit those entries. I created another form that should populate with info from the selected row..the only problem is that it's not. Here's all my code including login.php...just in case. Please let me . I've looked everywhere for an answer and couldn't find one. All I found was mysql and not mysqli.</p>
  
  <pre><code>&lt;?php //login.php
  $db_hostname = 'localhost';
  $db_database = 'mydata';
  $db_username = 'user';
  $db_password = '123#';
  
  // Connect to server.
  $link = mysqli_connect($db_hostname, $db_username, $db_password);
  if (!$link) {
      die('Not connected : ' . mysqli_error());
  }
  
  // Select the database. 
  $db_selected = mysqli_select_db($link, $db_database);
  if (!$db_selected) {
      die ('Can\'t use database : ' . mysqli_error());
  }
  ?&gt;
  </code></pre>
  
  <blockquote>
    <p>edit_client.php</p>
  </blockquote>
  
  <pre><code>&lt;?php
  $title = "Edit Client Form";
  ?&gt;
  
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;&lt;?php echo $title ?&gt;&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div&gt; &lt;!-- form container div --&gt;
      &lt;form action="" method="post"&gt;
          &lt;div&gt; &lt;!-- form description div --&gt;
              &lt;h2&gt;&lt;?php echo $title ?&gt;&lt;/h2&gt;
              &lt;p&gt;Edit a client here.&lt;/p&gt;
          &lt;/div&gt;
  
  &lt;?php
  require_once = 'login.php';
  // get value of id that sent from address bar
  $id=$_GET['id'];
  if(isset($id)) {
  $sql="SELECT * FROM `clients` WHERE client_id='$id'";
  $result=mysqli_query($sql);
  $row=mysqli_fetch_assoc($result);
   ?&gt;
          &lt;input type="hidden" name="id" value="&lt;?php echo $row['client_id']; ?&gt;"&gt;
          &lt;input type="text" name="first_name" value="&lt;?php echo $row['first_name']; ?&gt;" required&gt;&lt;br /&gt;
          &lt;input type="text"name="last_name" value="&lt;?php echo $row['last_name']; ?&gt;" required&gt;&lt;br /&gt;
          &lt;input type="text"name="company_name" value="&lt;?php echo $row['company_name']; ?&gt;" &gt;&lt;br /&gt;
          &lt;input type="text" name="address" value="&lt;?php echo $row['address']; ?&gt;"&gt;&lt;br /&gt;
          &lt;input type="text" name="city" value="&lt;?php echo $row['city']; ?&gt;" required&gt;&lt;br /&gt;
          &lt;input type="text" name="state" value="&lt;?php echo $row['state']; ?&gt;" required&gt;&lt;br /&gt;
          &lt;input type="number" name="zip_code" value="&lt;?php echo $row['zip_code']; ?&gt;" required&gt;&lt;br /&gt;
          &lt;input type="tel" name="tel_number" value="&lt;?php echo $row['tel_number']; ?&gt;"  required&gt;&lt;br /&gt;
          &lt;input type="email" name="email" value="&lt;?php echo $row['email']; ?&gt;" required&gt;&lt;br /&gt;
          &lt;input value="Submit" type="submit"&gt;
      &lt;/form&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>here's the table from "display_client" that sends the ID to the edit_form.php page</p>
  
  <pre><code>&lt;td align="left"&gt;' . '&lt;a href="\edit_client.php?id=' . $row['client_id'] . '/"&gt;Edit&lt;/td&gt;
  </code></pre>
  
  <p>any pointers in the right direction would be much appreciated. thank you.</p>
  
  <p>As suggested below I have used PDO Prepared statements to achieve my desired result. Thank you thank you! @nomistic !</p>
  
  <pre><code>&lt;?php
  $title = "Edit Client Form";
  ?&gt;
  
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;&lt;?php echo $title ?&gt;&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div&gt; &lt;!-- form container div --&gt;
      &lt;form action="update_client_submit.php" method="post"&gt;
          &lt;div&gt; &lt;!-- form description div --&gt;
              &lt;h2&gt;&lt;?php echo $title ?&gt;&lt;/h2&gt;
              &lt;p&gt;Edit a client here.&lt;/p&gt;
          &lt;/div&gt;
              &lt;?php
              require_once 'login.php';
  
              if(!empty($_GET['id'])){
                  $client_id = intval($_GET['id']);
              }
  
              try {
              $results = $db -&gt; prepare('SELECT * FROM clients WHERE client_id = ?');
              $results -&gt; bindParam(1, $client_id);
              $results -&gt; execute();
  
              } catch(Exception $e) {
                  echo $e -&gt; getMessage();
                  die();
              }
              $row = $results -&gt; fetch(PDO::FETCH_ASSOC);
              ?&gt;  
          &lt;input type="hidden" name="client_id" value="&lt;?php echo $row['client_id']; ?&gt;"&gt;
          &lt;input type="text" name="first_name" value="&lt;?php echo $row['first_name']; ?&gt;" required&gt;&lt;br /&gt;
          &lt;input type="text" name="last_name" value="&lt;?php echo $row['last_name']; ?&gt;" required&gt;&lt;br /&gt; 
          &lt;input type="text" name="company_name"  value="&lt;?php echo $row['company_name']; ?&gt;" required&gt;&lt;br /&gt;
          &lt;input type="text" name="address"  value="&lt;?php echo $row['address']; ?&gt;" required&gt;&lt;br /&gt;  
          &lt;input type="text" name="city" value="&lt;?php echo $row['city'] ?&gt;"  &gt;&lt;br /&gt;
          &lt;input type="text" name="state" value="&lt;?php echo $row['state'] ?&gt;" required&gt;&lt;br /&gt;
          &lt;input type="text" name="zip_code" value="&lt;?php echo $row['zip_code']; ?&gt;" required&gt;&lt;br /&gt; 
          &lt;input type="text" name="tel_number"  value="&lt;?php echo $row['tel_number']; ?&gt;" required&gt;&lt;br /&gt;
          &lt;input type="text" name="email"  value="&lt;?php echo $row['email']; ?&gt;" required&gt;&lt;br /&gt;
          &lt;input type="submit" name="sumbit"&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>