Jump to content

Trouble inserting data into table


Foulish

Recommended Posts

Hello,

 

I'm having trouble inserting data into a MySQL table.

 

The user has a form which is HEIGHT and WIDTH and NUMBER_OF_OBSERVATIONS. All these values are stored in the same table. NUMBER_OF_OBSERVATIONS does what it needs to and inserts its calculated results into the database, as does DATE_TIME.

 

I've been trying for a couple of days to get this done, and I am having no luck in getting it sorted.

 

Any help is greatly appreciated!

 

Generator.php

$WIDTH = $_POST['WIDTH'];
$HEIGHT = $_POST['HEIGHT'];
$NUMBER_OF_OBSERVATIONS = $_POST['NUMBER_OF_OBSERVATIONS'];

$db1 = new Number_Information();
$db1->openDB();
$OBSERVATION_ID = $db1->insert_Observation();
echo "<br /><br />Success. ID: <strong>$OBSERVATION_ID<strong>";
$db1->closeDB();

 

Number_Information.php

function insert_Observation() {

        //$Date_Now = datetime();
        $sql = "INSERT INTO Observations (DATE_TIME, HEIGHT, WIDTH) 
VALUES (NOW(), '{$esc_HEIGHT}','{$esc_WIDTH}' )";
        $result = mysql_query($sql, $this->conn);
        if (!$result) {
            die("SQL Insertion error: " . mysql_error());
        } else {
            return mysql_insert_id($this->conn);
        }

 

The table name is Observations

 

Again - Any help is greatly appreciated!

Link to comment
Share on other sites

not enough information... do you get an error?... which one?..  what have you done to debug your code?

we don't know anything regarding what your class Number_Information does

 

you said

"HEIGHT and WIDTH and NUMBER_OF_OBSERVATIONS. All these values are stored in the same table"

NUMBER_OF_OBSERVATIONS does what it needs to and inserts its calculated results into the database, as does DATE_TIME

your last sentence is not clear... you mean that you have separated inserts for NUMBER_OF_OBSERVATIONS and the one that you shown in your function insert_Observation() ?

 

be more specific and sure you will get help

Link to comment
Share on other sites

Sorry, its been a long day! I'll try again.

 

I have a form called Add_Numbers.php where the user inserts the numbers for HEIGHT, WIDTH and NUMBER_OF_OBSERVATIONS.

 

These figures then need to be inserted into the table Observations.

 

The figure inserted into NUMBER_OF_OBSERVATIONS telols the system how many random numbers it needs to generate, and it does it with a loop, and inserts these randomly generated figures into a table called Data.

 

Back to Observations - Observations generates an ID number and timestamps it as expected, but HEIGHT and WIDTH do not insert correctly.

 

There are no errors apprearing on screen.

 

Number_Information.php


<?php

//Save as Number_Information.php
require("db_config.php");

class Number_Information {
    /* DB connection handle */

    private $conn;

    function insert_Observation() {

        //$Date_Now = datetime();
        $esc_HEIGHT = mysql_real_escape_string($HEIGHT, $this->conn);
        $esc_WIDTH = mysql_real_escape_string($WIDTH, $this->conn);
        $sql = "INSERT INTO Observations (DATE_TIME, HEIGHT, WIDTH) 
VALUES (NOW(), '{$esc_HEIGHT}','{$esc_WIDTH}' )";
        $result = mysql_query($sql, $this->conn);
        if (!$result) {
            die("SQL Insertion error: " . mysql_error());
        } else {
            //$numofrows = mysql_affected_rows($this->conn);
            return mysql_insert_id($this->conn);
        }
    }

    function insert_GeneratedNumber($OBSERVATION_ID, $VALUE) {
        $esc_OBSERVATION_ID = mysql_real_escape_string($OBSERVATION_ID, $this->conn);
        $esc_VALUE = mysql_real_escape_string($VALUE, $this->conn);
        $sql = "INSERT INTO Data (OBSERVATION_ID, VALUE)
VALUES ('{$esc_OBSERVATION_ID}','{$esc_VALUE}')";
        $result = mysql_query($sql, $this->conn);
        if (!$result) {
            die("SQL Insertion error: " . mysql_error());
        } else {
            $numofrows = mysql_affected_rows($this->conn);
            return $numofrows;
        }
    }

    function openDB() {
        $this->conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
        if (!$this->conn) {
            die("SQL Connection error: " . mysql_error());
        }
        $db_selected = mysql_select_db(DB_NAME, $this->conn);
        if (!$db_selected) {
            die("SQL Selection error: " . mysql_error());
        }
    }

    function closeDB() {
        mysql_close($this->conn);
    }

    function getResult($sql) {
        $result = mysql_query($sql, $this->conn);
        if ($result) {
            return $result;
        } else {
            die("SQL Retrieve Error: " . mysql_error());
        }
    }
    
    

}

?>

 

Generator.php

<?php
$WIDTH = $_POST['WIDTH'];

$HEIGHT = $_POST['HEIGHT'];
$NUMBER_OF_OBSERVATIONS = $_POST['NUMBER_OF_OBSERVATIONS'];


echo "Your numbers will be generated from the range of 0 and $NUMBER_OF_OBSERVATIONS. <br />";

echo "{$NUMBER_OF_OBSERVATIONS} numbers will be created. <br />";
// This provides visual confirmation to the user. It will be removed.

$NUMBER_OF_QUADS = $WIDTH * $HEIGHT;
//This corrects the generate rate. Rather than create an extra number for zero.
//If you are to have a visual confirmation as to how many numbers are to be created,
//it needs to be placed below this statement


//returns id of insert row
$db1 = new Number_Information();
$db1->openDB();
$OBSERVATION_ID = $db1->insert_Observation();
echo "<br /><br />Success. ID: <strong>$OBSERVATION_ID<strong>";
$db1->closeDB();

$db1 = new Number_Information();
$db1->openDB();

for ($i = 0; $i <= $NUMBER_OF_OBSERVATIONS; $i++) {
    $NUMBER = rand(0, $NUMBER_OF_QUADS - 1); //This corrects the number of quads that the user sees
    $result = $db1->insert_GeneratedNumber($OBSERVATION_ID, $NUMBER);
    //$insert="INSERT INTO Data (VALUE) VALUES ('$NUMBER')";
}



//echo "<br /><br />Success. ID: <strong>$OBSERVATION_ID<strong>";
$db1->closeDB();  ?>

 

Note: The code for Generator.php does not show my attempt to insert $WIDTH and $HEIGHT into the table.

 

Through some testing, Add_Numbers is definatly sending $HEIGHT and $WIDTH to Generator.php.

Link to comment
Share on other sites

in this method.. check the included comment

 

function insert_Observation() {

        //$Date_Now = datetime();
        $esc_HEIGHT = mysql_real_escape_string($HEIGHT, $this->conn);
        $esc_WIDTH = mysql_real_escape_string($WIDTH, $this->conn);

       // echo which values you have in $esc_HEIGHT and $esc_WIDTH... they could be null

        $sql = "INSERT INTO Observations (DATE_TIME, HEIGHT, WIDTH) 
VALUES (NOW(), '{$esc_HEIGHT}','{$esc_WIDTH}' )";

       // As a debugging measure will be good too echo your $sql query and check for problems

        $result = mysql_query($sql, $this->conn);
        if (!$result) {
            die("SQL Insertion error: " . mysql_error());
        } else {
            //$numofrows = mysql_affected_rows($this->conn);
            return mysql_insert_id($this->conn);
        }
    }

Link to comment
Share on other sites

The echo is displaying: INSERT INTO Observations (DATE_TIME, HEIGHT, WIDTH) VALUES (NOW(), '','' )

 

The figures for W & H do not show, so for some reason, these variables are not being populated.

Link to comment
Share on other sites

Haha, I can see the probem, but it is bringing up an interesting error.

 

Warning: Missing argument 1 for Number_Information::insert_Observation(), called in /var/www/user/********/Generator.php on line 58 and defined in /var/www/user/********/Number_Information.php on line 11

 

Warning: Missing argument 2 for Number_Information::insert_Observation(), called in /var/www/user/********/Generator.php on line 58 and defined in /var/www/user/********/Number_Information.php on line 11

 

I suspect its due to the DATE_TIME bit.

 

(Line 11 is the start of the function where I had my oversight).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.