Jump to content


Photo

write to sql database


  • Please log in to reply
7 replies to this topic

#1 Creon

Creon
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 11 April 2006 - 04:18 PM

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' .

<?php 
// Login file
include("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?>


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.

//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");


#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 11 April 2006 - 04:31 PM

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.
$insert = "INSERT INTO blog (title,`text`,click,`time`) VALUES ('$title','$text,'','$time')";
mysql_query($insert) or die (mysql_error());
You'll find your problem was that your values needed quotes around them.

#3 Creon

Creon
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 11 April 2006 - 04:39 PM

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.

#4 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 11 April 2006 - 04:56 PM

Ok... I'll try and clarify things a little. Given this example.
INSERT INTO tbl (fld1,fld2) VALUES ('val1','val2');
Fields should only need to be surrounded by backticks ` when they are reserved words used by the database. Some examples are the words text and date. This same concept also applies to table names. So if we wanted to use reserved words our statment would look like.
INSERT INTO foo (`text`,fld2) VALUES ('val1','val2');
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...
$sql = "INSERT INTO tbl (fld1,fld2) VALUES ('val1','val2')";
And with reserved words...
$sql = "INSERT INTO foo (`text`,fld2) VALUES ('val1','val2')";
And with php variables as values...
$val1 = 'foo';
$val2 = 'bar';
$sql = "INSERT INTO foo (`text`,fld2) VALUES ('$val1','$val2')";
As for a field type to hold a time. I would recommend TIMESTAMP.

Hope this clears things up some.

#5 Creon

Creon
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 11 April 2006 - 05:16 PM

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

<?php 
// Login file
include("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?>


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)
$insert = "INSERT INTO blog (title,`text`,click,`time`) VALUES ('$title','$text,'','$ttime')";
to
$insert = "INSERT INTO blog (title,`text`,`time`) VALUES ('$title','$text,'$ttime')";




#6 Creon

Creon
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 11 April 2006 - 05:59 PM

[!--quoteo(post=363725:date=Apr 11 2006, 12:16 PM:name=PyroSmurf)--][div class=\'quotetop\']QUOTE(PyroSmurf @ Apr 11 2006, 12:16 PM) View Post[/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

<?php 
// Login file
include("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?>
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)
$insert = "INSERT INTO blog (title,`text`,click,`time`) VALUES ('$title','$text,'','$ttime')";
to
$insert = "INSERT INTO blog (title,`text`,`time`) VALUES ('$title','$text,'$ttime')";
[/quote]


#7 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 11 April 2006 - 06:06 PM

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 click from your query.

#8 Creon

Creon
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 11 April 2006 - 07:51 PM

thats helpfull. tnx. Everything works now.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users