Forum Moderators: coopster

Message Too Old, No Replies

help with inserting with php

         

gswahla

9:12 pm on Jul 17, 2003 (gmt 0)

10+ Year Member



I have created a database with sql called 'chana' and a table called 'milton'.

What I want to do is create a php page, which will let me add to the database.

I have tried the following, please could you lot help me out...

First I created a file that I will include in all my php files...this is used to connect to the db:

'always.php'

<?php

#test_holding_inc.php
#holds initial connection and changing to working
#database
$connection=mysql_connect("localhost","","");
if(!$connection) {
echo "Could not connect to the mySQL server!";
exit;
}
$db=mysql_select_db("chana",$connection);
if (!$db) {
echo "Could not change into the database";
exit;
}
?>

I have around 10 columns in the DB, they are just normal fields that I want numerical values entered.....so I created the following two files:

'milton_insert.html'

<modnote>just the form

and the following:

'milton_insertit.php'

include("always.php");

$sql="INSERT INTO milton (date, counter, cash, cards, cheques, paid_out, total, total_scripts, total_payments, comments) VALUES('$date','$counter','$cash','$cards','$cheques','$paid_out','$total','$total_scripts','$total_payments','$comments')";
mysql_query($sql, $connection);

mysql_close();

echo "Completed...";
echo "<A HREF=\"milton_show.php\">RESULTS HERE</A>";

?>

it doesnt return any errors, but it doesnt add the values to the DB either...

pls help in any way you lot can, thanks!

[edited by: jatar_k at 4:50 pm (utc) on July 18, 2003]
[edit reason] removed extra code [/edit]

hakre

9:38 pm on Jul 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hi gswahla,

you've forgotten the mysql_query() [php.net] function! add it to your code after the $sql line.

-hakre

Timotheos

9:44 pm on Jul 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi hakre the mysql_query is there.

I suspect the single quotes around the variables in your sql statement are creating havoc.

I usually break them out.

$sql="INSERT INTO milton (date, counter, cash, cards, cheques, paid_out, total, total_scripts, total_payments, comments) VALUES('" . $date . "','" . $counter"','....and so on

[edited by: Timotheos at 9:46 pm (utc) on July 17, 2003]

vincevincevince

9:46 pm on Jul 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hakre - that's not the problem, looks like the line is there

change your sql to:

$sql="INSERT INTO `milton` (`date`, `counter`, `cash`, `cards`, `cheques`, `paid_out`, `total`, `total_scripts`, `total_payments`, `comments`) VALUES('$date','$counter','$cash','$cards','$cheques','$paid_out','$total','$total_scripts','$total_payments','$comments')";

and change your query from:

mysql_query($sql, $connection);

to:

if (!mysql_query($sql, $connection)) echo "Error with sql : $sql";

then try to copy and paste the sql output to screen (if there is some) into the mysql command line (or into the SQL box of phpmyadmin)

hakre

9:48 pm on Jul 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



i could swear the query thingy was not in. maybe it's too late for mee to read the screen any longer ;). sorry folks.

gswahla

9:57 pm on Jul 17, 2003 (gmt 0)

10+ Year Member



vincevincevince...i get the following:

Error with sql: INSERT INTO `milton` (`date`, `counter`, `cash`, `cards`, `cheques`, `paid_out`, `total`, `total_scripts`, `total_payments`, `comments`) VALUES(`2003-12-12`,`20`,`20`,`20`,`20`,`1`,`1`,`5`,``,`lookkkk`)

I put this into phpmyadmin and get an error as well!

gswahla

10:00 pm on Jul 17, 2003 (gmt 0)

10+ Year Member



i re-entered the error into phpmyadmin, and it told me that i was using $total_payments in one place, and $total_purchases in another.. (DOH!)

but this didnt sort the problem out....after rectifyin my earlier error....i entered this into phpmyadmin:

INSERT INTO `milton` (`date`, `counter`, `cash`, `cards`, `cheques`, `paid_out`, `total`, `total_scripts`, `total_purchases`, `comments`) VALUES(`2003-12-12`,`20`,`20`,`20`,`20`,`1`,`1`,`5`,`1`,`lookkkk`)

and it tells me:

Unknown column '2003-12-12' in 'field list'

i thought that was the format we had to enter dates in....

vincevincevince

8:24 am on Jul 18, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



don't panic :-D the problem is you are confusing ` and '
they are NOT the same.

around a field you want to refer to in a tale, put `` eg, `date`, `counter`, `cash`

around a value you want to enter into a database, put '' eg, '15-13-2003','7','3.60'

the sql should be something like:

INSERT INTO `milton` (`date`, `counter`, `cash`, `cards`, `cheques`, `paid_out`, `total`, `total_scripts`, `total_payments`, `comments`) VALUES('$date','$counter','$cash','$cards','$cheques','$paid_out','$total','$total_scripts','$total_purchases','$comments')

gswahla

8:55 am on Jul 18, 2003 (gmt 0)

10+ Year Member



works now, thanks so much!

all the advice has been much appreciated!

:)