Main
Index
Search
Posts
Who's
Online
Log
In

Home: Products: CMS Builder:
Using addForm.php - insert logged in user num

 

 


gversion
User

Dec 31, 2011, 4:50 AM

Post #1 of 7 (1017 views)
Shortcut
Using addForm.php - insert logged in user num Can't Post

Hello,

I'm trying to setup the form, addForm.php and would like to auto-insert the I'd number of the currently logged in user so the new listing is associated with the logged in user and not the default" 0".

I have changed the code to be as follows, but it's not working:


Code
createdDate      = NOW(), 
                      updatedDate      = NOW(),
                      createdByUserNum = '".mysql_escape($CURRENT_USER['num'])."',
                      updatedByUserNum = '0'")


Could someone please help me out?

Thank you,
Greg


Jason
Staff / Moderator


Dec 31, 2011, 9:10 AM

Post #2 of 7 (1013 views)
Shortcut
Re: [gversion] Using addForm.php - insert logged in user num [In reply to] Can't Post

Hi Greg,

You should also assign updatedByUserNum to be the num of the currently logged in user like this:


Code
createdDate      = NOW(),  
updatedDate = NOW(),
createdByUserNum = '".mysql_escape($CURRENT_USER['num'])."',
updatedByUserNum = '".mysql_escape($CURRENT_USER['num'])."'")


Hope this helps. If you are still experiencing trouble, please let us know the exact nature of the problem and we can look into it further.

Thanks
---------------------------------------------------
Jason Sauchuk - Programmer 
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/ 


gversion
User

Dec 31, 2011, 10:09 AM

Post #3 of 7 (1010 views)
Shortcut
Re: [Jason] Using addForm.php - insert logged in user num [In reply to] Can't Post

Hi Jason,

Thanks for help. I have updated the code as you suggested but when I view the listing in the cmsAdmin area it still says:


Quote
Created Dec 31st, 2011 - 06:02:52 PM (by Unknown) change
Last Updated Dec 31st, 2011 - 06:02:52 PM (by Unknown)


How can I make it display the username of the person that created the listing, rather than "unknown"?

Thanks again,
Greg


Jason
Staff / Moderator


Dec 31, 2011, 12:43 PM

Post #4 of 7 (1000 views)
Shortcut
Re: [gversion] Using addForm.php - insert logged in user num [In reply to] Can't Post

Hi Greg,

That comes up when the value of createdByUserNum or updatedByUserNum is not a valid user number.

One potential cause is that the person is not logged in when the record is created.

Does your form force a person to be logged in before they can create a record?

If this doesn't fix it, please attach the .php file you're working with so I can take a closer look at your code.

Thanks
---------------------------------------------------
Jason Sauchuk - Programmer 
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/ 


gversion
User

Dec 31, 2011, 6:28 PM

Post #5 of 7 (993 views)
Shortcut
Re: [Jason] Using addForm.php - insert logged in user num [In reply to] Can't Post

Hi Jason,

Thanks for the help.

I've made the form only accessible to users that have logged in but I'm still getting "unknown" for createdBy and updatedBy.

Here is the code I'm using:


Code
<?php 
  require_once "cmsAdmin/lib/init.php";

// submit form
if (@$_REQUEST['submit']) {

  // error checking
  $errorsAndAlerts = "";
  if (!@$_REQUEST['product'])    { $errorsAndAlerts .= "Please specify title!<br/>\\n"; }
  if (!@$_REQUEST['description'])  { $errorsAndAlerts .= "Please specify content!<br/>\\n"; }

  // turn off strict mysql error checking for: STRICT_ALL_TABLES
  mysqlStrictMode(false); // disable Mysql strict errors for when a field isn't defined below (can be caused when fields are added later)

  // add record
  if (!@$errorsAndAlerts) {
    mysql_query("INSERT INTO `{$TABLE_PREFIX}wanted_ads` SET
                      product            = '".mysql_real_escape_string( $_REQUEST['product'] )."',
                      description          = '".mysql_real_escape_string( $_REQUEST['description'] )."',

                      createdDate      = NOW(),  
updatedDate      = NOW(),  
createdByUserNum = '".mysql_escape($CURRENT_USER['num'])."',  
updatedByUserNum = '".mysql_escape($CURRENT_USER['num'])."'")

    or die("MySQL Error Creating Record:<br/>\\n". htmlspecialchars(mysql_error()) . "\\n");
    $recordNum = mysql_insert_id();

    // display thanks message and clear form
    $errorsAndAlerts = "Thanks, we've added that record!";
    $_REQUEST = array();
  }

}

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

 <?php require_once "cmsAdmin/lib/viewer_functions.php"; ?>
<?php if (!$CURRENT_USER) { websiteLogin_redirectToLogin(); } ?>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style type="text/css">
  body, td { font-family: arial }
</style>
</head>
<body>

<form method="post" action="">
<input type="hidden" name="submit" value="1" />

<h1>Sample Record Add Form</h1>

<?php if (@$errorsAndAlerts): ?>
  <div style="color: red; font-weight: bold; font-size: 16px;"><br/>
    <?php echo $errorsAndAlerts; ?><br/><br/>
  </div>
<?php endif ?>


<table border="0" cellspacing="0" cellpadding="2">
 <tr>
  <td valign="top">Product</td>
  <td><input type="text" name="product" value="<?php echo htmlspecialchars(@$_REQUEST['product']) ?>" size="30" /></td>
</tr>
 <tr>
  <td valign="top">Description</td>
  <td><textarea name="description" cols="30" rows="4"><?php echo htmlspecialchars(@$_REQUEST['description']) ?></textarea></td>
</tr>
</table><br/><br/>


<input type="submit" name="add" value="Add Record &gt;&gt;" />

</form>
</body>
</html>


Thanks again fo all you help.

Regards,
Greg


Jason
Staff / Moderator


Jan 2, 2012, 9:27 AM

Post #6 of 7 (973 views)
Shortcut
Re: [gversion] Using addForm.php - insert logged in user num [In reply to] Can't Post

Hi Greg,

I think I may see the problem.

At the very top of your page, you have a require_once() statement to require cmsAdmin/lib/init.php.

Then, down below your code that populates the database you require the viewer_functions.php library. This may be affecting the value of $CURRENT_USER. Try this change, remove this line:


Code
require_once "cmsAdmin/lib/init.php";


and replace it with this:

Code
require_once "cmsAdmin/lib/viewer_functions.php"; 

if (!$CURRENT_USER) { websiteLogin_redirectToLogin(); }


This way you are assuring that $CURRENT_USER has a value before anything is entered into the database.

Hope this helps
---------------------------------------------------
Jason Sauchuk - Programmer 
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/ 


gversion
User

Jan 2, 2012, 3:00 PM

Post #7 of 7 (969 views)
Shortcut
Re: [Jason] Using addForm.php - insert logged in user num [In reply to] Can't Post

Hi Jason,

That's worked! :) Thanks for taking the time to help me with that one.

Regards,
Greg