parse error during database 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
jimpa1986
Forum Newbie
Posts: 4
Joined: Wed Jan 18, 2006 4:51 am

parse error during database query

Post by jimpa1986 »

Ok, i'm doing a program that should add quite alot of info into my db... but something's wrong with the code.
I get the message:
Parse error: parse error, unexpected $ in /hsphere/local/home/kitabu/kitabu.se/demo/add_img.php on line 41
The code looks like this:

Code: Select all

<html>
<body>
<?
if ($_POST['antal']=="") {?>
<form method="POST" action="add_img.php">
	<p>Hur många bilder vill du lägga in i databasen?<br>
&nbsp;<input type="text" name="antal" size="4"></p>
	<p><input type="submit" value="Skicka" name="B1"></p>
</form>
<?}
if ($_POST['antal']>0 && $_POST['antal']<100 && $_POST['Bild1']="") {?>
<form method="POST" action="add_img.php">
	<input type="hidden" name="antal" size="4" value="<?=$_POST['antal']?>">
	<p>Ange bildens namn (inkl. filendelse)<br>
	OBS! Alla bilder måste ligga i mappen /img/gallery/!</p>
	<p><? for($i=0; $i<$_POST['antal'];) { 
	$i=$i+1;?>
	Bild <?=$i?>: <input type="text" name="Bild<?=$i?>" size="25">
	<select size="1" name="kategori<?=$i?>">
	<option selected value="Blandat">Blandat</option>
	<option value="Lager">Läger</option>
	<option value="Mohippor">Möhippor</option>
	<option value="Svensexor">Svensexor</option>
	<option value="Barnkalas">Barnkalas</option>
	</select><? }?></p>
	<p><input type="submit" value="Skicka" name="B2"></p>
</form><?}
else {
include ('connect.php');
for($i=0; $i<$_POST['antal'];) {
$i=$i+1;
$Bild = $_POST['Bild$i'];
$Kat = $_POST['kategori$i'];
$query="INSERT INTO gallery set namn=\"$Bild\", category=\"$Kat\";
if (mysql_query($query);) {
?>Bilder har blivit tilllagda i din databas<?}
else {
?>Ett fel har uppstått!<?}}
};?>
</body>
</html>
connect.php look like:

Code: Select all

<?
//Startar sessionen
session_start();
//Ansluter till databasen
@mysql_connect("*******","******","******") or print("Kunde ej uprätta kontakt med databasen. var god försök igen senare.");
@mysql_select_db("kitabu_data") or print("Kunde ej Kitabu_data");
?>
Can somebody help me?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Code: Select all

$query="INSERT INTO gallery set namn=\"$Bild\", category=\"$Kat\";
should be

Code: Select all

$query="INSERT INTO gallery set namn=\"$Bild\", category=\"$Kat\"";
or even better

Code: Select all

$query='INSERT INTO gallery set namn="'.$Bild.'", category="'.$Kat.'"';
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Or better still (MYSQL syntax uses single quotes for values - by standards at least)

Code: Select all

$query = "INSERT INTO `gallery` SET `namn` = '$Bild', `category` = '$Kat'";
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post by foobar »

Even better, use prepared queries! Oh wait...
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Updated the topic title to something more descriptive than "Something's wrong! Help me!!!".
duk
Forum Contributor
Posts: 199
Joined: Wed May 19, 2004 8:45 am
Location: London

Post by duk »

by the way... never use a column named div... mysql don't know how to deal a column with the name div or DIV...

i don't know if anyone had trie or need a column with the name div... but i have... and i take a good time to understand that the problem was with Mysql...
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

duk wrote:by the way... never use a column named div... mysql don't know how to deal a column with the name div or DIV...

i don't know if anyone had trie or need a column with the name div... but i have... and i take a good time to understand that the problem was with Mysql...
simple fix is to wrap column names (if your lazy only columns with reserved words) in backticks
duk
Forum Contributor
Posts: 199
Joined: Wed May 19, 2004 8:45 am
Location: London

Post by duk »

yeah i think im lazy...

anyway i dindn't know that mysql had reserved words for columns... but just for curiosity you know if oracle and pgSQL have reserved names for columns to ?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

read their documentation, i'm sure they all have a section dedicated on reserved words.. (I even have them in my bookmarks but am too lazy to copy paste them here)
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post by foobar »

timvw wrote:read their documentation, i'm sure they all have a section dedicated on reserved words.. (I even have them in my bookmarks but am too lazy to copy paste them here)
MySQL 4.1 Manual.
Post Reply