kevinfwb Posted March 7, 2007 Share Posted March 7, 2007 I have a PHP page that has a list of sales figures. Next to each row I have a link to edit the information. When you click on edit it opens up another page which is basically the initial data entry page but with the values for the corresponding fields filled in. On the summary page the edit link calls edit.php?primaryKey=pk for that particular row. That part is working just fine. Normally when I call another page via a link my query is "SELECT * FROM table WHERE year=2007" or whatever the case my be. Since I'm passing the primary key there will only be one result from the query. Do I still need to wrap the whole thing within a while loop or is there a more simplistic way? Everything I've done before ended up in more than one result and the array was necessary. Here is an snippit of my edit page: $pk = $_GET["primaryKey"]; $query = "SELECT * FROM sales WHERE primaryKey = $pk"; $result = mysql_query($query); if (!$result) { echo ("<P>Error performing query: " . mysql_error() . "</P>"); exit(); } while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo " <input type=\"text\" name=\"Sales\" size=\"10\" value=\"$row['Sales']\"/></td>\n"; } there are several more parts to it, but it's just redundant code with different figures. Basically - Do I need to wrap the whole thing in an array when I'm querying based on the primary key and there will ony be 1 result for each field? Thanks -Kevin Link to comment https://forums.phpfreaks.com/topic/41541-solved-pretty-basic-question/ Share on other sites More sharing options...
Barand Posted March 7, 2007 Share Posted March 7, 2007 Basically - Do I need to wrap the whole thing in an array when I'm querying based on the primary key and there will ony be 1 result for each field? Thanks -Kevin You don't need the while but the array is an array of the fields in the row, not an array of rows. Use if ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo " <input type=\"text\" name=\"Sales\" size=\"10\" value=\"$row['Sales']\"/></td>\n"; } else { echo "No matching record\n"; } Link to comment https://forums.phpfreaks.com/topic/41541-solved-pretty-basic-question/#findComment-201321 Share on other sites More sharing options...
kevinfwb Posted March 7, 2007 Author Share Posted March 7, 2007 Thanks for the help, that makes sense! Link to comment https://forums.phpfreaks.com/topic/41541-solved-pretty-basic-question/#findComment-201344 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.