FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

whats wrong with this mysql query?

 


virre
I have a trouble with this code

Code:

echo "My Code";

// Select and other things are correct
$rub=mysql_real_escape_string($_POST['rubrik']);
   $mtxt=mysql_real_escape_string($_POST['txt']);
   $datum=date(Y)."-".date(m)."-".date(d);
   $katego=mysql_real_escape_string($_POST['kat']);
   $keyword=mysql_real_escape_string($_POST['keys']);
   $nuf_dat=date(Ymd);
   
   
   
   
   
   $query="INSERT INTO inlagg (rubrik,artikel,datum,kategori,keys,n_dat) VALUES ('".$rub."','".$mtxt."','".$datum."','".$katego."','".$keywords."','".$nuf_dat."');";
   
   
   echo "<br />Detta kommer utf&ouml;ras: <br />".$query."<br />";
   mysql_query($query) or die("Fel vid inmatning: <br />".mysql_error());
   mysql_close();


Excuse my mixing of languages in this post... The inlagg table contens is as follow:

uid int(11) auto_increment primary key
artikel longtext
datum varchar(20)
keys varchar(200)
rubrik varchar(200)
kategori varchar(200)
n_dat int(11)

and the variables are comming in from a form. I think the trouble is somewhere close to $mtxt. First I thought it just was when I included a " in the text input but now it seems it's constant. I get an:
mysql_error() wrote:

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 'keys,n_dat) VALUES ('Testar utan länkar','Kommer sidan att skrivas in om ja' at line 1


But I can't see why the query fails, not even after reentering it four times.
AOP Web Development
[quote="virre"]I have a trouble with this code

Code:

echo "My Code";

// Select and other things are correct
$rub=mysql_real_escape_string($_POST['rubrik']);
   $mtxt=mysql_real_escape_string($_POST['txt']);
   $datum=date(Y)."-".date(m)."-".date(d);
   $katego=mysql_real_escape_string($_POST['kat']);
   $keyword=mysql_real_escape_string($_POST['keys']);
   $nuf_dat=date(Ymd);
   
   
   
   
   
   $query="INSERT INTO inlagg (rubrik,artikel,datum,kategori,keys,n_dat) VALUES ('".$rub."','".$mtxt."','".$datum."','".$katego."','".$keywords."','".$nuf_dat."');";
   
   
   echo "<br />Detta kommer utf&ouml;ras: <br />".$query."<br />";
   mysql_query($query) or die("Fel vid inmatning: <br />".mysql_error());
   mysql_close();


Excuse my mixing of languages in this post... The inlagg table contens is as follow:

uid int(11) auto_increment primary key
artikel longtext
datum varchar(20)
keys varchar(200)
rubrik varchar(200)
kategori varchar(200)
n_dat int(11)

and the variables are comming in from a form. I think the trouble is somewhere close to $mtxt. First I thought it just was when I included a " in the text input but now it seems it's constant. I get an:
mysql_error() wrote:

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 'keys,n_dat) VALUES ('Testar utan länkar','Kommer sidan att skrivas in om ja' at line 1



Hello i just try to fix your error on this. But i didn't try it.. just trying to analyze.

from the code you enter i will try to make some modifications and hope it works

[code]
$rub=mysql_real_escape_string($_POST['rubrik']);
$mtxt=mysql_real_escape_string($_POST['txt']);
$datum=date("Y-m-d");
$katego=mysql_real_escape_string($_POST['kat']);
$keyword=mysql_real_escape_string($_POST['keys']);
$nuf_dat=date("Y-m-d");

$sqlquery="INSERT INTO inlagg (rubrik,artikel,datum,kategori,keys,n_dat) VALUES ('$rub','$mtxt','$datum','$katego','$keywords','$nuf_dat')";

$result = mysql_query($sqlquery) or die(mysql_error());
if($result){

echo "Successfully Inserted";
}else{
echo "Failed to Insert!";
}
[code]


Regarding with your table i try to do some changes of the data type but i don't know what do you really want about it.

uid int(11) auto_increment primary key
artikel longtext
datum date
keys varchar(200)
rubrik varchar(200)
kategori varchar(200)
n_dat date



I hope this will help! Smile
[/code]
virre
Acctualy I don't see any diffrence, and neither was there any changes in the mysql error.
fromegame
Code:
$query = "INSERT INTO `inlagg` (`rubrik`,`artikel`,`datum`,`kategori`,`keys`,`n_dat`) VALUES ('".$rub."','".$mtxt."','".$datum."','".$katego."','".$keywords."','".$nuf_dat."')";


Use ` not ' around the names.
Clever_As_Sin
fromegame wrote:
Code:
$query = "INSERT INTO `inlagg` (`rubrik`,`artikel`,`datum`,`kategori`,`keys`,`n_dat`) VALUES ('".$rub."','".$mtxt."','".$datum."','".$katego."','".$keywords."','".$nuf_dat."')";


Use ` not ' around the names.


Note for future:
It's better to use PEAR DB package to work with database. It can be found here http://pear.php.net/package/DB

DB is a database abstraction layer providing:
* an OO-style query API
* portability features that make programs written for one DBMS work with other DBMS's
* a DSN (data source name) format for specifying database servers
* prepare/execute (bind) emulation for databases that don't support it natively
* a result object for each query response
* portable error codes
* sequence emulation
* sequential and non-sequential row fetching as well as bulk fetching
* formats fetched rows as associative arrays, ordered arrays or objects
* row limit support
* transactions support
* table information interface
* DocBook and phpDocumentor API documentation

DB layers itself on top of PHP's existing
database extensions.

It supports following DB engines:

fbsql, ibase, informix, msql, mssql,
mysql, mysqli, oci8, odbc, pgsql,
sqlite and sybase.

DB is compatible with both PHP 4 and PHP 5.
-----------------
` - is not quite necessary if you are using PEAR DB
dwikristianto
fromegame wrote:
Code:
$query = "INSERT INTO `inlagg` (`rubrik`,`artikel`,`datum`,`kategori`,`keys`,`n_dat`) VALUES ('".$rub."','".$mtxt."','".$datum."','".$katego."','".$keywords."','".$nuf_dat."')";


Use ` not ' around the names.


it seem that there is reserved word problem on field keys. as fromegame wrote, use backtick to encode the field name in declarations.
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

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