Help with php and mysql

Help with php and mysql

Page 1 of 1

3 Replies - 3150 Views - Last Post: 25 June 2008 - 03:00 AM Rate Topic: -----

#1 Romu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-May 08

Help with php and mysql

Posted 23 June 2008 - 04:45 PM

I have a problem with the part of the code in red here. I need to add info from table called mdl_user to table called Person, I donno why this part of the code is not executing it shows me "Could not add information to the database whenever I am testing the code. Please help me.




<?php // $Id: signup.php,v 1.41.2.1 2005/07/15 00:25:27 stronk7 Exp $
include("navigator2.php");
	require_once("../config.php");
	require_once("../lib/smtp.php");
	//require_once("../lib/moodlelib1.php");
	require_once("../auth/$CFG->auth/lib.php");

	//HTTPS is potentially required in this page
	httpsrequired();
	$entry2= $_GET['username'];
	//echo $entry2;
	//echo $user;
	if ($CFG->auth != 'email' and (empty($CFG->auth_user_create) or !(function_exists('auth_user_create'))) ) {
		error("Sorry, you may not use this page.");
	}

	if ($user = data_submitted()) {

		$user->firstname = strip_tags($user->firstname);
	  $user->middle = strip_tags($user->middle);	
		$user->lastname = strip_tags($user->lastname);
		$user->phone1 = strip_tags($user->phone1);
		$user->email = strip_tags($user->email);
	   // echo $user->email;
		validate_form($user, $err);

		  // format username

		$user->username= trim(moodle_strtolower($user->username));

		  // substitute county name for FIPS
	
		$link = mysql_connect('localhost', 'moodleuser', 'IFASmoodle');
		if (!$link) 
		  die('Could not connect: ' . mysql_error());

		mysql_select_db('moodle',$link);

		$query = "select county from StateCounty where fipsCode='$user->county'";

		$result = mysql_query($query,$link);
 
		if(mysql_num_rows($result) > 0) 
		  $user->county = mysql_result($result,0);

		if (count((array)$err) == 0) {
			$plainpass = $user->password;
			$user->password = md5($user->password);
			$user->confirmed = 0;
			$user->lang = current_language();
			$user->firstaccess = time();
			$user->secret = random_string(15);
			//echo $user->secret;
			$user->auth = $CFG->auth;
			if (!empty($CFG->auth_user_create) and function_exists('auth_user_create') ){
				if (! auth_user_exists($user->username)) {
					if (! auth_user_create($user,$plainpass)) {
						error("Could not add user to authentication module!");
					}
				} else {
					error("User already exists on authentication database.");
				}
			}

			if (! ($user->id = insert_record("user", $user) ) ) {
				error("Could not add your record to the database!");
			}

[color=#FF0000]$queryperson="INSERT INTO Person (id,moodleUserID,firstName,middle,lastName,address,city,county,state,zip,phone,email,ccaID,occupation,employer,cropSpeciality)  SELECT 	id,username,firstname,middle,lastname,address,city,country,state,zip,phone1,email,cca,occupation,employer,cropSpeciality from mdl_user where id = 'user->id'";

	 $result_1 = mysql_query($queryperson,$link);

	if(!($result_1)) {
		error("Could not add your information to the database");
	}[/color]

										  

		   /* if (! send_confirmation_email($user)) {
				error("Tried to send you an email but failed!");
			}*/
			$smtp=new smtp_class;
			$domain="128.227.74.56";
			$message="Thanks for registering. Please confirm by clicking the link";
			$port=0;
			$resolve_message=30;
			//echo "Value is".$smtp->Connect($domain);
			$from1="hwb@ufl.edu";
			//echo $user->email;
			
			$site = get_site();
	$from = get_admin();

	$data->firstname = fullname($user);
	$data->sitename = $site->fullname;
	$data->admin = fullname($from) .' ('. $from->email .')';
	$data->link = $CFG->wwwroot .'/login/confirm.php?p='. $user->secret .'&s='. $user->username;
	$message = get_string('emailconfirmation', '', $data);
	//echo "message is".$message;
			 if($smtp->SendMessage($from1,
											   array($user->email,"npdn@ifas.ufl.edu"),
											   array("From: $from1",
										 "To: $user->email,npdn@ifas.ufl.edu",
										 "Subject: NPDN User Registration confirmation",
										 "Date: ".strftime("%a, %d %b %Y %H:%M:%S %Z")
											),
									   $message
						   ))
			 echo "";
	   // echo "Message has been sent to your email address Please check your email and follow directions to confirm"; 
			 else
		echo "Cound not send the message to $to.\nError: ".$smtp->error."\n";
	
			$emailconfirm = get_string("emailconfirm");
			print_header($emailconfirm, $emailconfirm, $emailconfirm);
			notice(get_string("emailconfirmsent", "", $user->email), "$CFG->wwwroot/");
			exit;
		}
	}

	if (!empty($err)) {
		$focus = "form.".array_shift(array_flip(get_object_vars($err)));
	} else {
		$focus = "";
	}

	if (empty($user->country) and !empty($CFG->country)) {
		$user->country = $CFG->country;
	}

	$newaccount = get_string("newaccount");
	$login = get_string("login");

	if (empty($CFG->langmenu)) {
		$langmenu = "";
	} else {
		$currlang = current_language();
		$langs	= get_list_of_languages();
		$langmenu = popup_form ("$CFG->wwwroot/login/signup.php?lang=", $langs, "chooselang", $currlang, "", "", "", true);
	}

	print_header($newaccount, $newaccount, "<a href=\"index.php\">$login</a> -> $newaccount", $focus, "", true, "<div align=\"right\">$langmenu</div>");
	include("signup_form.html");
	print_footer();



/******************************************************************************
 * FUNCTIONS
 *****************************************************************************/

function validate_form($user, &$err) {
	global $CFG;

	if (empty($user->username)){
		$err->username = get_string("missingusername");
	} else{
		$user->username = trim(moodle_strtolower($user->username));
		if (record_exists("user", "username", $user->username)){
			$err->username = get_string("usernameexists");
		} else {
			if (empty($CFG->extendedusernamechars)) {
				$string = eregi_replace("[^(-\.[:alnum:])]", "", $user->username);
				if (strcmp($user->username, $string)) {
					$err->username = get_string("alphanumerical");
				}
			}
		}
	}

	if (isset($CFG->auth_user_create) and $CFG->auth_user_create==1 and function_exists('auth_user_exists') ){
		if (auth_user_exists($user->username)) {
			$err->username = get_string("usernameexists");
		}
	}		 

	/*if (empty($user->cca)) {
		$err->cca = get_string("missingcca");
	}*/

	if (empty($user->password)) {
		$err->password = get_string("missingpassword");
	}

	if (empty($user->firstname)) {
		$err->firstname = get_string("missingfirstname");
	}
		
	if (empty($user->lastname)) {
		$err->lastname = get_string("missinglastname");
	}

   if (empty($user->cropSpeciality) || $user->cropSpeciality == "NONE") {
		$err->cropSpeciality = "Missing crop Speciality"; // get_string("missingcropspeciality");
	}

		

	if (empty($user->email)) {
		$err->email = get_string("missingemail");
		
	} else if (! validate_email($user->email)) {
		$err->email = get_string("invalidemail");
	
	} else if (record_exists("user", "email", $user->email)) {
		$err->email = get_string("emailexists")." <a href=\"forgot_password.php\">".get_string("newpassword")."?</a>";
	}
	

	if (empty($user->email2)) {
		$err->email2 = get_string("missingemail");

	} else if ($user->email2 != $user->email) {
		$err->email2 = get_string("invalidemail");
	}

/*
	if (empty($user->city)) {
		$err->city = get_string("missingcity");
	}
*/
	  // this works for state and county but does not display error message

	if (empty($user->state) || $user->state == "NONE") {
		$err->state = "Missing State"; // get_string("missingstate");
	}

	if (empty($user->county) || $user->county == "NONE") {
		$err->county = "Missing County"; // get_string("missingcounty");
	}

	if (empty($user->country)) {
		$err->country = get_string("missingcountry");
	}

	if (empty($err->email)) {
		if ($error = email_is_not_allowed($user->email)) {
			$err->email = $error;
		}
	}

	return;
}


?>



** Edit ** :code:

Is This A Good Question/Topic? 0
  • +

Replies To: Help with php and mysql

#2 jstephens  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 214
  • Joined: 07-November 05

Re: Help with php and mysql

Posted 24 June 2008 - 09:53 PM

Problem is that you are not giving any information to insert into the database. proper statement would be
Example of an Insert Statement.

$query = "INSERT INTO users(user_id, username, password, Firstname, Lastname)
				values(1,'myUsername', 'myPassword', 'myFirstname', 'myLastname')";


Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 4334
  • View blog
  • Posts: 12,130
  • Joined: 18-April 07

Re: Help with php and mysql

Posted 24 June 2008 - 10:40 PM

Well he is giving information to insert in the form of a nested select, but the problem is that there are some problems and a syntax error.

First of all, I am not sure why there is a "& #40" and a "& nbsp;" in there. Those don't belong in a query.

Second thing is that you do a user->id but you forgot a dollar sign for "user" since that is a variable.

Lastly, make sure that moodleUserID is a string because you are attempting to put "username" into it. If moodleUserID is an integer, you will have to insert a number into that field, not a string.

To help you troubleshoot, try running the query inside a query tool on the database first and see if the query works. This will also give you an immediate error message that you can use to debug the query. Once you have it working there, then put it into PHP.

Hope this helps! :)
Was This Post Helpful? 0
  • +
  • -

#4 bipula  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-June 08

Re: Help with php and mysql

Posted 25 June 2008 - 03:00 AM

I have been in the same problem from 2 days. I have moodle 1.8.3 and everything was working well in localhost. But once I uploaded to the server I get error while user registration Could not add your record to the database!. But the login works well.

I dont know why this error is coming. I need to solve this as soon as possible.

I found that tables are different once I have uploaded. The NULL field has "no" in localhost and "yes" in live server. I cannot change that too. The change is not seen.

I don't know if its the version of php that is giving this problem.
I need to solve it.
Please help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1