How to retrieve value of loop textfield in php

I created all the textfield by loop and when I click button of the textfield, I can post it to another page. But now I cannot retrieve value of the textfield. How can I retrieve the value of the loop textfield?

This is my code.

<?php
	$i=0;
	$MYSQLQuery1=mysql_query("SELECT id,ccode,pdesc,cost FROM product2")or die(mysql_error());
	echo '<form action="addvehicle.php" role="form" method="POST" class="form-inline">';
	while($row=mysql_fetch_array($MYSQLQuery1))
	{
	 echo "
		 <tr>
		<td><img src='img/".$row['ccode'].".jpg' width='150' height='200'/><br/>
			  ".$row['pdesc']."<br/>"
			   .$row['cost']. "</td>
		<td>
		  <input type='text' name='product[]'>
		</td>
		<td>
		<input type='text' name='ID' value='".$row['id']."'>
		<input type='submit' name='SUBMITEDITVEHICLE'  value='Edit' class='btn btn-success'>
		</td>
		</tr>";
	}
		echo "</form>";
?>

This is where I post the value.

	if(isset($_POST['SUBMITEDITVEHICLE']))
	{
		if($_POST['SUBMITEDITVEHICLE']=='Edit')
		{	
			$E_1 = clean($_POST['ID']);
			echo "ID: ",$E_1,"<br/>";
			$gt = clean($_POST['product']); 
			print_r("product: ",$gt);
			$query_editvehicle=mysql_query("SELECT ccode,pdesc,cost FROM product2 WHERE ID='$E_1' ")or die(mysql_error());
			while($roweditvehicle=mysql_fetch_array($query_editvehicle))
			{
				$txtid = $roweditvehicle['id'];
				$txtvehicleregno = $roweditvehicle['ccode'];
				$txtmakemodel = $roweditvehicle['pdesc'];
				$txtenginecc = $roweditvehicle['cost'];	
			}
		}
	}

If I insert value in the textbox and clicked button of product id 5

Suppose is the value of the textbox will be post to next page. But now that is my problem, because it only retrieve the last record from the database

It is not easy to find a solution without having access to the database tables.

I did notice a coupe of errors which are revealed with the following error reporting:

<?php
  declare(strict_types=1);        // REMOVE IF NOT USING PHP7
  error_reporting(-1);            // maximum errors
  ini_set('display_errors', '1'); // 

// ERROR: 1
// mysql is deprecated, please use mysqli or PDO

// ERROR: 2
// wrong syntax for print_r(); - see Php Online Manual
// print_r("product: ", $gt);
// or use var_dump();

// ERROR: 3
// mysql table fields are CASE-SENSITIVE
/*
$sql = "
	SELECT 
 	   ccode, 
	   pdesc, 
	   cost 
	FROM 
       product2 
	WHERE 
      ID='$E_1' 
";
$query_editvehicle=mysqli_query( $sql )or die(mysql_error());

There maybe other errors :slight_smile:

1 Like

Actually this code got problem to retrieve the record, when I clicked at any button its only the last record will be display

Did you add the debug script I suggested?

Did you change the case of the database table field name?

Try a count( $query_editvehicle ); to see how many records are returned.

Yes, I already change it.
And I add count( $query_editvehicle ); but still nothing

Were there any errors?

Try adding this:

echo 'count( $query_editvehicle ); ==> ' .count( $query_editvehicle );
die;

No error. And it display this.

OK it is better to use the MySQL function to count the rows:

$row_cnt = mysqli_num_rows( $query_editvehicle );

echo 'mysqli_num_rows( $query_editvehicle ); ==> ' .$row_cnt;
die;


And this is the result

ID: 30
product: mysqli_num_rows( $query_editvehicle ); ==>

OK looks as though the query did not return any $rows.

Did you change the case on the table field name?

$sql = "
	SELECT 
 	   ccode, 
	   pdesc, 
	   cost 
	FROM 
       product2 
	WHERE 
      ID='$E_1' 
";
$query_editvehicle=mysqli_query( $sql )or die(mysql_error());
echo $sql ;
die;

I have edited my question.

Try adding this function to your page and confirm that each variable has the expected results.

<?php

//====================================
// usage:
//       fred( $_POST ); die;
//       fred( $val );  die;
//====================================
function fred( $val )
{
  echo '<pre>';
   # print_r( $val );
  var_dump( $val );
  echo '</pre>';
}

Still the same thing.

Did you check the value of the $_POST by using fred( $_POST ); ?

string(2) "30"
ID: 
array(30) {
  [0]=>
  string(2) "11"

This was display.

It looks as though that is only part of the $_POST variable and that there are 30 items in the array().

Remove that line and proceed to the next checking your variables.

Does it only retrieve the last record, or do you only display the last record? In your code in post #1 you have a while() loop to retrieve the results from your query based on the ID, but you don’t display them inside the loop. Or is that just truncated for the forum?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.