Issue in replacement post value with database value

Hi Sir by using your code and guidance i try to implement this in following steps

  1. get form value via Post (city value which i get is 1)
    2)look at mysql table which have two fields id, city
  2. sql querry to get database table city value (which is London) in which city from post will be equal to id field of the database (all working fine)
    3- I wanted to replace 1 with london.
  3. but when used replace value as then its given just “L” rather than full London as city and if I posted two values from Html form then “O” is displaying for 2nd line rather than value of 2nd row
echo '<td style = "text-align:left; border:1px solid #cccccc;">'.$city1[$i].'</td>';

Below is full code

<?php

if($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submit']))
{
// getting all values from the HTML form
       $country = $_POST['country'];
	$state = $_POST['state'];
	$city = $_POST['city'];        
     
 // database details
    $host = "localhost";
    $username = "thehospi_root";
    $password = "u1m1a1r1";
    $dbname = "thehospi_hmis2";

    // creating a connection
    $con = mysqli_connect($host, $username, $password, $dbname);

    // to ensure that the connection is made
    if (!$con)
    {
        die("Connection failed!" . mysqli_connect_error());
    
	
	foreach($_POST['city'] as $k => $v){
		$city = $_POST['city'][$k];	 
		
		$sql = "SELECT `city` FROM `city` WHERE `id` = '$city'";
		$result = $con->query($sql); 
		if ($result->num_rows > 0) {
			while ($row = $result->fetch_assoc()) {			
				$city1 = $row['city'];	
			  echo var_dump($city1);   // for checking value is given after check found                     
			
				
}} 
}

?>
 //Now belonw I want to display value
<html>
<head>
<link rel="stylesheet" type="text/css" href="style1.css" />
</head>

<body>


 for ($i=0;$i<count($_POST['country']);$i++) {
 echo "<tr>";
echo '<td style = "text-align:left; border:1px solid #cccccc;">'.$_POST['country'][$i].'</td>';
echo '<td style = "text-align:left; border:1px solid #cccccc;">'.$city1[$i].'</td>';

} 
 ?>

</table>

I don’t see the form, but based on this

$country = $_POST['country'];
$state = $_POST['state'];
$city = $_POST['city'];

$_POST['city'] and $_POST['country'] represent values and not an array of cities or countries. So you would NOT loop through them or use KEYS with them, e.g. $_POST['country'][$i].

Inside your foreach() loop, you are using the same $city1 variable, so if you do have multiple city values coming in from the form as I believe you mentioned in another thread, you will just end up with one value for $city1, and it will contain whichever was the last value in your $_POST['city'] array. If you need to have multiples, you need to create an array and store the results in there.

When you loop through at the end, if you use a numeric index (such as [1] or [2]) on a scalar string (i.e. not an array) it will return the character you indicate in the brackets - so if $city1 = "London", then echo $city1[2] will display “n”.

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