The file is called test.php, It takes username and check if it is blank in the same page and show a message.. I wrote the code but its not working.. please help.

test.php:


<html>
<form method="post" action="test.php">
User Name :


<input name="UserName" type="text" id="UserName" value="User Name" size="20" maxlength="30" />
<input type=submit value=Submit>
</form>
</html>
<?php
mysql_connect("Localhost","username","password")
or die("Failure to communicate");
mysql_select_db("mydb")
or die("Could not connect to Database");
if($_POST == 'Submit'){
if(!$_POST) || $_POST==""){
echo "Enter User Name";}}
else{
$p="INSERT INTO mytable(UserName)VALUES('$UserName')";
$q=mysql_query($p);}
?>

Edited 3 Years Ago by happygeek: fixed formatting

Try this:

<html>
<form method="post" action="test.php">
User Name : <input name="UserName" type="text" id="UserName" value="User Name" size="20" maxlength="30" />
<input type="submit" name="submit" value="Submit">
</form>
</html>
<?php
if (isset($_POST['submit'])) {
mysql_connect("Localhost","username","password")
or die("Failure to communicate");
mysql_select_db("mydb")
or die("Could not connect to Database");
if ($_REQUEST['UserName'] == NULL) {
echo "Please Enter a User Name";
}
else {
$p="INSERT INTO mytable(UserName)VALUES('$UserName')";
$q=mysql_query($p);
}
}
else {
echo "Enter a User Name";
}
?>

Try this:

<html>
<form method="post" action="test.php">
User Name : <input name="UserName" type="text" id="UserName" value="User Name" size="20" maxlength="30" />
<input type="submit" name="submit" value="Submit">
</form>
</html>
<?php
if (isset($_POST['submit'])) {
mysql_connect("Localhost","username","password")
or die("Failure to communicate");
mysql_select_db("mydb")
or die("Could not connect to Database");
if ($_REQUEST['UserName'] == NULL) {
echo "Please Enter a User Name";
}
else {
$p="INSERT INTO mytable(UserName)VALUES('$UserName')";
$q=mysql_query($p);
}
}
else {
echo "Enter a User Name";
}
?>

Thanks and this is working partly..as when a username is provided its not going into the database though no error is being shown.. please help

replace this($q=mysql_query($p);)
with this($q=mysql_query($p) or die(mysql_error());

to show the error you want to display.

try this code:

<?php
mysql_connect("localhost","root","")
or die("Failure to communicate");
mysql_select_db("mydb")
or die("Could not connect to Database");
if(isset ($_POST))
{
if(empty ($_POST))
{
echo "Enter User Name";
}
else
{
$query="INSERT INTO tbl1 SET fieldname = '" .$_POST. "'";
mysql_query($query) or die("can't execute query");
echo "query finished";
}
}
?>
<form method="post" action="test.php">
User Name :
<input name="UserName" type="text" size="20" maxlength="30" />
<input type="submit" value="Submit" name="submit">
</form>

Edited 3 Years Ago by happygeek: fixed formatting

how can php form submit on same page without reloading all input those are givven with check validation??

<html>
<form method="post" action="test.php">
User Name : 
<input name="UserName" type="text" id="UserName" value="User Name" size="20" maxlength="30" />

<input type = "submit" name = "sumbit" value = "Submit">
</form>
</html>

<?php
mysql_connect("Localhost","username","password")
or die("Failure to communicate");

mysql_select_db("mydb")
or die("Could not connect to Database");

if($_POST['submit'] == 'sumbit'){

if(!$_POST['UserName']){
echo "Enter User Name";}}
else{
$username = $_POST['UserName'];

$query = "INSERT INTO mytable(UserName)VALUES('$username')";
if(mysql_query($query))
  printf("Successfully Inserted");
else
  printf("Error in Insert query");
}
?>

Edited 5 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.

Could you please wrap any code in code-tags.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.