I have 2 tables involved in which I am trying to show a list of available data to choose from, however I want the initial selected value to appear as the current value.
My tables are:
tableLines
tableMakers
On the web page, I am displaying a list of all data in tableLines which consists of:
line_id
line_name
line_make_id
line_foot_id
line_legal_id
The column line_make_id contains a value from tableMakers, which contains the following:
make_id
make_name
I want to have ability for someone to edit a row in tableLines. In this specific example, I want the ability to edit the value shown in line_make_id, which is just a number, but I want it to display the make_name in the list, instead of the line_make_id number. I am a little lost on how to do this.
Here is the code I have at this time:
//execute the sql and get some results
$sql="select * from tableLines;";
$result1 = $conn->query($sql);
//Page Title
echo '<div class="userDisplayBox userDisplayBoxText">';
echo "Welcome " . $loginName;
echo '</div>';
//if the results have more than 1 row
if ($result1->num_rows > 0) {
echo '<table class="optionsMenuTable">';
echo '<tr>';
echo '<td><b>line_id</b></td>';
echo '<td><b>line_name</b></td>';
echo '<td><b>line_make_id</b></td>';
echo '<td><b>line_foot_id</b></td>';
echo '<td><b>line_legal_id</b></td>';
echo '</tr>';
while($row1 = $result1->fetch_assoc()) {
echo '<tr>';
echo '<td>' . $row1["line_id"] . '</td>';
// Edit Option
if ($id == "e" . $row1["line_id"]) {
// edit form
echo '<td>';
echo '<form name="edit_newData" id="edit_newData" action="main-lines.php?id=c' . $userSelection .'" method="post">';
echo '<input type="text" name="edit_newLineName" value="' . $row1["line_name"] . '">';
echo '</td><td>';
echo '<select name="edit_newMakeId" class="form-control">';
$makeSql = "select * from tableMakers order by make_name;";
$result2 = $conn->query($makeSql);
if ($result2->num_rows > 0) {
while($row2 = $result2->fetch_assoc()) {
?>
<option value="<?php echo $row2["make_id"] ?>"<?php if ($row2["make_id"] == $edit_newMakeId) { ?> selected<?php } ?>><?php echo $row2["make_name"] ?></option>
<?php
}
}
echo '</select>';
echo '</td><td>';
echo '<input type="text" name="edit_newMakeId" value="' . $row["line_make_id"] . '">';
echo '</td><td>';
echo '<input type="text" name="edit_newFootId" value="' . $row1["line_foot_id"] . '">';
echo '</td><td>';
echo '<input type="text" name="edit_newLegalId" value="' . $row1["line_legal_id"] . '">';
echo '</td><td>';
echo '<input type="submit" value="Confirm Change">';
echo '</form>';
echo '</td>';
}
else {
echo '<td>' . $row1["line_name"] . '</td>';
echo '<td>' . $row1["line_make_id"] . '</td>';
echo '<td>' . $row1["line_foot_id"] . '</td>';
echo '<td>' . $row1["line_legal_id"] . '</td>';
}
The above code is displaying a list of make_names, but it is not doing what I want. In this case if the line_id is 3, then what is happening is that it is showing the make_name for make_id #3, where-as some how I need to do a join, but I’m not sure how to incorporate the following join:
select a.line_id, b.make_name from tableLines a, tableMakers b where a.line_make_id = b.make_id;
I then need the value of this to replace $edit_newMakeId in the line above, but I can’t figure out how to make this work.
Part of my problem, is that I can’t figure out how to query a specific item of data. I know if I can figure that out, I can figure out the above (albeit by nesting a query, within a query, within another query).
For example, in the above I have the following code:
else {
echo '<td>' . $row1["line_name"] . '</td>';
echo '<td>' . $row1["line_make_id"] . '</td>';
echo '<td>' . $row1["line_foot_id"] . '</td>';
echo '<td>' . $row1["line_legal_id"] . '</td>';
}
For line_make_id, I want it to show make_name instead. I have tried the following, which does not work (but I really don’t know what I’m doing):
//$sql4="select a.line_id, b.make_name from tableLines a, tableMakers b where a.line_make_id = b.make_id;";
//$result4 = $conn->query($sql4);
//$row4 = mysql_fetch_row($result4);
I then alter the above example (line_make_id) to:
echo '<td>' . $row4["make_name"] . '</td>';
The result is blank. If I someone can help me with this query, then I should be able to solve my problem up top.