Page 1 of 1

PHP Form and MySQL

Posted: Wed Mar 31, 2010 1:19 pm
by MannyG
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.

Code: Select all

<?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?

Re: PHP Form and MySQL

Posted: Wed Mar 31, 2010 1:41 pm
by jbulaswad
Manny,

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:

Code: Select all

<?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);
	
?>


Re: PHP Form and MySQL

Posted: Wed Mar 31, 2010 1:56 pm
by MannyG
Ah, when I ran that it gave me an error I could not figure out D:

Then I took out the part of the code where you check the results and just printed out $objResult and it did not work, empty text field D:

Re: PHP Form and MySQL

Posted: Wed Mar 31, 2010 2:16 pm
by jbulaswad
The $objResult is a resource link and will not output anything useful for you. Try this:

Code: Select all

<?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>Error occured: " . mysql_error() . "</span>";
        } else if (mysql_num_rows($objResult) == 0) {
			echo "<span>Unable to locate record</span>";
        } else {
                // Fetch results
        		while ($row = mysql_fetch_assoc($objResult)){
               		$arrTest = $row;
        		}
                // 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($objResult);
       
?>

 

Re: PHP Form and MySQL

Posted: Thu Apr 01, 2010 8:16 pm
by allspiritseve
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:

[text]<center>
<form method="POST" action="editInsert.php">
<h1><b>Edit Results</b></h1></br>
<p class="hlight"><b>Codeline</b>
<input type="text" name="codeline" value="<?php echo $test['version']; ?>" maxlength="10" size="10">
</p>
etc...[/text]

That will make your code more readable and should lessen your chances of triggering an error due to a missed quote somewhere.