Page 1 of 1

query problem

Posted: Wed Mar 16, 2005 7:24 am
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]

Re: query problem

Posted: Wed Mar 16, 2005 7:37 am
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?

Posted: Wed Mar 16, 2005 7:37 am
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')";

Posted: Wed Mar 16, 2005 10:06 am
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.