Page 1 of 1

parse error during database query

Posted: Wed Jan 18, 2006 4:56 am
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?

Posted: Wed Jan 18, 2006 5:19 am
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.'"';

Posted: Wed Jan 18, 2006 6:03 am
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'";

Posted: Wed Jan 18, 2006 7:06 am
by foobar
Even better, use prepared queries! Oh wait...

Posted: Wed Jan 18, 2006 7:14 am
by m3mn0n
Updated the topic title to something more descriptive than "Something's wrong! Help me!!!".

Posted: Wed Jan 18, 2006 7:32 am
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...

Posted: Wed Jan 18, 2006 7:35 am
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

Posted: Wed Jan 18, 2006 7:44 am
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 ?

Posted: Wed Jan 18, 2006 8:56 am
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)

Posted: Wed Jan 18, 2006 12:08 pm
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.