First some observations you should be using prepared statements, I have no idea what db_escape() is (I’m guessing some self-made escape function that is useless if you use prepared statements) and I’m also guessing you don’t have error reporting turned on. I would suggest using PDO instead of mysqli for in my opinion it’s easier to implement and you are NOT tied down to MySQL database.
With that said I’m guessing custid is either not being passed in (even though it supposedly passed in with sessions and that’s why error reporting is nice to have on locally
) or you actually want the mysql database to auto increment custid, so that probably means your database table is not set up properly.
One last thing, stop using global variables for that is a bad habit to get into and makes debugging harder (case in point your particular problem).
If you insist on using a function for doing writing and/or reading to a database table(s) then pass the database connection and variable(s) into the function, for example:
[php]function insert_address(array $address, $custId = \NULL, $db = \NULL) {
/* You code goes here */
} [/php]
That’s my input and I’m sure others here have theirs as well. HTH John
P.S. To prove that I practice what I preach here’s me passing an array to my database tables via a method (another term for function).
[php]if (isset($submit) && $submit === ‘register’) {
$data['username'] = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$data['password'] = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$data['verify_password'] = filter_input(INPUT_POST, 'verify_password', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$data['full_name'] = filter_input(INPUT_POST, 'full_name', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$data['email'] = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
$data['verify_email'] = filter_input(INPUT_POST, 'verify_email', FILTER_SANITIZE_EMAIL);
$data['private'] = filter_input(INPUT_POST, 'private', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
if (empty($data['private'])) {
$data['private'] = 'no';
}
$validate = new FormValidation($data); // Pass the array to a validation class:
if ($validate->result) {
/* All the data has validated proceed to saving and sending email verification */
$sendEmail = new FormVerification();
$result = $sendEmail->sendEmailVerification($data); // Passing an array to a class:
if ($result) {
$data['security_level'] = 'public';
$data['confirmation_code'] = $sendEmail->confirmationNumber;
$finalResult = $users->create($data); // Pass the same array to another class (Users class I believe):
if ($finalResult) {
header('Location: notice.php');
exit();
}
}
} else {
$invalid = TRUE; // Invalid Data being sent to Form - Have user re-enter:
}
}[/php]
My database connection gets passed into the class a different way, but it’s still not done by a global variable.