1

Please assist! I am trying to build a PHP script that will do multiple functions. I have 2 mysql tables (I can't post pics yet so here are the descriptions):

PrimaryTable

ID    first    Last    phone              
1    Dan      Powers   5559876578         
2    Pete    Williams  5559023245

SecondaryTable

ID    user_id    First    Last      phone

1       2        Pete    Williams   5559023245

I need a PHP script that will allow you to search for a phone # from the first (PrimaryTable), get all the data from that table, and echo it in a html table. I have this part working, see code below:

<?php

 require 'Admin_connect.php';

 $sql = 'SELECT * FROM PrimaryTable';


 mysql_select_db('PrimaryTableDatabase');
 $retval = mysql_query( $sql, $conn );

 if(! $retval )
 {
   die('Could not get data: ' . mysql_error());
 }
 if (!empty($_REQUEST['searchphone'])) {

   $searchphone = mysql_real_escape_string($_REQUEST['searchphone']);    
   $insert = mysql_real_escape_string($_REQUEST['insert']); 

   $sql = "SELECT * FROM PrimaryTable WHERE phone LIKE '%".$searchphone."%'"; 
   $r_query = mysql_query($sql); 

   while ($row = mysql_fetch_array($r_query)){  

     $searchresults = '<tr>' . '<td headers="first_name">' .$row['first_name'] . '</td>' .
     '<td headers="last_name">' .$row['last_name'] . '</td>' .   
     '<td id="phoneresult">' .$row['phone'] . '</td>' .  
     '<td headers="Birthday">' .$row['Birthday'] . '</td>' .   
     '<td headers="Gender">' .$row['Gender']. '</td>' .
     '<td id="zip">' .$row['zip'] . '</td>' .
     '<td>' . '<input type="submit" value="Scan" name="Scan" />' . '</form>' . '</td>' . '</tr>';   
    }  

  }
?>
---------HTML----------
<form action="" method="post">  
Search: <input type="text" name="searchphone" /><br />  
<input type="submit" value="Submit" />  
</form>  

<table width="200" border="1" cellspacing="1" cellpadding="1">
 <tr>
<td>First Name:</td>
<td>Last Name:</td>
<td>Phone:</td>
<td>Birthday:</td>
<td>Gender:</td>
<td>Zip:</td>
 </tr>
<?php echo $searchresults ?>
</table>

The next part has me stumped. I need to have a Submit button next to each row that shows up in the table (as you can see in the php above). When a user clicks that submit button it gets the phone # and name of the user displayed in the HTML table, selects all their information from the Mysql primaryTable (SELECT * FROM PrimaryTable WHERE phone=(variable) && Last = (variable)), and inserts all their information from the PrimaryTable into the SecondaryTable.

I think if I can find a way to turn the table <td> content into variables I can use them to SELECT and INSERT. But I don't know how to parse the HTML table in a way I can turn the phone # and name into their own variables. I've looked into DomDocument but can't seem to get it to work with what I need. Any help would be appreciated.

1
  • Your best bet is to go with a javascript/ajax post for each line. To answer your question about getting data as postable variables add for example <input type='hidden' name='first_name' value='$row[first_name]' /> in each <td> Commented Sep 5, 2014 at 18:34

1 Answer 1

1

I think it will be easier if you use the ID from the table (I am assuming the ID column is the primary key.) If you wrap your table in a form (<form method="post"><table>...</table></form>) then you can add a <button> element in each row like this:

'<td><button name="id" value="' . $row['id'] . '">Submit</button></td>'

When you click on the button it will submit the form with the corresponding id. Then you can get the id from $_POST['id'], be sure it's escaped properly, and let your database get the name and phone number for you instead.

INSERT INTO SecondaryTable 
SELECT id AS user_id, first, last, phone 
FROM PrimaryTable
WHERE id = $id
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.