Input form?

Hello all,

I’m curious as to why my input form won’t submit data if I’m not using th/tr table forms. The data just won’t submit to the database if I try it any other way. I am wondering if it is something in my php code?

Here is the original (Working/Contains Tables):

[php]<?php
require_once(‘general.php’);
require_once(‘db/db_connection.php’);

if (isset($_POST['submitBtn'])) {
     $name     = (isset($_POST['name'])) ? htmlentities($_POST['name']) : '' ;
     $comment  = (isset($_POST['comment'])) ? htmlentities($_POST['comment']) : '' ;
     $company = (isset($_POST['company'])) ? htmlentities($_POST['company']) : '' ;
	 $phone = (isset($_POST['phone'])) ? htmlentities($_POST['phone']) : '' ;
	 $discipline = (isset($_POST['discipline'])) ? htmlentities($_POST['discipline']) : '' ;
     $web  = (isset($_POST['web'])) ? htmlentities(str_replace('http://','',$_POST['web'])) : '' ;
     $email    = (isset($_POST['email'])) ? htmlentities($_POST['email']) : '' ;
     $actDate  = date("Y-m-d H:i:s");
     
     //Minimum name and comment length.
     if ((strlen($name) > 2) && (strlen($comment) > 5)){
         $sql = "INSERT INTO distributor (name,text,insertdate,company,phone,discipline,web,email) VALUES (";
         $sql .= "'".$name."','".$comment."','".$actDate."','".$company."','".$phone."','".$discipline."','".$web."','".$email."')";
         $MyDb->f_ExecuteSql($sql);
     }
     
     header("Location: index.php");
}
else {

?>

<!-- PNG FIX for IE6 -->
<!-- http://24ways.org/2007/supersleight-transparent-png-in-ie6 -->
<!--[if lte IE 6]>
    <script type="text/javascript" src="login_panel/js/pngfix/supersleight-min.js"></script>
<![endif]-->

<script src="login_panel/js/slide.js" type="text/javascript"></script>
          <tr><td colspan="2" align="center"><br/><input class="text" type="submit" name="submitBtn" value="Submit" /></td></tr>
        </table>  
      </form>

    </div> 
<?php } ?> [/php]

Here is the form I am trying to use:

[code] <!–

Contact Information


Name



Comment



Company



Phone



Discipline
<?php listDisciplines(); ?>



Web



Email

			<p class="submit"><input class="text" type="submit" name="submitBtn" value="Submit"/></p>	
						
		</fieldset>								
	</form>	-->

[/code]

It will just not connect to the database regardless of what I change.

If you need any more code, please let me know.

Name:
Comment:
Company:
Phone:
Discipline: <?php listDisciplines(); ?>
Web:
Email:

first thing albiet off topic I’d suggest checking out this article: http://blogs.msdn.com/b/sqlphp/archive/2008/09/30/how-and-why-to-use-parameterized-queries.aspx

as far as your form…

is this commented for a reason?(//echo $_SERVER[‘PHP_SELF’])
[php][/php]

are you getting any errors? does db_connection.php include any error handling? Is the problem in not connecting to the database? Or is it just that information from the form isn’t getting there?

Another off topic bit that can help clean up your code a bit when handling forms:

[php]if (isset($_POST[‘submitBtn’])) {
$name = (isset($_POST[‘name’])) ? htmlentities($_POST[‘name’]) : ‘’ ;
$comment = (isset($_POST[‘comment’])) ? htmlentities($_POST[‘comment’]) : ‘’ ;
$company = (isset($_POST[‘company’])) ? htmlentities($_POST[‘company’]) : ‘’ ;
$phone = (isset($_POST[‘phone’])) ? htmlentities($_POST[‘phone’]) : ‘’ ;
$discipline = (isset($_POST[‘discipline’])) ? htmlentities($_POST[‘discipline’]) : ‘’ ;
$web = (isset($_POST[‘web’])) ? htmlentities(str_replace(‘http://’,’’,$_POST[‘web’])) : ‘’ ;
$email = (isset($_POST[‘email’])) ? htmlentities($_POST[‘email’]) : ‘’ ;
$actDate = date(“Y-m-d H:i:s”);[/php]

can be changed to:
[php]
//no need for isset if this form was posted it will trigger
if ($_POST[‘submitBtn’]) {
foreach ($_POST as $k => $v) {
//loops through all _POST variables and creates a new variable with the array key($_POST[‘this_is_the_key’]) as the variable name also runs the htmlentities function on the value. You can also run other functions you may need to prepare the users input specifically
$$k = htmlentities($v);
}
$actDate = date(“Y-m-d H:i:s”);
}
[/php]

I apologize, the commented code was not meant to be commented out.
Thank you for that article, I’ll definitely change up my code thanks to you :).

I am not having any database errors, it is simply just not getting information there. I do not have much error handling, although my mysql class does.

Wow, thank you very much. I really appreciate all of the help.

Here is the mysql php class.

[php]<?php
/**

  • MySql database class
  • This class contains the important MySql database functions.
  • @author WC
  • @version 1.0
    */
    class cMysqlDB
    {
    var $connection_id;
    var $result;
    var $record = array();

/**

  • MySql class constructor

  • The constructor establishes a connection to a MySQL server and set working database.

  • If an error occures return with false else return with the connection_id.

  • @param string $hostname

  • @param string $username

  • @param string $userpassword

  • @param string $database

  • @param bool $persistent optional

  • @return connection_id

  • @author WC

  • @version 1.0
    */
    function cMysqlDB($hostname, $username, $userpassword, $database, $persistent = true)
    {
    $this->host = $hostname;
    $this->user = $username;
    $this->password = $userpassword;
    $this->dbname = $database;
    $this->persistent = $persistent;

     $this->connection_id = ($this->persistent) ? mysql_pconnect($this->host, $this->user, $this->password) : mysql_connect($this->host, $this->user, $this->password);
    
     if ($this->connection_id)
     {
     	if ($this->dbname != "")
     	{
     		$dbselect = mysql_select_db($this->dbname);
    
     		if( !$dbselect )
     		{
     			mysql_close($this->db_connect_id);
     			$this->connection_id = false;
     		}
     	}
     	return $this->connection_id;
     }
     else
     	return false;
    

    }

    function f_CloseConnection()
    {
    if( $this->connection_id )
    return mysql_close($this->connection_id);
    else
    return false;
    }

    function f_ExecuteSql($sql = “”)
    {
    unset($this->result);

     if ($sql != "")
     	$this->result = mysql_query($sql, $this->connection_id);
    
     if (!$this->result) {
         $err = mysql_error();
     }
         
     if ($this->result)
     {
     	unset($this->record[$this->result]);
     	return $this->result;
     }
    

    }

    function f_GetSelectedRows($query_id = 0)
    {
    if( !$query_id ) $query_id = $this->result;

     return ( $query_id ) ? mysql_num_rows($query_id) : false;
    

    }

    function f_GetAffectedRows()
    {
    return ( $this->connection_id ) ? mysql_affected_rows($this->connection_id) : false;
    }

    function f_GetRecord($query_id = 0)
    {
    if( !$query_id ) $query_id = $this->result;
    if ($query_id)
    {
    $this->record = mysql_fetch_assoc($query_id);
    return $this->record;
    }
    else
    return false;
    }

    function f_SetRecordPointer($recordnumber, $query_id = 0)
    {
    if( !$query_id ) $query_id = $this->result;

     return ( $query_id ) ? mysql_data_seek($query_id, $recordnumber) : false;
    

    }

    function f_GetNextId()
    {
    return ( $this->connection_id ) ? mysql_insert_id($this->connection_id) : false;
    }

    function f_FreeResult($query_id = 0)
    {
    if( !$query_id ) $query_id = $this->query_result;

     if ( $query_id )
     {
     	unset($this->record[$query_id]);
    
     	mysql_free_result($query_id);
    
     	return true;
     }
     else
     	return false;
    

    }

    function f_GetSqlError()
    {
    $result[‘message’] = mysql_error($this->connection_id);
    $result[‘code’] = mysql_errno($this->connection_id);

     return $result;
    

    }
    }
    ?>[/php]

db_connection.php
[php]<?php
require_once(‘mysql.php’);

$serverhost = “localhost”;
$serveruser = “root”;
$serverpwd = “toor”;
$dbname = “dbname”;

$MyDb = new cMysqlDB($serverhost,$serveruser,$serverpwd,$dbname);
?>
[/php]

Of course this is going to consist of one of my weaknesses…

I believe
function f_ExecuteSql($sql = “”)
should be
function f_ExecuteSql($sql)

I also believe
$MyDb->f_ExecuteSql($sql);
should be
$cMysqlDB->f_ExecuteSql($sql);

let me know if this works and we can go from there and maybe someone more experienced in OOP will jump in as well.

Demetri - can you tell me what you used to create the dbforms that you are using here? I have a website I took over that uses this code, but there is no reference as to its origin…

Thanks

Sponsor our Newsletter | Privacy Policy | Terms of Service