php mysql simple form help

whats wrong with my code ?? every time i refreh my page a new field is created in database 'example' i want my code to create new field in database only if something is insert in text field

here is my code

<?php
$name=$_POST['name'];
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("quotes") or die(mysql_error());
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="name">
<input type="submit" value="GO">
</form>

<?php
	mysql_query ("INSERT INTO example (name) VALUES('$name')") or die(mysql_error());
	$result = mysql_query("SELECT * FROM example")
	or die(mysql_error()); 
	$row = mysql_fetch_array( $result );
	echo "Name: ".$row['name'];
?>

also when i am fetching all the records from databse , the ouput showing nothing on the screen :(
please help

first question solved
the second question is that

$result = mysql_query("SELECT * FROM example")
	or die(mysql_error()); 
	$row = mysql_fetch_array( $result );
	echo $row['name'];

i am fetching everything from the databse but nothing is showing

where exactly is the problem? everything is running ok but there is no result?

hey , like this , you will just be adding empty feild each time u run your page before adding another page
check this code , it might help you

<?php
if(!isset($_POST['name']))
{
?>

<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="name">
<input type="submit" value="GO">
</form>
<?php
}
else
{
$name = $_POST['name'];
if($name != "")
{
mysql_connect("localhost","root","") or die("cant connect to the server ".mysql_error());
mysql_select_db("quotes") or die("cant cselect the database ".mysql_error());
$result = mysql_query("INSERT INTO example (name) VALUES('$name')");
if($result)
{
echo "Thanks, the name has been submited ";
}
else
{
echo "Error <BR>".mysql_error();
}
}
else
{
echo "please fill the form with something ";
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="name">
<input type="submit" value="GO">
</form>
<?php
}
}
$select = mysql_query("SELECT * FROM example") or die("cant select from the table example ".mysql_error());
while($rows = mysql_fetch_array($select))
{
echo $rows['name'];
echo "<BR>";
}
mysql_close();

this coude will add the name to your database as well as it will display the names on the database , but i think your problem is that your database has too many empty feilds , so open phpmyadmin and remove all the empty feilds .

thanks, here is my code

<?php
	$name=$_POST['name'];
	mysql_connect("localhost", "root", "") or die(mysql_error());
	mysql_select_db("quotes") or die(mysql_error());
?>

<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="name">
<input type="submit" value="GO" name="submit">
</form>

<?php
	if(isset($_POST['submit'])) 
	{
	mysql_query ("INSERT INTO example (name) VALUES('$name')") or die(mysql_error());
	}
//Fetching
$query = "SELECT * FROM example"; 
	 
$result = mysql_query($query) or die(mysql_error());


while($row = mysql_fetch_array($result)){
	echo $row['name'];
	echo "<br />";
}
?>

the database is successfully fetching but how to make a link for every database,
for example , when database 'one' is fetched a new page should be created 'one.php'

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.