Creon Posted April 11, 2006 Share Posted April 11, 2006 Ive been looking at this script for an hour or so and know that it have to be a fundamental error. I just cant see it. The following code is to write a blog enterie from a form to a database table called 'blog' . [code]<?php // Login fileinclude("login.php");//connect to database$conn = mysql_connect($server, $db_user, $db_pass) or die ("UPPKOPPLINGS FEL");mysql_select_db($database,$conn) or die ("Could not open database");$title = $_POST['title'];$text = $_POST['text'];$pass = $_POST['pass']; $time = date("m.d.y"); echo "$title $text $ttime ";//insert the values$insert = "INSERT INTO `blog` ( `id` , `title` , `text` , `click` , `time` ) VALUES ( '', $title , $text, '' , $time )";mysql_db_query($database, $insert) or die ("did not send");php?>[/code]There is no real error showd exept for "did not send". everything else works. So im inclined to think that this part is wrong, just cant see it.[code]//insert the values$insert = "INSERT INTO `blog` ( `id` , `title` , `text` , `click` , `time` ) VALUES ( '', $title , $text, '' , $time )";mysql_db_query($database, $insert) or die ("did not send");[/code] Quote Link to comment Share on other sites More sharing options...
trq Posted April 11, 2006 Share Posted April 11, 2006 You've already selected the database so no need to use mysql_db_query. Also, if the id column is auto_increment, just leave it out. Also, be aware that backticks ` are only required around resrved words.As far as debuging, use mysql_error() to get infomation on the last error.[code]$insert = "INSERT INTO blog (title,`text`,click,`time`) VALUES ('$title','$text,'','$time')";mysql_query($insert) or die (mysql_error());[/code]You'll find your problem was that your values needed quotes around them. Quote Link to comment Share on other sites More sharing options...
Creon Posted April 11, 2006 Author Share Posted April 11, 2006 thanks Still a bit confused for when to use ' , ", ` or nothing at all.Im alsow wondering what type i should use for the $time in the sqldatabase. Quote Link to comment Share on other sites More sharing options...
trq Posted April 11, 2006 Share Posted April 11, 2006 Ok... I'll try and clarify things a little. Given this example.[code]INSERT INTO tbl (fld1,fld2) VALUES ('val1','val2');[/code]Fields should only need to be surrounded by backticks ` when they are reserved words used by the database. Some examples are the words [i]text[/i] and [i]date[/i]. This same concept also applies to table names. So if we wanted to use reserved words our statment would look like.[code]INSERT INTO foo (`text`,fld2) VALUES ('val1','val2');[/code]Next... ALL values need to be surrounded by single quotes (you can use double quotes but it makes life a little more difficult within a php context).Next... In php all string need to be surrounded in either single or double quotes. Most people will use double quotes to surround sql statements as this allow variables to be parsed easily. So to make our example statement into a php string it would be...[code]$sql = "INSERT INTO tbl (fld1,fld2) VALUES ('val1','val2')";[/code]And with reserved words...[code]$sql = "INSERT INTO foo (`text`,fld2) VALUES ('val1','val2')";[/code]And with php variables as values...[code]$val1 = 'foo';$val2 = 'bar';$sql = "INSERT INTO foo (`text`,fld2) VALUES ('$val1','$val2')";[/code]As for a field type to hold a time. I would recommend TIMESTAMP.Hope this clears things up some. Quote Link to comment Share on other sites More sharing options...
Creon Posted April 11, 2006 Author Share Posted April 11, 2006 yes, thanks. that made it quite clear or at least i think so. Saved your explination on my comp if i ever need e reference hehe.Still have a problem with the script. I did the changes you showed me. And I used Timestamp as sql typ for the date. But now it returns a sql syntax error.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 '04.11.06')' at line 1[code]<?php // Login fileinclude("login.php");//connect to database$conn = mysql_connect($server, $db_user, $db_pass) or die ("UPPKOPPLINGS FEL");mysql_select_db($database,$conn) or die ("Could not open database");$title = $_POST['title'];$text = $_POST['text'];$pass = $_POST['pass']; $ttime = date("m.d.y"); echo "$title $text $ttime ";//insert the values$insert = "INSERT INTO blog (title,`text`,click,`time`) VALUES ('$title','$text,'','$ttime')";mysql_query($insert) or die (mysql_error());php?>[/code]im alsow wondering if i could shorten the script by changing (thus removing the click value since its emty, altho i have a click table in the blog database)[code]$insert = "INSERT INTO blog (title,`text`,click,`time`) VALUES ('$title','$text,'','$ttime')";[/code]to[code]$insert = "INSERT INTO blog (title,`text`,`time`) VALUES ('$title','$text,'$ttime')";[/code] Quote Link to comment Share on other sites More sharing options...
Creon Posted April 11, 2006 Author Share Posted April 11, 2006 [!--quoteo(post=363725:date=Apr 11 2006, 12:16 PM:name=PyroSmurf)--][div class=\'quotetop\']QUOTE(PyroSmurf @ Apr 11 2006, 12:16 PM) [snapback]363725[/snapback][/div][div class=\'quotemain\'][!--quotec--]yes, thanks. that made it quite clear or at least i think so. Saved your explination on my comp if i ever need e reference hehe.Still have a problem with the script. I did the changes you showed me. And I used Timestamp as sql typ for the date. But now it returns a sql syntax error.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 '04.11.06')' at line 1[code]<?php // Login fileinclude("login.php");//connect to database$conn = mysql_connect($server, $db_user, $db_pass) or die ("UPPKOPPLINGS FEL");mysql_select_db($database,$conn) or die ("Could not open database");$title = $_POST['title'];$text = $_POST['text'];$pass = $_POST['pass']; $ttime = date("m.d.y"); echo "$title $text $ttime ";//insert the values$insert = "INSERT INTO blog (title,`text`,click,`time`) VALUES ('$title','$text,'','$ttime')";mysql_query($insert) or die (mysql_error());php?>[/code]im alsow wondering if i could shorten the script by changing (thus removing the click value since its emty, altho i have a click table in the blog database)[code]$insert = "INSERT INTO blog (title,`text`,click,`time`) VALUES ('$title','$text,'','$ttime')";[/code]to[code]$insert = "INSERT INTO blog (title,`text`,`time`) VALUES ('$title','$text,'$ttime')";[/code][/quote] Quote Link to comment Share on other sites More sharing options...
trq Posted April 11, 2006 Share Posted April 11, 2006 Sorry, I didn't realise you where inserting a date into $time. Use a DATE type field. And yes... providing the filed supports NULL's you can remove the [i]click[/i] from your query. Quote Link to comment Share on other sites More sharing options...
Creon Posted April 11, 2006 Author Share Posted April 11, 2006 thats helpfull. tnx. Everything works now. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.