PDA

View Full Version : How Can I insert the username of who submitted a form to the database?


EquinoxWorld
07-13-2011, 05:15 PM
Hello everyone, I am trying to capture the username of whoever submits a custom form I created. I need to insert that username of who submits the form into a database table I created. I need this to first have a record of who submitted the form and also to later print out the username next to the form submitted, as well as restrict later access to that form using if conditions, but first I need to get that username. Any one has any ideas how to go about this or point me in the right direction?? Please any information would be very useful. Thank you for your time everyone. :)

kh99
07-13-2011, 05:24 PM
I think the username should be available in $vbulletin->userinfo['username'].

EquinoxWorld
07-13-2011, 05:27 PM
Right but how does the form know that? Should I just insert that while I'm inserting the form values? I don't get how that knows that that form was submitted by that user. Anyone can explain please??

This is my form:
{vb:stylevar htmldoctype}
<html xmlns="http://www.w3.org/1999/xhtml" dir="{vb:stylevar textdirection}" lang="{vb:stylevar languagecode}" id="vbulletin_html">
<head>
<title>{vb:raw vboptions.bbtitle} - {vb:raw pagetitle}</title>
{vb:raw headinclude}
{vb:raw headinclude_bottom}
</head>
<body>

{vb:raw header}

{vb:raw navbar}


<div id="pagetitle">
<h1>{vb:raw pagetitle}</h1>
</div>

<div id="usercp_content">

{vb:raw oftw_sidemenu}

<div class="cp_content" style="width: 80%; float: right;">
<h2 class="blockhead">Contests Of The Week</h2>
<div class="blocksubhead">
<center><form action="nominate.php" method="post">
Enter Image URL to Nominate: <input type="text" name="imgurl"><input type="submit" name="submit">
</form>
</center>
</div>
{vb:raw print_nominations}
</div>


</div>
{vb:raw footer}

</body>
</html>

This processes the form:
<?php

require_once('./global.php');

define('CSRF_PROTECTION', true);


if (!isset($_POST['submit'])) {


}
else {

$imgurl = empty($_POST['imgurl']) ? die ("ERROR: Enter a imgurl") : mysql_escape_string($_POST['imgurl']);

// create query
$db->query_write("INSERT INTO oftw_nominations (imgurl) VALUES ('$imgurl')");

header( "refresh:0; url=http://development.aniworlds.net/oftw_nominations.php" );
// close connection
mysql_close($connection);
}

?>

Any ideas how I can also insert the username of whoever submitted this form?

kh99
07-13-2011, 05:34 PM
When you include global.php, it does a lot of stuff for you like makes sure the user is logged in (actually you have to check that - if the user id is '0' then the user is an unregistered guest). So hopefully the answer to your question is that it should all be handled by including global.php.

(If this doesn't answer your question I'll let someone else try :) )

Disasterpiece
07-13-2011, 05:36 PM
<?php

require_once('./global.php');

define('CSRF_PROTECTION', true);


if (!isset($_POST['submit'])) {


}
else {

$imgurl = empty($_POST['imgurl']) ? die ("ERROR: Enter a imgurl") : mysql_escape_string($_POST['imgurl']);

// create query
$db->query_write("INSERT INTO oftw_nominations (imgurl, username) VALUES ('$imgurl', '".$vbulletin->userinfo['username']."')");

header( "refresh:0; url=http://development.aniworlds.net/oftw_nominations.php" );
// close connection
mysql_close($connection);
}

?>

should do the trick

EquinoxWorld
07-13-2011, 05:42 PM
P E R F E C T !! I knew it had to be something simple :) Thank you SOO much Disasterpiece, hopefully you'll be seeing my first mod up here soon enough? Thanks to everyone's help, it's really easy to learn anything when you have this level of support. Thanks guys. :)