FRIHOST • FORUMS • SEARCH • FAQ • TOS • BLOGS • COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


insert data to mySQL database problem





ranzon
Hi....
i have maked an script who inserts data to mySQL database..
but when i use this code:

Code:
<?php
$username="username";
$password="password";
$database="database";

$navn = $_POST['navn'];
$emne = $_POST['emne'];
$nyhet = $_POST['nyhet'];
$tid = $_POST['tid'];
$dato = $_POST['dato'];

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("Fitte database");

$query = 'INSERT INTO `nyheter` (`id`, `navn`, `tid`, `dato`, `nyhet`) VALUES (\'\', \'$navn\', \'$tid\', \'$dato\', \'$nyhet\')'

$result=mysql_query($query);

mysql_close();
?>

then it returns this:
Quote:
Parse error: syntax error, unexpected T_VARIABLE in /home/ranzon/domains/fredrik.frih.net/public_html/beta/admin/writenews.php on line 17


and i don't know what to do.. please help me.
mathiaus
assuming that is writenews.php you have no semicolon on the query. I also removed all the horrible backslashes. Use

Code:
$query = "INSERT INTO `nyheter` (`id`, `navn`, `tid`, `dato`, `nyhet`) VALUES ('', '$navn', '$tid', '$dato', '$nyhet')";
ranzon
uhm.. this is same project i'm working with so, whats wrong here?
Code:
<?php
$username="";
$password="";
$database="";

$side = nl2br($_POST['side']);

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Jζvla fitte mySQL");
$query  = "UPDATE metall_gakk SET sidee WHERE side = `$side`";
$result=mysql_query($query)
mysql_close();
?>


it just echos this:
Quote:
Parse error: syntax error, unexpected T_STRING in file.php on line 12

line 12 wich contains this text:
Code:
mysql_close();


please help me..
mathiaus
Line 11 is unfinished, no semicolon again
Code:
$result=mysql_query($query);
ranzon
mathiaus wrote:
Line 11 is unfinished, no semicolon again
Code:
$result=mysql_query($query);


do'h i allways forget that.. can someone please help me learning that? Razz
just kidding
alalex
And to insert a variable in the database, use " instead of '
Example:
Code:

$query = INSERT INTO username, password VALUES ("$username","$password");
$result = mysql_query($query);
Rhysige
alalex wrote:
And to insert a variable in the database, use " instead of '
Example:
Code:

$query = INSERT INTO username, password VALUES ("$username","$password");
$result = mysql_query($query);



Ahh what the? putting "" you are likely to break something, ' is the proper quatation to use.
BlackSkad
alalex wrote:
And to insert a variable in the database, use " instead of '
Example:
Code:

$query = INSERT INTO username, password VALUES ("$username","$password");
$result = mysql_query($query);


You are indeed going to break a lot of stuff. First of all, you'll get a parse error from PHP. PHP will not know what to do with the words after '$query = '.
Secondly, your mysql will return an error becaus values need to be surrounded with single quoted.
And thirdly, you forgot the table in which you want to insert, and the names of the columns should be surrounded by braces ();
This is the correct code:
Code:
$query = "INSERT INTO `table` (username, password) VALUES ('$username','$password');


BlackSkad
DoctorBeaver
[quote="BlackSkad"]
alalex wrote:


This is the correct code:
Code:
$query = "INSERT INTO `table` (username, password) VALUES ('$username','$password');


BlackSkad


Should the table name really be in single quotes? I believe the only time that would be correct is if you are using a variable name as the table name, as in "INSERT INTO '$table_name'..."

Also, you haven't closed the double quotes.

I use this construct in many places as...

Code:
$query = "INSERT INTO tablename (username, password) VALUES ('$username','$password')";
Jamatu
Surprised BlackSkad missed the closing double quotes!!!

Anyway DoctorBeaver you don't have to put single quotes around the table name, some people think it's good practice and makes the statement easier to read.
Related topics
Mysql database problem
[MySQL database] Password and username
Delete data from MySQL database problem!!
MySQL database
how to insert data into mysql base from a web page
Problem connecting to MySQL database with PHP [RESOLVED]
Help importing CSV file into MySQL database
How to search in a MySQL - database
Inserting PHP multidimensional arrays into a mySQL database
Connecting to 2 different Mysql database and tables?
Changing entries in MySQL database
Optimizing a mySQL database
Maximum MYSQL database size
MySQL privileges Problem
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.