Forum Moderators: coopster
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]
you've forgotten the mysql_query() [php.net] function! add it to your code after the $sql line.
-hakre
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]
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)
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!
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....
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')