query problem

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
dietmar
Forum Newbie
Posts: 1
Joined: Wed Mar 16, 2005 7:19 am

query problem

Post by dietmar »

Code: Select all

<?php
	session_start();
	require "connect.php";
	$title = $_GET['title'];
	$startdate = $_GET['startdate'];

	$query = "insert into project values (0,'".$title."','".$startdate."')";
	$result = mysql_query($query, $connection)
		or die(mysql_error());

	$projid = mysql_insert_id($connection);

	foreach($_SESSION['group'] as $key => $person){
	$query = "insert into member values(".$_SESSION['group'][$key]['pid'].",".$projid.",'".$_SESSION['group'][$key]['job']."')";
	$result = mysql_query($query, $connection)
		or die(mysql_error());
	}
	unset($_SESSION['group']);
	header("Location:viewgroups.php");
	exit();
?>
There is a problem with the > in the for loop which automatically matches the <?php tag and does not allow any of the code following it to be used. How can i get round this problem?

feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: query problem

Post by Chris Corbyn »

There is a problem with the > in the for loop which automatically matches the <?php tag and does not allow any of the code following it to be used. How can i get round this problem?
[/quote]

I don't see how using the standard => syntax in the foreach causes a problem but I do see a problem with your query.

You are missing single quotes around all but the last of your values. Why doesn't it work, do you get an error or could you show some output?

EDIT: $_SESSION['group'] is an array itself is it?
Last edited by Chris Corbyn on Wed Mar 16, 2005 7:38 am, edited 1 time in total.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Your query structure is a bit off. I'm not sure if this matters though but you might want to make them like this.

Code: Select all

$query = "INSERT INTO project (field1, field2, field3) VALUES ('0', '$title', '$startdate')";
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post by Bill H »

The column names are optional if you are providing values for all columns and in the same order as the columns exist in the db. I do like to provide them for code readability, though.
Post Reply