How to update details of the current logged in user?

Hi, I need to find a solution on how to update details of the current logged in user…using php & mysql. I need it urgently, my deadline is in a few days time. All suggestions are welcome. Thanks

What have you got so far?

Nothing really in that regards. I am new to php. I have three basic forms that are connected. Form 1 is where external jobs get accepted or decline. Accepted jobs from Form 1 get displayed in the Form 2 to logged in users. Logged in users can then apply or book for one job at the time. When logged in user apply for the job, that job changes the status from “Available” to “Pending” and the query sends data to Form 3 where Admin Accepts or Decline Logged in user. The Admin action updates Form 2 job status, if Accepted then updates the job status in Form 2 to “Booked” if Decline then status changes back to “Available” kind of like “booking system”.
At the moment I have all this working except that query from Form2 to Form3 is hard coded. With hard coded info I was able to see that forms which are interconnected are working, but need to have that hard coded data to be replace with current login details of the user. Here is the query with hard coded info:

if (isset($_POST[‘apply’])) {
mysql_query(“INSERT INTO student_project_applications(uid, name, mail, sid) VALUES (1, ‘bobsmith’, ‘bobs@gmail.com’, “.intval($_POST[‘sid’]).”)”);

Thanks for that. I will try. At the moment only that hard coded entry is shown in From 3 and I need to have that anyone how is logged in (so will be user name) is able to apply for job and query will then display the details of ALL those that they are applied in Form3.

Yesterday there was a reply about global variable that helped me get on the right track, can’t see that reply any more, but thanks anyway, much appreciated :slight_smile:

Also is there are any suggestion of how to send an automated email on submit/apply button. I am guessing i need “if statement” under the submit button… i am beginner with php and dont know the syntax for email confirmation :confused:

Where will the email address be coming from? Is that in the form which the user fills in and presses ‘submit’?

Hi there, no its not in the registration form. There are 3 forms that are interconnected are serving the purpose like a booking system, and there are two scenarios for email issue I have:
Scenario 1:

  1. Public user applies for lets call it “service” by filling out the registration form, on submission “thank you” message appears to confirm registration submission…and that’s working fine as it is uses Web form module.

  2. The submission details are then displayed to admin in the table using my own php code. The application is accepted or decline, either way the automated email should go out to public user to advise the result. The email was mandatory field in registration process mentioned in the step 1, so it’s stored in db.
    Scenario 2:
    When application is accepted (Scenario 1/Step2) then this information is displayed to students who are logged in users. The student’s emails will be their user name for their logins. One of those 3 custom made forms, displays accepted applications (Scenario 1/Step2) for students to apply for. When they apply, the application is reviewed by admin, and same as previous scenario it could be accepted or declined, either way the email should go out when the Save button is hit to advise of results.

    Code for submit button Scenario1:

    if (isset($_POST[‘approvalstat’]) && in_array($_POST[‘approvalstat’],$allowed)) {
    mysql_query(“UPDATE webform_data SET approval_status = '”.mysql_real_escape_string($_POST[‘approvalstat’]).“’
    WHERE nid = “.intval($_POST[‘nid’]).” AND sid = “.intval($_POST[‘sid’]).””);
    echo “Rows updated”;
    //Enter code here to send an email

    }

    Code for submit button Scenario2:

    if (isset($_POST[‘applicationstat’]) && in_array($_POST[‘applicationstat’],$allowed)) {
    mysql_query(“UPDATE stu_applications SET app_status = '”.mysql_real_escape_string($_POST[‘applicationstat’]).“’
    WHERE app_id=”.intval($_POST[‘app_id’])."; ");
    //echo “Rows updated”;
    /if ($_POST[‘applicationstat’] == “Booked”) {
    // Enter code here to send an email
    }
    /
    }
    echo “</table>”;

Well if the email address is coming from the database what you have to do is:

Once the application has been approved and the flag set in your table, go back to the db, get the email address out, and construct all the parts of the email message.

Then send it using the native mail() function.

Unless you envisage 100s or 1000s of emails per day, then using straight mail() should be fine, read all about it in the [fphp]mail[/fphp] section of the manual.

Thanks for that :slight_smile: From reading that link you have sent I understand that message should be constructed at the top of the form?! Right? But where to put mail()? the automated mail should be send when “Apply” button is hit. I am also going to have two different messages…one if application is approved and one if decline. Not sure how syntax for that should look like :frowning:

This is totally untested, and I do not use mysql_* functions, so its up to you to test this line by line, but in answer to your point “What does the syntax look like?”, this is roughly the structure of a script to approve your user, and to then send them an email based on your scenario 1 (I think).


<?php

// if your script does not receive the data it expects then die or re-locate or whatever

if (!isset($_POST['approvalstat']) || !in_array($_POST['approvalstat'],$allowed)) {
echo 'abort - did not pass approval or data was missing';
die();
}

// I will presume all this stuff works
$query = "UPDATE webform_data SET approval_status = '".mysql_real_escape_string($_POST['approvalstat'])."'
WHERE nid = ".intval($_POST['nid'])." AND sid = ".intval($_POST['sid'])."");

// this is taken from mysql_query man page

// Perform Query
$result = mysql_query($query);

// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\
";
    $message .= 'Whole query: ' . $query;
    die($message);
}

// So, up to here you have a) the right data, and b) have put it into your db

// next get the email address from the db -- I guess your email address is stored in a field called email

$email_query = "select email from webform_data where sid = ".intval($_POST['sid']) ;

// Perform Query
$email_result = mysql_query($email_query);

// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$email_result) {
    $message  = 'Invalid email query: ' . mysql_error() . "\
";
    $message .= 'Whole query: ' . $_email_query;
    die($message);
}

$row = mysql_fetch_assoc($email_result);

// have a temporary line of debug here for sanity's sake
var_dump( $row );

$to = $row[0]['email'];
$subject = "your subject";
$message = "your message";

if( mail($to, $subject, $message) ){
echo 'message sent';
}else{
echo 'message not sent';
}
?>

If things go wrong then read the manual page for mysql_query, and the comments.

No doubt this can be improved and better interleaved with your scenario 2, but code you can take from the manual should result in syntax such as this.

AGAIN, untested and if anyone can spot mad errors, please pipe up.

Hi Cups, thanks for going through so much trouble trying to help me, I tried to put the code you sent into my query but getting message ”Rows updatedInvalid query:Query was emptyWhole query:”
When run in db directly query is working fine and returning four records with email fileds. I also commented query and explained that webform stores email in the field name data under cid7 and nid7. Hope I make sense. Any suggestion is welcome. Cheers
Here is my form:

&lt;html&gt;
&lt;body&gt;
&lt;?php
	//Connect to database using script
	
	
	//Retrieve data from a table....query tested in db and its working
	$result = mysql_query("SELECT nid, sid, data, approval_status FROM webform_submitted_data WHERE cid='20' AND nid='7' ")
	or die(mysql_error());
	
	//Dropdown allowed values
	$allowed = array("Approved", "Declined", "Pending");
	
	//Table starts
	echo "&lt;table border='1'&gt;";
	echo "&lt;tr&gt; &lt;th&gt;Project ID &lt;/th&gt; &lt;th&gt;Project Name&lt;/th&gt; &lt;th&gt; Approval Status&lt;/th&gt; &lt;th&gt;Approve/Unapprove&lt;/th&lt;/tr&gt;";
	
	// Keeps getting the next row until there are no more to get
	while($row = mysql_fetch_array( $result)) {
		//Print out the contents of each row into a table
		echo "&lt;tr&gt;&lt;td&gt;";
		echo $row['nid'];
		echo $row['sid'];
		echo "&lt;/td&gt;&lt;td&gt;";
		echo $row['data'];
		echo "&lt;/td&gt;&lt;td&gt;";
		echo $row['approval_status'];
		echo "&lt;/td&gt;&lt;td&gt;";
	


		// Dropdown menu options and save button query
		echo "&lt;form id=form1 method='POST'&gt;
		&lt;select id='approvalstat' name='approvalstat'&gt;
		&lt;option value='-select-'&gt;-Select-&lt;/option&gt;
		&lt;option value='Approved'&gt;Approved&lt;/option&gt;
		&lt;option value='Declined'&gt;Declined&lt;/option&gt;
		&lt;option value='Pending'&gt;Pending&lt;/option&gt;
		&lt;/select&gt;
		&lt;input type='hidden' value='".$row['nid']."' name='nid'&gt;
		&lt;input type='hidden' value='".$row['sid']."' name='sid'&gt;
        &lt;input id='sub' type='submit' value='Save' onClick='window.location.reload()'&gt;
		&lt;input type='button' value='Reload Page' onClick='window.location.reload()'&gt;";
		echo "&lt;/form&gt;";
		echo "&lt;/td&gt;&lt;/tr&gt;";
	}
	if (isset($_POST['approvalstat']) && in_array($_POST['approvalstat'],$allowed)) {
		mysql_query("UPDATE webform_submitted_data SET approval_status = '".mysql_real_escape_string($_POST['approvalstat'])."'
		WHERE nid = ".intval($_POST['nid'])." AND sid = ".intval($_POST['sid'])."");
		echo "Rows updated";
		
		//query is working when tested in db...pls note the registration process is done using webform and the data is stored in rows rather then columns (A NIGHTMARE!!)
		//and email is stored under field data, but to distinguish need to use nid=7 and cid=7 Cid is component id and it is row where email data is stored
		$email_query = "SELECT data FROM webform_submitted_data WHERE `webform_submitted_data`.`nid` =7  AND`webform_submitted_data`.`cid` =7 AND sid = ".intval($_POST['sid']) ;

		// So, up to here you have a) the right data, and b) have put it into your db...YES THERE ARE 4 RECORDS IN DB
		// next get the email address from the db -- I guess your email address is stored in a field called email....NO, as explained coupole of lines above
		// Perform Query
		$result = mysql_query($query);

		// Check result
		// This shows the actual query sent to MySQL, and the error. Useful for debugging.
		if (!$result) {
		$message  = 'Invalid query: ' . mysql_error() . "\
";
		$message .= 'Whole query: ' . $query;
		die($message);
		}

		// Perform Query
		$email_result = mysql_query($email_query);

		// Check result
		// This shows the actual query sent to MySQL, and the error. Useful for debugging.
		if (!$email_result) {
		$message  = 'Invalid email query: ' . mysql_error() . "\
";
		$message .= 'Whole query: ' . $_email_query;
		die($message);
		}

		$row = mysql_fetch_assoc($email_result);

		// have a temporary line of debug here for sanity's sake
		var_dump( $row );

		$to = $row[0]['email'];
		$subject = "Project approvals";
		$message = "it would be so great if it's works ";

		if( mail($to, $subject, $message) ){
		echo 'message sent';
		}else{
		echo 'message not sent';
		}
	}
	echo "&lt;/table&gt;";
?&gt;
&lt;/body&gt;
&lt;/html&gt;

$to = $row[0]['email'];

from what you say, the line above should then read:


$to = $row[0]['data'];

Thanks, of course I should have picked that one. With that fixed it giving me this error: Rows updatedInvalid query: “Query was empty Whole query”.
It also this message pops up to:
• Notice: Undefined variable: query in eval() (line 59 of \drupal\modules\php\php.module(80) : eval()'d code).
• Notice: Undefined variable: query in eval() (line 65 of \drupal\modules\php\php.module(80) : eval()'d code).

Here what is in that php.module file from line 59 to the line 88:


 * @ingroup php_wrappers	//this is line 59
  *
 * @see php_filter_info()
 */
function php_eval($code) {
  global $theme_path, $theme_info, $conf;
				//this blank line is line 65
  // Store current theme path.
  $old_theme_path = $theme_path;

  // Restore theme_path to the theme, as long as php_eval() executes,
  // so code evaluated will not see the caller module as the current theme.
  // If theme info is not initialized get the path from theme_default.
  if (!isset($theme_info)) {
    $theme_path = drupal_get_path('theme', $conf['theme_default']);
  }
  else {
    $theme_path = dirname($theme_info->filename);
  }

  ob_start();
  print eval('?>' . $code);
  $output = ob_get_contents();
  ob_end_clean();

  // Recover original theme path.
  $theme_path = $old_theme_path;

  return $output;
}

Sorry, I’m no Drupal user, perhaps someone else can help?