Page 1 of 1
[SOLVED] Inserting data to database
Posted: Mon Nov 13, 2006 7:44 am
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
Posted: Mon Nov 13, 2006 9:08 am
by volka
it always failed.
meaning
die ("Failed inserting address data into database."); is executed?
then try
http://de3.php.net/mysqli_error
Posted: Mon Nov 13, 2006 11:39 am
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...
Posted: Mon Nov 13, 2006 12:15 pm
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
Posted: Mon Nov 13, 2006 12:48 pm
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());
}
Posted: Mon Nov 13, 2006 12:51 pm
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
Posted: Mon Nov 13, 2006 12:54 pm
by RobertGonzalez
What was the error?