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);}
?>

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

Do you have all of your mysql connect info right?
Do you have a table set up?

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>

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");
}
?>

Could you please wrap any code in code-tags.

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.