Jump to content

Archived

This topic is now archived and is closed to further replies.

Creon

write to sql database

Recommended Posts

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 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?>
[/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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 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?>
[/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]


Share this post


Link to post
Share on other sites
[!--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 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?>
[/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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.