Page 1 of 1
How to post a field that is based on a sql results query
Posted: Thu Sep 17, 2009 8:42 am
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:
Re: How to post a field that is based on a sql results query
Posted: Thu Sep 17, 2009 9:49 am
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']?
Re: How to post a field that is based on a sql results query
Posted: Thu Sep 17, 2009 9:47 pm
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';
Re: How to post a field that is based on a sql results query
Posted: Fri Sep 18, 2009 4:32 am
by jackpf
Can't you just use $id?

Re: How to post a field that is based on a sql results query
Posted: Fri Sep 18, 2009 7:39 am
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';
Re: How to post a field that is based on a sql results query
Posted: Fri Sep 18, 2009 11:11 am
by edawson003
Yeah, I figured that out the hard way. Making thing more difficult than they need to be obviously. Thanks all.

Re: How to post a field that is based on a sql results query
Posted: Fri Sep 18, 2009 11:54 am
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