Won't query to database?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ichversuchte
Forum Newbie
Posts: 9
Joined: Thu Nov 10, 2005 12:05 pm

Won't query to database?

Post by ichversuchte »

I am trying to run a query, getting the info from a form. The code below is the actually query.

Code: Select all

<?php 
// Get the PHP file containing the DbConnector class 
require_once('../includes/DbConnector.php'); 

// Check whether a form has been submitted. If so, carry on 
if ($_POST){ 

    // Create an instance of DbConnector 
    $connector = new DbConnector(); 

    // IMPORTANT!! ADD FORM VALIDATION CODE HERE - SEE THE NEXT ARTICLE 

    // Create an SQL query (MySQL version) 
    $insertQuery = "INSERT INTO info (raname,floater,weekendstart,weekendend) VALUES (". 
   "'".$_POST['raname']."', ". 
"'".$_POST['floater']."', ". 
$_POST['weekendstart'].", ". 
"'".$_POST['weekendend']."')"; 

    // Save the form data into the database 
    if ($result = $connector->query($insertQuery)){ 

        // It worked, give confirmation 
        echo '<center><b>Article added to the database</b></center><br>'; 

    }else{ 

        // It hasn't worked so stop. Better error handling code would be good here! 
        exit('<center>Sorry, there was an error saving to the database</center>'); 

    } 

} 
?>
This is the form code

Code: Select all

<form action="newArticle.php" method="POST"> 
  <p>Resident Assistant's Name: 
    <input type="text" name="raname" /> 
</p> 
  <p>If Floater Enter Date: 
    <input type="text" name="floater"/> 
</p> 
  <p>If Weekend: From 
    <input type="text" name="weekendstart"/> 
to 
<input type="text" name="weekendend"/> 
  </p> 
  <p>    <input type="submit" /> 
        </p> 
</form>
I echoed out the $insertQuery and got:
INSERT INTO info (raname,floater,weekendstart,weekendend) VALUES ('test', 'test', test, 'test')

I didn't know if it was my tables in my database so i took a screenshot and i posted it here:

http://sweetopc.com/Image.bmp

Does anyone have any idea? any help will be appreciated. :?:
Sequalit
Forum Commoner
Posts: 75
Joined: Wed Oct 12, 2005 9:57 pm
Location: Texas

Post by Sequalit »

I think it may be a problem with your db connecter class... or im just missing the problem.

But you should get that when you echo out your insertQuery cause its just a string that youve done nothing with yet.
The problem your having is exactly what you should be getting when you echo that.
ichversuchte
Forum Newbie
Posts: 9
Joined: Thu Nov 10, 2005 12:05 pm

Post by ichversuchte »

This is the dbconnector page. I think this is fine, because i am able to pull stuff from the database and echo it.

Code: Select all

<?php

require_once 'SystemComponent.php';

class DbConnector extends SystemComponent {

    var $theQuery;
    var $link;

    //*** Function: DbConnector, Purpose: Connect to the database ***
    function DbConnector(){

        // Load settings from parent class
        $settings = SystemComponent::getSettings();

        // Get the main settings from the array we just loaded
        $host = $settings['dbhost'];
        $db = $settings['dbname'];
        $user = $settings['dbusername'];
        $pass = $settings['dbpassword'];

        // Connect to the database
        $this->link = mysql_connect($host, $user, $pass);
        mysql_select_db($db);
        register_shutdown_function(array(&$this, 'close'));

    }

    //*** Function: query, Purpose: Execute a database query ***
    function query($query) {

        $this->theQuery = $query;
        return mysql_query($query, $this->link);

    }

    //*** Function: fetchArray, Purpose: Get array of query results ***
    function fetchArray($result) {

        return mysql_fetch_array($result);

    }

    //*** Function: close, Purpose: Close the connection ***
    function close() {

        mysql_close($this->link);

    }


}
?>
Post Reply