What on earth am I doing wrong? (php+mysql)

Poff

macrumors 65816
Original poster
Sep 16, 2003
1,258
1
Stavanger, Norway
It's been a while since I've been doing anything with sql, and unfortunately I've deleted most of my old php files. I found a few, though, and tried to implement them. But something goes wrong, and I can't see what. Anyone else see it?

PHP:
<?php

$periode="10";
$konto="20";
$belop="40";

mysql_connect("sql02.fastname.no","user","password");
@mysql_select_db("flaatartist") or die("error connecting to database");

mysql_query("INSERT INTO 2007 VALUES ('$periode','$konto','$belop')");


mysql_close();
?>
 

Poff

macrumors 65816
Original poster
Sep 16, 2003
1,258
1
Stavanger, Norway
Maby try this:

PHP:
mysql_query("INSERT INTO 2007 VALUES (" . $periode . "," . $konto . "," . $belop . ")");
thanks, but that didn't work. :(

it needs to be ' since I allready have a ". And I didn't understand why the punctuations? (I'm a rookie when it comes to mySQL.)
 

Aranince

macrumors 65816
Apr 18, 2007
1,104
0
California
In PHP the . can be used to add a variable to a string. What is the problem exacly?

PHP:
mysql_query("INSERT INTO 2007 VALUES (\'" . $periode . "\' , \' " . $konto . " \',\'" . $belop . "\' )");
 

Poff

macrumors 65816
Original poster
Sep 16, 2003
1,258
1
Stavanger, Norway
apparantly, the problem was me calling the table "2007". It worked nice in CocoaMySQL, but my php-script didn't like it. I renamed it "2007test" and now it works just fine.

PHP:
mysql_query("INSERT INTO 2007test VALUES ('$periode','$konto','$belop')");
anyone know a way to insert into a table named "2007"? There has to be a way..
 

Cabbit

macrumors 68020
Jan 30, 2006
2,129
1
Scotland
apparantly, the problem was me calling the table "2007". It worked nice in CocoaMySQL, but my php-script didn't like it. I renamed it "2007test" and now it works just fine.

PHP:
mysql_query("INSERT INTO 2007test VALUES ('$periode','$konto','$belop')");
anyone know a way to insert into a table named "2007"? There has to be a way..
make sure the table value is int.
 

Poff

macrumors 65816
Original poster
Sep 16, 2003
1,258
1
Stavanger, Norway
In PHP the . can be used to add a variable to a string. What is the problem exacly?

PHP:
mysql_query("INSERT INTO 2007 VALUES (\'" . $periode . "\' , \' " . $konto . " \',\'" . $belop . "\' )");
I was adding values to three fields in a table, but I didn't specify the field names in my script so it might have been confusing. (picture describing this in bottom of post) But all is well now, since my table is no longer named "2007".

make sure the table value is int.
I think we're misunderstanding each other. Here's a picture of what I'm talking about, I want to name the table "2007", and not "2007test", but my php-script doesn't manage to post to the "2007"-table for some reason.

 

Knox

Administrator
Staff member
Jul 1, 2002
1,259
1
UK
anyone know a way to insert into a table named "2007"? There has to be a way..
You'd need to use ` (backtick) characters around the table name, e.g.

PHP:
mysql_query("INSERT INTO `2007` VALUES ('$periode','$konto','$belop')");
 

Poff

macrumors 65816
Original poster
Sep 16, 2003
1,258
1
Stavanger, Norway
You'd need to use ` (backtick) characters around the table name, e.g.

PHP:
mysql_query("INSERT INTO `2007` VALUES ('$periode','$konto','$belop')");
I did try that, but it didn't work.

..and I still don't understand why using only numbers should be worse than using letters and numbers.. :/
 

Aranince

macrumors 65816
Apr 18, 2007
1,104
0
California
There is also a way to specify which column the values get put into. This might be a good thing to do...

"INSERT INTO 2007 (column1, column2, column3) VALUES ('$periode', '$kanto', '$belop')
 

Poff

macrumors 65816
Original poster
Sep 16, 2003
1,258
1
Stavanger, Norway
There is also a way to specify which column the values get put into. This might be a good thing to do...

"INSERT INTO 2007 (column1, column2, column3) VALUES ('$periode', '$kanto', '$belop')
yeah, but that doesn't solve the problem with the table being named "2007". (that line you wrote wouldn't work on my test-project.)

I simply don't understand this.. :/
 

zimv20

macrumors 601
Jul 18, 2002
4,388
7
toronto
from here:
Database, table, index, column, and alias names are identifiers. [...] Identifiers may begin with a digit but unless quoted may not consist solely of digits.
so the note about using ticks was right on; there must be another reason it's not working for you.
 

OutThere

macrumors 603
Dec 19, 2002
5,730
0
NYC
how about trying this:

PHP:
mysql_query("INSERT INTO `2007` VALUES ('$periode','$konto','$belop')") or die(mysql_error());
 

Matteh117

macrumors regular
May 24, 2007
202
0
Surrey, UK
You'd need to use ` (backtick) characters around the table name, e.g.

PHP:
mysql_query("INSERT INTO `2007` VALUES ('$periode','$konto','$belop')");
You don't need a "backtick". An apostrophe works fine. ;)

Also, if you're using an @ (which I believe ignores errors) then you don't need an "or die".

PHP:
mysql_query("insert into '2007' values ('".$periode."', '".$konto."', '".$belop."')");
Should do it.
 

Knox

Administrator
Staff member
Jul 1, 2002
1,259
1
UK
You don't need a "backtick". An apostrophe works fine. ;)
Code:
mysql> describe '2007';
ERROR 1064: You have an error in your SQL syntax.  Check the manual that corresponds to your MySQL server version for the right syntax to use near ''2007'' at line 1

mysql> describe `2007`;
+-------+------------------+------+-----+---------+-------+
| Field | Type             | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+-------+
| id    | int(10) unsigned |      |     | 0       |       |
+-------+------------------+------+-----+---------+-------+
Also, if you're using an @ (which I believe ignores errors) then you don't need an "or die".

PHP:
mysql_query("insert into '2007' values ('".$periode."', '".$konto."', '".$belop."')");
Should do it.
@ suppresses the display of errors but they should still be handled in some way. die() doesn't give the most user friendly of error pages, but does at least give something.
 

Mr.nix

macrumors member
Jan 30, 2007
68
0
To create

PHP:
mysql_query
	("CREATE TABLE `2007` (
		id INT NOT NULL AUTO_INCREMENT, 
		PRIMARY KEY(id),
		field1 INT(5) NOT NULL, 
		field2 INT(5) NOT NULL,
		field3 INT(5) NOT NULL
	)")
 or die(mysql_error());

To insert
PHP:
$periode = '10';
$konto = '20';
$belop = '40';


mysql_query("
	INSERT INTO `2007` 
		(field1, field2, field3)
	VALUES
		(
			'" . $periode . "',
			'" . $konto . "',
			'" . $belop . "'
		)
	");

Or this. Depends on what you're doing.

PHP:
$periode = '10';
$konto = '20';
$belop = '40';


mysql_query("
	INSERT INTO `2007` 
		(field1, field2, field3)
	VALUES
		(
			$periode,
			$konto,
			$belop 
		)
	");


Formated so you can read it.