hello guys i am stuck in a small but horrible problem.i am redirect page to itself using $_server[''PHH_SELF] method but the problem is that i apply the insert query at the top of my html page but it shows me that you are not connect to the DB.

<?php

if(isset($_POST['action']))
{
$con = mysql_connect("localhost","root","");
if ($con)
  {
  die('connected: ' );
  }
  
  else{
	  
	  echo "not connected"; [B][U]// ITS ECHO ME NOT CONNECT WHAT THE PROBLEM I CANT FIGURE IT OUT[/U][/B]
	  }

$db_selected = mysql_select_db("project", $con);



echo $classess = $_POST['class_name'];
$sql = mysql_query("INSERT INTO `classes` (class_name) VALUES ('$classess')");

	

}

?>



<html>
<head>
<link rel="stylesheet" type="text/css" href="enter_classCss.css" />
<?php include("header.php"); ?>
</head>


	<div class="upper">
		</div>
	

        


      <div class="main">

      <div class="center">

      <div class="testing">
       </div>
  
  
      <div class="test">
                       
    


  
     </div> 
 
 		</div>
                  

     <div class="white">
                            
      <div class="wrapper2" style="float: left;margin-left: 6px;margin-top: -35px;">    
          </div>
                            
                            <form id="formID" class="formular" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"  style="width:600px">
                           
                            <div class="field2">
 								<span>Enter Class</span>
    								</div>
                                    
                                    
                              <div class="newfield">
 								<span>Class</span>
    								</div>   
                		    
                            
                                
                                
  							<div class="field5">     
 				<input type="text" class="validate[required] text-input" name="class_name" id="class_name"  /> 
    							</div>
     
<input type="submit" value="submit"  style="  margin-left: -90px;
    margin-top: 234px;"/>    							</div>    
   
                                 
 </form>                         

     
          </div>
  
  
  
  
        

     <div class="image">
       <img src="images/compclass.jpg" style=" width:40px; height:40px;"/>
          </div>
      
     <div class="name2">
        Class Form 
           </div>
</body>
</html>

Hello
First why you have set die() if connection is successful?


Your problem is not from the PHP_SELF. Your problem is at the top of your code.
You have set $_POST["action"] for it you have to set

<input name="action">

the action at <form> is only to set location for your form. try only with $_POST instead ..
Other thing is to look at your database password you haven't set any? (perhaps for the demo only)..

<?php

if(isset($_POST))
{
$con = mysql_connect("localhost","root","");
if ($con)
  {
  die('connected: ' );
  }
  
  else{
	  
	  echo "not connected"; // ITS ECHO ME NOT CONNECT WHAT THE PROBLEM I CANT FIGURE IT OUT
	  }

$db_selected = mysql_select_db("project", $con);



echo $classess = $_POST['class_name'];
$sql = mysql_query("INSERT INTO `classes` (class_name) VALUES ('$classess')");

	

}

?>



<html>
<head>
<link rel="stylesheet" type="text/css" href="enter_classCss.css" />
<?php include("header.php"); ?>
</head>


	<div class="upper">
		</div>
	

        


      <div class="main">

      <div class="center">

      <div class="testing">
       </div>
  
  
      <div class="test">
                       
    


  
     </div> 
 
 		</div>
                  

     <div class="white">
                            
      <div class="wrapper2" style="float: left;margin-left: 6px;margin-top: -35px;">    
          </div>
                            
                            <form id="formID" class="formular" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"  style="width:600px">
                           
                            <div class="field2">
 								<span>Enter Class</span>
    								</div>
                                    
                                    
                              <div class="newfield">
 								<span>Class</span>
    								</div>   
                		    
                            
                                
                                
  							<div class="field5">     
 				<input type="text" class="validate[required] text-input" name="class_name" id="class_name"  /> 
    							</div>
     
<input type="submit" value="submit"  style="  margin-left: -90px;
    margin-top: 234px;"/>    							</div>    
   
                                 
 </form>                         

     
          </div>
  
  
  
  
        

     <div class="image">
       <img src="images/compclass.jpg" style=" width:40px; height:40px;"/>
          </div>
      
     <div class="name2">
        Class Form 
           </div>
</body>
</html>

i have two question for you

1. why

$_post['action']

not work in way as i use form

action="<?php echo $_SERVER['PHP_SELF']; ?>"

.

2. Second that why to use

<input name="action"/>

whats the logic behind this.

i will be waiting for your answer thanks in Advancesv3tli0

with

action="<?php echo $_SERVER['PHP_SELF']; ?>"

You set to which file you want to post your form data. Its not a POST data which you send only a url which you use.

The $_POST["names"] are coming from your inputs like this one

<input type="text" name="INPUTNAME"  />

This will come out as $_POST["INPUTNAME"]

For your script I can offer you just one easy check
At start just set

if($_POST) { YOUR PHP CODE }

This will work only if you have posted data to your page(with your form).

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.