Hi richei,
Thank you for your reply and help.
Unfortunately correcting my code to your suggestions still doesn’t work I’m tearing my hair out!
Sorry the above message was directed at dex not richie
Hi user23,
Don’t worry about it. I’m sure its not a big problem. Anyway, i want to help but i don’t have all your source code, so i have to assume that your insert code are working fine.
You mentioned that you have radio buttons attached to the subject but when i really look at your code, you are actually using checkboxes.
I want to know what intentions do you have when inserting the record. When you insert the subjects into the db, is it like math, english and etc? I mean the subject is all combined together and saved in a column?
What is your actual problem that you are facing? You can’t insert the record at all? Or you can’t insert more than one subject?
Regards,
developer.dex2908
Thanks again.
Yes sorry I changed the radio buttons to checkboxes. Yes I wanted the user to check the boxes of the school subjects they do. Then I have named a field in the table subject_name where the subjects should be added accordingly.
The Register page is the page with the initial registration
[php]<?php
include ‘core/init.php’;
logged_in_redirect();
include ‘includes/overall/overallheader.php’;
if (empty($_POST) === false) {
$required_fields = array(‘username’, ‘password’, ‘first_name’, ‘last_name’, ‘email’);
foreach($_POST as $key=>$value) {
if (empty($value) && in_array($key, $required_fields) === true){
$errors[] = ‘Fields marked with an asterisk are required’;
break 1;
}
}
if (empty($errors) === true) {
if (user_exists($_POST['username']) === true){
$errors[] = 'Sorry, the username \'' . $_POST['username'] . '\' is already taken.';
}
if (preg_match("/\\s/", $_POST['username']) == true) {
$errors[] = 'Your username must not contain any spaces.';
}
if (strlen($_POST['password']) < 6) {
$errors[] = 'Your password must be at least 6 characters';
}
if ($_POST['password'] !== $_POST['password_again']) {
$errors[] = 'Your passwords do not match';
}
if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false) {
$errors[] = 'A valid email address is required';
}
if (email_exists($_POST['email']) === true){
$errors[] = 'sorry the email \'' . $_POST['email'] . '\' is already in use';
}
}
}
?>
Register
<?php if (isset($_GET['success']) && empty($_GET['success'])) { //if success is added to end of url echo 'You\'ve been registered successfully!'; } else { if (empty($_POST) === false && empty($errors) === true) { //if post data is not empty and there are no errors we can register user $register_data = array( 'username' => $_POST['username'], 'password' => $_POST['password'], 'first_name' => $_POST['first_name'], 'last_name' => $_POST['last_name'], 'email' => $_POST['email'], 'school' => $_POST['school'], 'subject_name[]' => $_POST['subject_name[]'], 'email_code' => md5($_POST['username'] + microtime()) ); register_user($register_data); header('Location: register.php?success'); exit(); }else if (empty($errors) === false){ echo output_errors($errors); } ?><ul>
<li>--------------Personal Details-----------------</li>
<br />
<li>
Username*:<br>
<input type="text" name="username">
</li>
<li>
Password*:<br>
<input type="password" name="password">
</li>
<li>
Password again*:<br>
<input type="password" name="password_again">
</li>
<li>
First Name*:<br>
<input type="text" name="first_name">
</li>
<li>
Last Name:<br>
<input type="text" name="last_name">
</li>
<li>
Email*:<br>
<input type="text" name="email">
</li>
<br />
<br />
<li>
-----------------Subjects Area ------------------
</li>
<br />
Select your school:
St Marys, Charleville
CBS, Charleville
<li>
<input type="submit" value="register">
</li>
</ul>
<?
}
include 'includes/overall/overallfooter.php'; ?>
[/php]
To my understanding then, the get user.php page is what is causing the subjects from the subjects table to be printed which are linked to the school table. So one school1 may only offer maths, eng, art, music for example.
Previous to adding the school drop down box and checkboxes, the records were adding to the db (they were just text boxes were data was input) However, now no new records at all are being added to the db, not even name, email etc
Thanks
This is the get user.php file
[php]<?php
$q=$_GET[“q”];
$con = mysql_connect(‘localhost’, ‘root’, ‘root’);
if (!$con)
{
die('Could not connect: ’ . mysql_error());
}
mysql_select_db(“lr”, $con);
$sql=“SELECT subject_name FROM subjects WHERE id = '”.$q."’";
$result = mysql_query($sql);
echo "
Please Select your subjects | |
---|---|
” . $row[‘subject_name’] . “ | ”;”; echo “”; echo “ | ”;
mysql_close($con);
?>[/php]
Hi user23,
In that case, you need to print out your sql statement and check the syntax. I can’t debug it for you since i don’t have your source code.
You have see the FINAL sql statement that you have supplied to be executed. You’ll be able to figure out what’s wrong then.
Regards,
developer.dex2908
I just presumed the sql generated itself as you fill out the registration form… i dont know is that ridiculous… I’m really confused as to what I’m looking for tbh! …
The forum wouldnt let me post my sql code, so i posted it on speedy share, if you could have a look at it that’d be great!
http://speedy.sh/UpJhd/test.sql
Is it just the sql file you need?
Thank you for your help
Hi user23,
I don’t mean the .sql file. I meant when you call this line register_user($register_data); What is the code for register_user? It should execute some insert statements inside there am i right? I need that. Not the sql file.
Regards,
developer.dex2908
Hi Dex, sorry about that… i presume its the users.php file?
[php]<?php
function activate($email, $email_code){
$email = mysql_real_escape_string($email);
$email_code = mysql_real_escape_string($email_code);
if (mysql_result(mysql_query(“SELECT COUNT(user_id
) FROM users
WHERE email
= ‘$email’ AND email_code
= ‘$email_code’ AND active
=0”), 0) ==1){
mysql_query(“UPDATE users
SET active
= 1 WHERE email
= ‘$email’”);
return true;
} else {
return false;
}
}
function has_access($user_id, $type) {
$user_id = (int)$user_id;
$type = (int)$type;
return (mysql_result(mysql_query(“SELECT COUNT(user_id
) FROM users
WHERE user_id
= $user_id AND ‘type’ = $type”), 0) == 1) ? true : false;
}
function update_user($update_data) {
global $session_user_id;
$update = array();
array_walk($update_data, ‘array_sanitize’);
foreach($update_data as $field=>$data) {
$update[] = '`' . $field . '` = \'' . $data . '\'';
}
mysql_query("UPDATE `users` SET " . implode(', ', $update) . "WHERE `user_id` = $session_user_id");
}
function change_password($user_id, $password) {
$user_id = (int)$user_id;
$password = md5 ($password);
mysql_query("UPDATE `users` SET `password` = '$password' WHERE `user_id` = $user_id");
}
function register_user($register_data) {
array_walk($register_data, ‘array_sanitize’);
$register_data[‘password’] = md5($register_data[‘password’]);
$fields = '`' . implode('`, `', array_keys($register_data)) . '`';
$data = '\'' . implode('\', \'', $register_data) . '\'';
mysql_query("INSERT INTO `users` ($fields) VALUES ($data)");
email($register_data['email'], 'Activate your account', "Hello " . $register_data['first_name'] . ", \n\n You need to activate your account, so use the link below:\n\n http://www.ischool.freeiz.com/lr/activate.php?email=" . $register_data['email'] . "&email_code=" . $register_data['email_code'] . " \n\n -administrator" );
}
function user_count() {
return mysql_result(mysql_query(“SELECT COUNT(user_id
) FROM users
WHERE active
= 1”), 0);
}
function user_data($user_id) {
$data = array();
$user_id = (int)$user_id;
$func_num_args = func_num_args();
$func_get_args = func_get_args();
if ($func_num_args > 1) {
unset($func_get_args[0]);
$fields = '`' . implode('`, `', $func_get_args) . '`';
//echo "SELECT $fields FROM `users` WHERE `user_id` = $user_id";
$data = mysql_fetch_assoc(mysql_query("SELECT $fields FROM `users` WHERE `user_id` = $user_id"));
return $data;
}
}
function logged_in () {
return (isset($_SESSION[‘user_id’])) ? true : false;
}
function user_exists($username) {
$username = sanitize($username);
$QUERY = mysql_query(“SELECT COUNT(user_id
) FROM users
WHERE username
= ‘$username’”);
return (mysql_result($QUERY, 0) == 1) ? true :false;
}
function email_exists($email) {
$email = sanitize($email);
$QUERY = mysql_query(“SELECT COUNT(user_id
) FROM users
WHERE email
= ‘$email’”);
return (mysql_result($QUERY, 0) == 1) ? true :false;
}
function user_active($username) {
$username = sanitize($username);
$QUERY = mysql_query(“SELECT COUNT(user_id
) FROM users
WHERE username
= ‘$username’ AND active
= 1”);
return (mysql_result($QUERY, 0) == 1) ? true :false;
}
function user_id_from_username($username) {
$username = sanitize($username);
return mysql_result(mysql_query(“SELECT user_id
FROM users
WHERE username
= ‘$username’”), 0, ‘user_id’);
}
function login($username, $password) {
$user_id = user_id_from_username($username);
$username = sanitize ($username);
$password = md5 ($password);
return (mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username' AND `password` = '$password'"), 0) == 1) ? $user_id : false;
}
?>
[/php]
Hi user23,
Yup, that’s the right file. Now, at register_user function, you need to check what is the actual sql statement being passed to the mysql_query for execution. I mean like this:
[php]
function register_user($register_data) {
array_walk($register_data, ‘array_sanitize’);
$register_data[‘password’] = md5($register_data[‘password’]);
$fields = ‘' . implode('
, ', array_keys($register_data)) . '
‘;
$data = ‘’’ . implode(’’, ‘’, $register_data) . ‘’’;
echo “INSERT INTO users
($fields) VALUES ($data)”;
//mysql_query(“INSERT INTO users
($fields) VALUES ($data)”);
//email($register_data[‘email’], ‘Activate your account’, “Hello " . $register_data[‘first_name’] . “, \n\n You need to activate your account, so use the link below:\n\n http://www.ischool.freeiz.com/lr/activate.php?email=” . $register_data[‘email’] . “&email_code=” . $register_data[‘email_code’] . " \n\n -administrator” );
}
[/php]
Look at what is being echoed to your browser. What is the insert statement like? Any syntax errors?
Regards,
developer.dex2908
Hi Dex,
I echoed this in the register.php page and it printed
[php]]NSERT INTO users
() VALUES ()[/php]
*INSERT
but to be honest since i added the dropboxes and checkboxes, i didnt touch the register_user function file directly
Hi user23,
Since the fields and the insert statement is incomplete, i suspect there is something wrong with your array_sanitize function. I couldn’t find that function.
Also, i noticed that you are using longtext type for the subject_name column. Why don’t you use varchar instead? I’ve seen bugs when using longtext. For the purpose of this test, perhaps you can change the type to varchar? Most probably, this is not the cause, but no harm trying.
Do try to look into you array_sanitize function.
Regards,
developer.dex2908
Dex thank you very much for your continued help. i changed to varchar but it made no change.
This is the file that contains the sanitise function
[php]<?php
function email($to, $subject, $body){
mail($to, $subject, $body, ‘From: [email protected]’);
}
function logged_in_redirect(){
if (logged_in() === true){
header(‘Location: index.php’);
exit();
}
}
function protect_page() { // function so can use on all protected pages, not repeat code
if (logged_in() === false){
header(‘Location: protected.php’);
exit();
}
}
function admin_protect() {
global $user_data;
if (has_access($user_data[‘user_id’], 1) === false) {
header(‘Location: index.php’);
exit();
}
}
function array_sanitize(&$item) {
$item = mysql_real_escape_string($item);
}
function sanitize($data) {
return mysql_real_escape_string($data);
}
//function needed so have option to sanitize additional data in future
function output_errors($errors) {
return ‘
- ’ . implode(’
- ’, $errors) . ‘
}
?>[/php]
To be honest I think it is to do with the get user.php file being on a separate page to the array …like the subject data is being inputted to the get user.php page but th array is on the register.php page but i just can’t fix it and iv spent too long on it already
I just removed the subject code (get user.php and ajax) and when i just use the school dropbox and above details it works properly.
Its like the ajax is obstructing the code from working?
Hi user23,
Can you give me all the files? I could debug it way faster that way because as of now, i’m looking only at partial code and there could be a lot of different factors that can lead to a bug. But anyways, your form page looks weird though. Why is the form tag before the html tag? You can remove the tag that is enclosing the select tag and put the form tag(the one before html) and bring it inside the body tag.
Regards,
developer.dex2908
Hi Dex,
yes of course
I have uploaded the files to speedy share, this is the link http://speedy.sh/CyMbb/lr-3.zip
Thank you very much for your help!
Hi user23,
I’ve manage to find out the problem. There are few errors each in different file. Its very difficult to guide you how. Perhaps i can email to you?
Regards,
developer.dex2908
Btw, don’t wait for me to email you now though, its already 2am in my place. I’ll try to email you by tomorrow. Don’t worry its fixed!!! ;D
Regards,
developer.dex2908
Oh my god thats so great!! Thank you so much, iv sent my email address in a message!
Thanks