INSERT data using variables created on the fly.

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
vray
Forum Newbie
Posts: 9
Joined: Wed Jul 28, 2004 10:15 am

INSERT data using variables created on the fly.

Post by vray »

Could someone please check my syntax and my logic in the insert code below? I converted a small code snippet to suit my needs, but am not understanding the code well enough to debug it properly. If anyone could give me some pointers i'd be much appreciative. If more info is needed, or if the code that creates the form variables will help, I'd be glad to post it. Thanks.

Code: Select all

//Get reviewer user_id
  $query_user = "SELECT user_id FROM user WHERE user_name = '$user_name'";
  $result_uid = mysql_query($query_user);
  while ($row = mysql_fetch_object($result_uid))
	{
		$reviewerid =  $row->user_id;
		print ("Reviewer ID is $reviewerid<BR><BR>"); //test to see if correct id is being used
	}
  
   
 //INSERT SUBMITTED VARIABLES INTO DB

$query = "SELECT * FROM user WHERE user_name != '$user_name'";
$result = mysql_query($query);
while ($record = mysql_fetch_object($result))
{ 
$teamworkVar = "r1_".$record->user_id; 
$attitudeVar = "r2_".$record->user_id;
$prod_knowVar = "r3_".$record->user_id;
$productivityVar = "r4_".$record->user_id;
$communicationVar = "r5_".$record->user_id;
$professionalismVar = "r5_".$record->user_id;
$commentVar = "c_".$record->user_id; 
$sql = "INSERT INTO review (review_id, user_id, comment_on_id, teamwork, attitude, prod_know, productivity, communication, professionalism, comment, review_date) 
VALUES ("; 
$sql .= "NULL, "; 
$sql .= "'$reviewerid', "; 					//user_id of reviewer
$sql .= "'".$record->user_id."', "; 		//user_id of the victim
$sql .= "'".$$teamworkVar."', ";
$sql .= "'".$$attitudeVar."', ";
$sql .= "'".$$prod_knowVar."', ";
$sql .= "'".$$productivityVar."', ";
$sql .= "'".$$communicationVar."', ";
$sql .= "'".$$professionalismVar."', ";
$sql .= "'".$$commentVar."', ";				//Review Comment
$sql .= "NOW()) ";
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

so the submitted variable names change based on the user's id number?
vray
Forum Newbie
Posts: 9
Joined: Wed Jul 28, 2004 10:15 am

Post by vray »

yes, here is the code that creates the variables:

Code: Select all

<?php
query = "SELECT * FROM user WHERE user_name != '$user_name'";

$result = mysql_query($query); 
print("<FORM METHOD=post ACTION=submit_review.php>\n");

while ($row = mysql_fetch_object($result))
{
print("<TABLE BORDER=1 cellpadding=2 cellspacing=0 bordercolor=#666666><tr bgcolor=#999999><TD>\n");
echo "<TABLE>\n
<tr><td><B> $row->first_name $row->last_name </B></td></tr>\n
</table>\n 
   <TABLE border=0 cellpadding=2 cellspacing=0 bordercolor=#666666>\n<tr>
  <td><div align=center>Teamwork |<BR><select size=6 name="r1_".$row->user_id.""><option value=0>NA</option><option value=1>1</option><option value=2>2</option><option value=3>3</option><option value=4>4</option><option value=5>5</option></select></div></td>\n 
   <td><div align=center>Attitude |<BR><select size=6 name="r2_".$row->user_id.""><option value=0>NA</option><option value=1>1</option><option value=2>2</option><option value=3>3</option><option value=4>4</option><option value=5>5</option></select></div></td>\n 
 <td><div align=center>Product Knowlege |<BR><select size=6 name="r3_".$row->user_id.""><option value=0>NA</option><option value=1>1</option><option value=2>2</option><option value=3>3</option><option value=4>4</option><option value=5>5</option></select></div></td>\n 
 <td><div align=center>Productivity |<BR><select size=6 name="r4_".$row->user_id.""><option value=0>NA</option><option value=1>1</option><option value=2>2</option><option value=3>3</option><option value=4>4</option><option value=5>5</option></select></div></td>\n 
	<td><div align=center>Communication |<BR><select size=6 name="r5_".$row->user_id.""><option value=0>NA</option><option value=1>1</option><option value=2>2</option><option value=3>3</option><option value=4>4</option><option value=5>5</option></select></div></td>\n 
    <td><div align=center>Professionalism <BR><select size=6 name="r6_".$row->user_id.""><option value=0>NA</option><option value=1>1</option><option value=2>2</option><option value=3>3</option><option value=4>4</option><option value=5>5</option></select></div></td>\n 
</table>\n 
<TABLE>\n
<tr><td>Comment<BR><textarea cols=60 rows=3 name="c_".$row->user_id.""></textarea></td></TR>\n
</table>\n"; 
print("</TD></TR></table><BR>\n");
}
print("<div align=center>Please check your review carefully before submitting.<BR>There is NO going back once you hit the button below..<BR><BR></div>\n");

print("<div align=center><INPUT TYPE=submit NAME=submit VALUE=Submit></div>\n");
echo "</TD></TR></TABLE>\n";
}
?>
vray
Forum Newbie
Posts: 9
Joined: Wed Jul 28, 2004 10:15 am

Post by vray »

A quick explanation of the app: This application is a peer review app. User1 logs on, all the members of user1's team are loaded into a form, except for user1 himself. User1 can now rate the co-workers and leave a comment. So when the review is submitted, I submit the user_id of the reviewer, the user_id of the reviewed, and then the reviewed results. The number of team members fluctuate, the form variables are created on the fly. If my db tables would help, let me know and I'll post them..
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

..and you have register globals on?
vray
Forum Newbie
Posts: 9
Joined: Wed Jul 28, 2004 10:15 am

Post by vray »

Hmm, no, register globals is off. I'll give it a try with them on.
Last edited by vray on Wed Jul 28, 2004 12:17 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

then you need to use $_POST['r1_' . $row->user_id]
vray
Forum Newbie
Posts: 9
Joined: Wed Jul 28, 2004 10:15 am

Post by vray »

Thanks, I'll try that.
vray
Forum Newbie
Posts: 9
Joined: Wed Jul 28, 2004 10:15 am

Post by vray »

$_POST["r1_".$record->user_id];
or
$_POST['r1_'.$record->user_id];

Single or double quotes?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

in that case, it doesn't make a difference. If you want to put variables inside the string you'll need double quotes, but otherwise, it doesn't matter much.
vray
Forum Newbie
Posts: 9
Joined: Wed Jul 28, 2004 10:15 am

Post by vray »

Ok., thanks. i am getting a parse error:
Parsing Error: \\Bboard\www\html\auth\submit_review.php line 60 - parse error, unexpected $end

Line 60 is the closing tag ?>

Any ideas?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

those are generally errors of not closing a string..
vray
Forum Newbie
Posts: 9
Joined: Wed Jul 28, 2004 10:15 am

Post by vray »

yes, i am not sure of the syntax of the $sql = "INSERT etc.. Especially the VALUES line with all the $sql variables. Does it look okay to you? This is the string I believe i am not closing.
vray
Forum Newbie
Posts: 9
Joined: Wed Jul 28, 2004 10:15 am

Post by vray »

Ok, I left out the closing brace... now I get the following error:

Notice: Undefined variable: 2 in /home/www/html/auth/submit_review.php on line 48

Notice: Undefined variable: 1 in /home/www/html/auth/submit_review.php on line 49

etc...

Progress at last! The posting is working, that's the 2 and 1 I entered in the form.

The error lines are all the variables with the double $.
eg. $sql .= "'".$$teamworkVar."', ";

declared as
$teamworkVar = $_POST["r1_".$record->user_id];

What is the reasoning behind the $$?
Post Reply