[SOLVED] Inserting data to database

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
christian_phpbeginner
Forum Contributor
Posts: 136
Joined: Sat Jun 03, 2006 2:43 pm
Location: Java

[SOLVED] Inserting data to database

Post by christian_phpbeginner »

Hello,

I don't know why, but I can't insert data to database. Please take a look at the function to insert data below:

Code: Select all

public static function insertNewAddressData($addressID, $typeID, $userID, $streetAddress, $state, $city, $postalCode, $country, $defaultAddress) {
   		$query = "INSERT INTO tbladdress (addressID, typeID, userID, streetAddress, state, city, postalCode, country, defaultAddress, addedOn, lastUpdatedOn) VALUES (null, $typeID, $userID, $streetAddress, $state, $city, $postalCode, $country, $defaultAddress, NOW(), NULL)";
   		$result = mysqli_query(MySQLConnection::connectToMySQL(), $query);
   	    
   	    if (!($result)) {
   	    	die ("Failed inserting address data into database.");
   	    }
   	    
   	    echo mysqli_affected_rows($result). ' address(es) have been added to the database.';
   	    mysqli_close(MySQLConnection::connectToMySQL());   	    
   	}
In all my codes to retrieve data I am using MySQLConnection. There is nothing wrong with the connection and I can get the results. But when I insert new data, it always failed. And I can't seem to figure out where I made mistake ?

Thank you,
Chris
Last edited by christian_phpbeginner on Mon Nov 13, 2006 12:29 pm, edited 1 time in total.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

it always failed.
meaning die ("Failed inserting address data into database."); is executed?
then try http://de3.php.net/mysqli_error
User avatar
snowrhythm
Forum Commoner
Posts: 75
Joined: Thu May 04, 2006 1:14 pm
Location: North Bay Area, CA

Post by snowrhythm »

did you check to make sure that addressID is allowed to be null in the database? i'm just guessing but it seems like that might
be your primary key for that table and wouldn't be allowed to be null. also, what's the error message that you're getting?
that would help explain things a bit more...
User avatar
christian_phpbeginner
Forum Contributor
Posts: 136
Joined: Sat Jun 03, 2006 2:43 pm
Location: Java

Post by christian_phpbeginner »

Hey, thanks guys !

I guess, I am the one who wasn't careful. I edited the function insertNewAddressData:

Code: Select all

public static function insertNewAddressData($addressID, $typeID, $userID, $streetAddress, $state, $city, $postalCode, $country, $defaultAddress) {
   		if (!get_magic_quotes_gpc()) {
   			addslashes($streetAddress);
   			addslashes($state);
   			addslashes($city);
   			addslashes($country);
   		}
   		$query = "INSERT INTO tbladdress (addressID, typeID, userID, streetAddress, state, city, postalCode, country, defaultAddress, addedOn, lastUpdatedOn) VALUES (\"NULL\", \"$typeID\", \"$userID\", \"$streetAddress\", \"$state\", \"$city\", \"$postalCode\", \"$country\", \"$defaultAddress\", \"$addedOn\", \"NULL\")";
   		$result = mysqli_query(MySQLConnection::connectToMySQL(), $query);
   	    
   	    if (!($result)) {
   	    	die (mysqli_error(MySQLConnection::connectToMySQL()));
   	    }
   	    
   	    echo mysqli_affected_rows(MySQLConnection::connectToMySQL()). ' address(es) have been added to the database.';
   	    mysqli_close(MySQLConnection::connectToMySQL());   	    
   	}
by adding \" \" and works fine....

Thanks,
Chris
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Just for cleanliness sake...

Code: Select all

public static function insertNewAddressData($addressID, $typeID, $userID, $streetAddress, $state, $city, $postalCode, $country, $defaultAddress) {
    if (!get_magic_quotes_gpc()) {
        addslashes($streetAddress);
        addslashes($state);
        addslashes($city);
        addslashes($country);
    }

    $query = "INSERT INTO
	              tbladdress (addressID, typeID, userID, streetAddress, state, city, postalCode, country, defaultAddress, addedOn, lastUpdatedOn)
			  VALUES ('NULL', '$typeID', '$userID', '$streetAddress', '$state', '$city', '$postalCode', '$country', '$defaultAddress', '$addedOn', 'NULL')";

    $result = mysqli_query(MySQLConnection::connectToMySQL(), $query);

    if (!$result) {
        die(mysqli_error(MySQLConnection::connectToMySQL()));
    }

    echo mysqli_affected_rows(MySQLConnection::connectToMySQL()). ' address(es) have been added to the database.';
    mysqli_close(MySQLConnection::connectToMySQL());
}
User avatar
christian_phpbeginner
Forum Contributor
Posts: 136
Joined: Sat Jun 03, 2006 2:43 pm
Location: Java

Post by christian_phpbeginner »

Everah wrote:Just for cleanliness sake...

Code: Select all

$query = "INSERT INTO
                      tbladdress (addressID, typeID, userID, streetAddress, state, city, postalCode, country, defaultAddress, addedOn, lastUpdatedOn)
                          VALUES ('NULL', '$typeID', '$userID', '$streetAddress', '$state', '$city', '$postalCode', '$country', '$defaultAddress', '$addedOn', 'NULL')"; 
hmm...that's actually the way I love it too, and usually I do that too ! I tried that before my last post, but it didn't work at my server, the same error...

Thanks,
Chris
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

What was the error?
Post Reply