Form, MYSQL addition and retrieval

Hello everyone, this is my first post on these forums because i just joined. I am learning PHP and MYSQL and I have an issue with PHP since I want my code to do the following:

In a page called countries.php, I want it to display a table of information that it gets from a MYSQL table which has a list of countries (column name country_name) and their capitals (column name capital).
Either on the same page or on a different one add_new.php, I have a form to add a new country and its capital from the list. Right now I have these 2 PHP files (countries.php and add_new.php) but I get this error which I tried to fix:

Notice: Undefined variable: country in C:\wamp\www\countries.php on line 27 Notice: Undefined variable: capital in C:\wamp\www\countries.php on line 27

This is my code thus far:
countries.php
[php]

Untitled Document

Add new capital

<?php // Connects to Database mysql_connect("localhost", "username", "password") or die(mysql_error()); mysql_select_db("countries") or die(mysql_error()); $data = mysql_query("SELECT * FROM general_info") or die(mysql_error()); $country = $_POST['country_name']; $capital = $_POST['capital']; $query = "INSERT INTO general_info (country_name, capital) VALUES ('$country', '$capital')"; Print ""; Print ""; Print ""; while($info = mysql_fetch_array( $data )) { Print ""; Print " "; Print ""; } Print "
Country Capital
".$info['country_name'] . "".$info['capital'] . "
"; ?> [/php]

add_new.php
[php]

Untitled Document

Country:

Capital:

<?php // Connects to Database mysql_connect("localhost", "username", "password") or die(mysql_error()); mysql_select_db("countries") or die(mysql_error()); $data = mysql_query("SELECT * FROM general_info") or die(mysql_error()); $country = $_POST['country']; $capital = $_POST['capital']; $query = "INSERT INTO general_info (country_name, capital) VALUES ('$country', '$capital')"; ?> [/php]

Thank you.

Sorry for the double post, I didn’t see an Edit button. I fixed the undefined variable problem because that was the issue I had when experimenting. The actual issue (which matches that code) is:

Notice: Undefined index: country_name in C:\wamp\www\countries.php on line 18 Notice: Undefined index: capital in C:\wamp\www\countries.php on line 19

Hi…
Welcome!!!
To solve your problem, I believe you keyed in the wrong id for receiving the country name in countries.php…
In line 18, it should be:
[php]
$country = $_POST[‘country’];
$capital = $_POST[‘capital’];
[/php]
instead of:
[php]
$country = $_POST[‘country_name’];
$capital = $_POST[‘capital’];
[/php]

I don’t see where you use the $query variable that you have created… If you do use it, make sure to test whether $country and $capital have values in them…
To store them in a database I believe this would be the best route:
[php]
if(!empty($country) && !empty($capital)) {
$query = “INSERT INTO general_info (country_name, capital) VALUES (’$country’, ‘$capital’)”;
mysql_query($query) or die(mysql_error());
}
[/php]

Hope that fixes everything… :smiley:
Good luck!

Sponsor our Newsletter | Privacy Policy | Terms of Service