0

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

3
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by Xufyan
0

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

Edited by Xufyan: n/a

0

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

0

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 .

0

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'

Edited by Xufyan: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.