Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.
Hey I have encountered a problem with a form page I am using which takes the information from a row of an html table (dynamic html table that displays mysql data). I have the form page acting as an edit information screen where I would have text fields for entries the user may want to edit. What I want to do is pre-load the information from table (that specific row selected) into the text field's. I have used an auto_increment field from the mysql table to differentiate the value however every time i try to run this query through the form to print out the selected result it does not work.
<?php
##Connection to Database
$conn = mysql_connect("machine", "", "") or die("Could not connect : " . mysql_error());
mysql_select_db("my_db", $conn) or die("Could not select database");
$Auto_id = $_GET['Auto_id']; // this shows in url as 1,2,3 is my auto_inc field in database
$test = "SELECT * FROM build WHERE build_id = '$Auto_id'"; //simple query where build_id is also an auto_increment field in database that pertains to this field
mysql_query($test);
echo "<center>";
echo "<form method='POST' action='editInsert.php'>"; //editInsert.php is just me running queries to database using $_POST so i can update values, have tested and it works when i manually enter a 'value' for each form input but i want the form textfields to automatically load the row data
echo "<h1><b>Edit Results</b></h1></br>";
echo "<p class='hlight'><b>Codeline</b>";
echo "<input type='text' name='codeline' value='".$test['version']."' maxlength='10' size='10'>";
echo "</p>";
echo "<p class='hlight'><b>Bundle</b>";
echo " <input type='text' name='bundle' value='' maxlength='10' size='10'>";
echo "</p>";
echo "<p class='hlight'><b>Device</b>";
echo " <input type='text' name='device' value='' maxlength='10' size='10'>";
echo " </p>";
echo " <p class='hlight'><b>Java Precert</b>";
echo " <input type='text' name='java' value='' maxlength='10' size='10'>";
echo "</p>";
echo " <p class='hlight'><b>MMS</b>";
echo "<input type='text' name='mms' value='' maxlength='10' size='10'>";
echo " </p>";
echo "<p class='hlight'><b>Branch</b>";
echo " <input type='text' name='branch' value='' maxlength='10' size='10'>";
echo "</p>";
echo "<p class='hlight'><b>JC</b>";
echo "<input type='text' name='jc' value='' maxlength='10' size='10'>";
echo " </p>";
echo "<p>" ;
echo "<input type='submit' name='submit' value='Submit'>";
echo "<input type='button' name='cancel' value='Cancel' onclick='location.href='index.php''>";
echo " <input type='hidden' name='Auto_id' value = '' >";
echo "</p>";
echo "</form>";
echo "</div>";
echo "</center>";
?>
Sorry if it is a little messy, I tried
I tried it in the first line with 'codeline' as the field I wanted to check out. Basically just want to load the info from the table to these particular text fields.
I have checked the query in MySQL cmd and it works fine, so I know its not a query issue. Thanks~
Edit** There is a value that appears in the textfield, and the value is 'S' ...not sure what it means or what I can do about it?
You were fairly close, you executed the query successfully, but then you need to fetch the results using the mysql_fetch_assoc() method. See the example below:
<?php
##Connection to Database
$conn = mysql_connect ( "machine", "", "" ) or die ( "Could not connect : " . mysql_error () );
mysql_select_db ( "my_db", $conn ) or die ( "Could not select database" );
$Auto_id = $_GET ['Auto_id']; // this shows in url as 1,2,3 is my auto_inc field in database
// Query string
$strTestSql = "SELECT * FROM build WHERE build_id = '$Auto_id'"; //simple query where build_id is also an auto_increment field in database that pertains to this field
// Execute query
$objResult = mysql_query ( $strTestSql );
// Check results
if (!$objResult) {
echo "<span>Unable to locate record or error occured</span>";
} else {
// Fetch results
$arrTest = mysql_fetch_assoc($objResult);
// REMOVE ME This is for testing, let's see what came back
var_export($arrTest);
// Display form
echo "<center>";
echo "<form method='POST' action='editInsert.php'>"; //editInsert.php is just me running queries to database using $_POST so i can update values, have tested and it works when i manually enter a 'value' for each form input but i want the form textfields to automatically load the row data
echo "<h1><b>Edit Results</b></h1></br>";
echo "<p class='hlight'><b>Codeline</b>";
echo "<input type='text' name='codeline' value='" . $arrTest ['version'] . "' maxlength='10' size='10'>";
echo "</p>";
echo "<p class='hlight'><b>Bundle</b>";
echo " <input type='text' name='bundle' value='' maxlength='10' size='10'>";
echo "</p>";
echo "<p class='hlight'><b>Device</b>";
echo " <input type='text' name='device' value='' maxlength='10' size='10'>";
echo " </p>";
echo " <p class='hlight'><b>Java Precert</b>";
echo " <input type='text' name='java' value='' maxlength='10' size='10'>";
echo "</p>";
echo " <p class='hlight'><b>MMS</b>";
echo "<input type='text' name='mms' value='' maxlength='10' size='10'>";
echo " </p>";
echo "<p class='hlight'><b>Branch</b>";
echo " <input type='text' name='branch' value='' maxlength='10' size='10'>";
echo "</p>";
echo "<p class='hlight'><b>JC</b>";
echo "<input type='text' name='jc' value='' maxlength='10' size='10'>";
echo " </p>";
echo "<p>";
echo "<input type='submit' name='submit' value='Submit'>";
echo "<input type='button' name='cancel' value='Cancel' onclick='location.href='index.php''>";
echo " <input type='hidden' name='Auto_id' value = '' >";
echo "</p>";
echo "</form>";
echo "</div>";
echo "</center>";
}
// Free resources
mysql_free_result($result);
?>
MannyG wrote:Sorry if it is a little messy, I tried
Considering how much HTML you have and how little PHP you have, I would recommend selectively placing PHP within HTML rather than echoing out all of the HTML through PHP. So as soon as your <center> tag starts (isn't the center tag deprecated?), switch to HTML: