Replace post value with database

I have small html form as showing below. I can successfully display form values by post method. But I am trying , instead of city, it should display country in the post. ( city and country both are columns fields in mysql table). Is there any way to replace post value city that it should be replace with country from that mysql table ???

<label>Name</label><input type = text name = "name">
<label>Father</label><input type = text name = "father"><label>City</label><input type = text name = "city">
<input type = submit value = "submit">
Here is php source code

        $Name = $_POST['name'];
	 $Father = $_POST['father'];
        $city = $_POST['city'];


<table class="center" border=0  width=600>
<tr><td><b>Name : </td><td><?php echo $_POST["name"]; ?>
<tr><td><b>Father : </td><td><?php echo $_POST["Father"]; ?>
<tr><td><b>City : </td><td><?php echo $_POST["city"]; ?>

So, you want to take the city that the user entered into the form, search for that city in your database table, and then display the country that is associated with the city?

That shouldn’t be too difficult. What have you tried so far? Pseudo-code would be something like:

receive form data
query = "select country from mytable where city = ?"
run the query
if there is a result, display it

How will you handle cities that appear in more than one country?

1 Like

actually in my database I have unique city name
in procedueral method where city = $_Post[‘city’] will be used ?

query = "select country from mytable where city = ?"

? is the parameter that you would replace with the city name that you’re searching for, once you’ve validated it. I was using ? because that’s how you’d do it with a prepared statement.

there will be value , but how to replace that value with post one ???

@droopsnoot eror occured
Notice: Array to string conversion in /home/thehospi/public_html/le/bill/invoice.php on line 61

  $sql = "SELECT * FROM city WHERE city = '$city'";
   $result = $con->query($sql); 

    if ($result->num_rows > 0) {        

        while ($row = $result->fetch_assoc()) {

                $city1 = $row['city'];
		$postTest = $_POST[$city1];
                $_POST["test"] == $postTest;

Which is line 61? And why are you assigning a value to $_POST['test'] in this code? What is in $city? Does your query run without error when you test it in phpmyadmin?

its first line which I send you, actually my html form has multiple value instead of single, some thing like. thats whty i am facing that error, My html form can post multiple value against one submit

<label>Name</label><input type = text name = "name[]">
<label>Father</label><input type = text name = "father[]">

Ah, OK, that wasn’t in the code that you put in the first post, that’s just a single value.

If you want to select multiple rows, you’ll need to either use OR / AND as required, or use IN clauses, in your query.

I’m confused now as there are two very similar threads running about exactly the same subject.

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