How to post a field that is based on a sql results query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

How to post a field that is based on a sql results query

Post by edawson003 »

How would I go about setting a $variable as a result from a sql query and have post the result as datapoint into mysql tabe? What I am trying to accomplish is once a user is logged in, when they make a form subission I want their acctid logged with data entry. The acctid is an autoicrement field that id unique to every user who registers thru my site.

Here's the code I use to pulldown the acctid:

Code: Select all

 
$acctidquery = mysql_query( "SELECT acctid FROM Users WHERE username ='".$_SESSION['username']."'" )
or die("SELECT Error: ".mysql_error()); 
while ($get_info = mysql_fetch_row($acctidquery)){ 
foreach ($get_info as $id);
} 
 
Feel free to advise if there is a better way to run the above. However the query code seemsworks. I tested it by echoing the $acctidquery.

Here's the other part where I have tried to set up a form post variable based on the query result:

Code: Select all

 
$creatby = $acctidquery;
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: How to post a field that is based on a sql results query

Post by jackpf »

Hmm...I don't think you need the while() loop, since there should only be one user right?

Also, I'm not sure what the purpose of the foreach() loop is...

Do you not just want $get_info['id']?
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

Re: How to post a field that is based on a sql results query

Post by edawson003 »

Okay, I took your advice and got rid of that while() loop & foreach() loop stuff. I made the query way more simpler.

Code: Select all

 
<?
$acctidquery = "SELECT * FROM Users WHERE username ='".$_SESSION['username']."'" ;
$result = mysql_query($acctidquery);
$row = mysql_fetch_assoc($result);
$id =  $row['acctid']; 
?> 
 
Now how do I get the value in $id variable (which happens to be "5") insert into the the back-end table field (createdby int(11) field) via my form. All the other field in the form posts exept for id.

Code: Select all

 
   $molbdenum = $_POST['molbdenum'];
   $creatdt = date("Y-m-d");
[color=#FF0000]   $creatby = $get_info['id'];[/color] <---- Didnt work, posted as NULL
   $displaycd = '01001';
 
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: How to post a field that is based on a sql results query

Post by jackpf »

Can't you just use $id? 8O
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: How to post a field that is based on a sql results query

Post by Eric! »

This value is coming from your query right ($id=row['acctid'])? So, like jack says

Code: Select all

    $molbdenum = $_POST['molbdenum'];
   $creatdt = date("Y-m-d");
   $creatby = $id;
// or $creatby=$row['acctid'];
// or just use $id and forget $creatby
   $displaycd = '01001';
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

Re: How to post a field that is based on a sql results query

Post by edawson003 »

Yeah, I figured that out the hard way. Making thing more difficult than they need to be obviously. Thanks all. :oops:
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: How to post a field that is based on a sql results query

Post by jackpf »

Lol yeah.

There's not really much point in directly assigning the value of one variable to another, unless you do different things to each of them.

For example:

Code: Select all

$a_variable = 'something';
 
$another_variable = $a_variable;
 
do_something_with($another_variable); // you might as well just use $a_variable...
 
//unless...
 
do_something_by_reference(&$another_variable);
echo $a_variable;
//...two different uses
Post Reply