1

Recently I have been assigned a group project for a college class and I will need to query a customers name from a database and then print out the rest of the row in form fields. I have the select menu working correctly and it will print to the form field. However, the problem occurring is the query results will only show the last row in the MYSQL table I selected. Any help here would be greatly appreciated. I have been spinning my wheels for a few days on this issue. I am only a beginner coder, so it might be a little messy.

Thanks,

Connection.PHP File

<?php


function Connect()
{
 $dbhost = "localhost";
 $dbuser = "root";
 $dbpass = "";
 $dbname = "medley";

 // Create connection
 $conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname) or die($conn->connect_error);

 return $conn;
}

?>

My Query Page

 <?php
require 'connection.php';
$conn    = Connect();

$sql = "SELECT * FROM cust_info";
$result = $conn->query($sql);

echo "<select id='firstName' name='firstname' onchange=populatesecondbox()>";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['F_Name'] . "', '" . $row['L_Name'] . "'> " . $row['F_Name'] . " " . $row['L_Name'] . "</option>";

$pphone = $row['P_Phone'];

}
echo "</select>";
?>

<input id="secondinputbox" type="text" />



<script type="text/javascript">
function populatesecondbox(val) {
    var dropdown = document.getElementById("firstName");
    var pphone = document.getElementById("secondinputbox");
    var secondfname = document.getElementById("thirdinputbox");
    var str = "<?php echo $pphone ?>";
    var sfname = "<?php echo $sfname ?>";
    pphone.value = str;
    secondfname.value = sfname;
}
</script>







​
  • 5
    Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Nov 30 '16 at 14:54
  • 3
    I think your problem is that you are not aware that PHP runs on the SERVER and javascript runs in the BROWSER. Therefore the javascript code does not have direct access to the PHP variables when it is running – RiggsFolly Nov 30 '16 at 14:57
  • I understand your statement here, however I am wondering what you exactly mean? The script does print the PHP variables, but only prints the last row in the MYSQL table and not from the row selected from the dropdown menu. – Brandon Wilkerson Nov 30 '16 at 15:38
  • I don't think anyone can properly answer your question until, as @RiggsFolly suggests, you replace the `mysql_` functions with `mysqli_` functions. – AntonChanning Nov 30 '16 at 15:41
  • Please put the code PHP & Javascript in the order that it appears in your real code – RiggsFolly Nov 30 '16 at 15:54
  • For some reason this would not allow me to post the code with the script at the bottom and would only do it on the top. How I have it on my webpage is with the script directly under the inputs. I will work on converting to the mysqli in the meantime. Thanks, – Brandon Wilkerson Nov 30 '16 at 16:07
  • I did change to mysqli and that is working now, however still have the same issue. – Brandon Wilkerson Nov 30 '16 at 17:02
  • To test, after the $result= line, do a print_r($result); Then look at your browser source code to see what shows up. – kojow7 Nov 30 '16 at 17:43
  • This is the result I got "mysqli_result Object ( [current_field] => 0 [field_count] => 10 [lengths] => [num_rows] => 3 [type] => 0 )" – Brandon Wilkerson Nov 30 '16 at 17:47
  • The lines: var str = ""; and var sfname = ""; will only ever get run once. This is because the PHP code is processed first, then sends the resulting HTML/Javascript to your web browser. You will need to get the updated values from JavaScript alone. – kojow7 Nov 30 '16 at 17:50
  • Also, the line "$pphone = $row['P_Phone'];" runs for every record processed. So, when the while loop is done, only the last one gets stored in $pphone. – kojow7 Nov 30 '16 at 17:55

2 Answers2

0

Instead of using

$row = mysqli_fetch_array($result)

on line 10, use

$row = $result->fetch_assoc()

or

$row = $result->fetch_array()

The difference between fetch_assoc and fetch_array is that fetch_array contains both numeric indices and named indices. For example echo $row[0] will output as same as echo $row['id']. Whereas, fetch_assoc only contains named indices.

Wolverine
  • 1,712
  • 1
  • 15
  • 18
bocanjis
  • 1
  • 3
  • Thank you for the suggestions. I tried them and it still unfortunately does the same thing where it only looks for the last row instead of the row selected. – Brandon Wilkerson Dec 01 '16 at 00:35
0

Based on the provided loop statement

$pphone = $row['P_Phone'];

$pphone is getting assigned to the last row on termination of the loop because each iteration is overriding $pphone with the current row until at which point in the loop $pphone gets the last value.

Instead of using

$pphone = $row['P_Phone'];

Try the following in the loop.

$pphone[] = $row['P_Phone'];

Your concatenated phones should be provided after the loop.

$pphones = "['".join("','" , $pphone)."']";

In your js script tag just get $pphones as a string;

var pphones = <?php echo $pphones; ?>;

ddl = document.getElementById('firstname');
pphone.value = pphones[ddl.selectedIndex];
Vahe
  • 1,699
  • 3
  • 25
  • 76
  • Unfortunately this did not work either. I am still having a little trouble working this one out. – Brandon Wilkerson Dec 05 '16 at 16:54
  • Hi Brandon, can you provide any output or results at the point where it does not work? If needed, I can modify the response provided. Thanks, I'd be happy to provide further assistance. – Vahe Dec 05 '16 at 17:13
  • My table would show columns "F_Name, L_Name, P_Phone" The drop down menu is showing all the rows correctly however the code is printing the last value in 'P_Phone' So it is printing out the correct column, just not the right value associated with the value I select. The code seems to print out the last value in that column. Thanks for the assistance. – Brandon Wilkerson Dec 06 '16 at 00:11
  • Disregard I found completely different code that helped me resolve this issue. Thanks for your suggestions – Brandon Wilkerson Dec 06 '16 at 17:08