|
17 Oct 2003, 21:53
|
#1
|
Registered User
Join Date: Oct 2000
Posts: 4,944
|
PHP forms and mysql database?
What's the easiest way to input data into a database via a form?
There's loads of 'form processors' but isn't there an easy way to do this?
__________________
I find it kind of funny
I find it kind of sad
The dreams in which i'm dying
Are the best i've ever had
|
|
|
17 Oct 2003, 22:37
|
#2
|
Registered User
Join Date: Jan 2003
Posts: 4,290
|
Re: PHP forms and mysql database?
if(isset($submit)){
mysql_query("INSERT INTO table(bla, blabla) values ($bla,$blabla)");
}
im not eactly sure what you mean though
__________________
im not tolerant, i just dont care.
|
|
|
18 Oct 2003, 12:05
|
#3
|
Registered User
Join Date: Jul 2000
Location: :noitacoL
Posts: 1,200
|
Re: PHP forms and mysql database?
if (isset($_POST['subit'])) { [mysql stuff] }
like wu_trax said, just target your form to the page that holds this (even can be the same page)
|
|
|
18 Oct 2003, 12:12
|
#4
|
Registered User
Join Date: Oct 2000
Posts: 4,944
|
Re: PHP forms and mysql database?
Quote:
Originally Posted by wu_trax
if(isset($submit)){
mysql_query("INSERT INTO table(bla, blabla) values ($bla,$blabla)");
}
im not eactly sure what you mean though
|
ta, i think that does what i want, i assume i need to connect to the db first before doing what you typed above..
/* Connecting, selecting database */
$link = mysql_connect("localhost", "user", "pass")
or die("Could not connect : " . mysql_error());
mysql_select_db("mydb_co_uk_-_1") or die("Could not select database");
is right isn't it?
__________________
I find it kind of funny
I find it kind of sad
The dreams in which i'm dying
Are the best i've ever had
|
|
|
18 Oct 2003, 13:05
|
#5
|
Registered User
Join Date: Oct 2000
Posts: 4,944
|
Re: PHP forms and mysql database?
ok all sorted so far, this should be my last question for a while
using
"UPDATE blah SET one = '$one' ";
works to change the value in field one to the new value of $one, but i want to ADD the value on, not replace. I thought the following would work but it didn't..
"UPDATE table SET one = one + '$one' ";
<edit> SET one = one + $one worked </edit>
__________________
I find it kind of funny
I find it kind of sad
The dreams in which i'm dying
Are the best i've ever had
Last edited by Ragnarak; 18 Oct 2003 at 13:11.
|
|
|
19 Oct 2003, 00:08
|
#6
|
Registered User
Join Date: Oct 2000
Posts: 4,944
|
Re: PHP forms and mysql database?
another problem
i can't get my sql syntax right
$sql = "INSERT INTO table1 (fixture,score,submit) VALUES ('$game','$score','$submit'), INSERT INTO table2(fixture,score,submit) VALUES ('$game','$score','$submit') INSERT INTO table3 (fixture,score,submit) VALUES ('$game','$score','$submit')";
doesn't work
also tried
$sql = "INSERT INTO keller (fixture,kellerscore,submit) VALUES ('$game','$kellerscore','$submit')";
$sql= ."INSERT INTO burch (fixture,burchscore,submit) VALUES ('$game','$burchscore','$submit')";
$sql= ."INSERT INTO hirschfield (fixture,hirschfieldscore,submit) VALUES ('$game','$hirschfieldscore','$submit')";
the following should explain what $sql is
#execute SQL statement
$result = mysql_query($sql, $cid);
__________________
I find it kind of funny
I find it kind of sad
The dreams in which i'm dying
Are the best i've ever had
|
|
|
19 Oct 2003, 00:16
|
#7
|
Henry Kelly
Join Date: Apr 2000
Posts: 7,374
|
Re: PHP forms and mysql database?
Try delimiting multiple queries with a semicolon, not a comma.
__________________
You're now playing ketchup
|
|
|
19 Oct 2003, 00:20
|
#8
|
Registered User
Join Date: Oct 2000
Posts: 4,944
|
Re: PHP forms and mysql database?
Quote:
Originally Posted by pablissimo
Try delimiting multiple queries with a semicolon, not a comma.
|
didn't work
__________________
I find it kind of funny
I find it kind of sad
The dreams in which i'm dying
Are the best i've ever had
|
|
|
19 Oct 2003, 00:30
|
#9
|
Henry Kelly
Join Date: Apr 2000
Posts: 7,374
|
Re: PHP forms and mysql database?
=((
We need RR =(
__________________
You're now playing ketchup
|
|
|
19 Oct 2003, 00:38
|
#10
|
Infallible
Join Date: Feb 2001
Location: Milton Keynes, UK
Posts: 604
|
Re: PHP forms and mysql database?
mysql_query($query) or die(mysql_error());
Will tell you all you need.
[edit]
What the hell are you trying to do? You cannot run multiple queries in one query to the server.
To run multiple queries, you need multiple instances of mysql_query:
Code:
$query = "DELETE FROM table1";
mysql_query($query) or die(mysql_error());
$query = "INSERT INTO table1 (id) VALUES('1')";
mysql_query($query) or die(mysql_error());
|
|
|
19 Oct 2003, 00:52
|
#11
|
Henry Kelly
Join Date: Apr 2000
Posts: 7,374
|
Re: PHP forms and mysql database?
The PHP docs seem to claim that it can be done. But I don't see any reason why you'd want to tbh.
__________________
You're now playing ketchup
|
|
|
19 Oct 2003, 02:15
|
#12
|
Love's Sweet Exile
Join Date: May 2001
Location: Living on a Stair (Now Sword-less)
Posts: 2,371
|
Re: PHP forms and mysql database?
I don't see a reason why
PHP Code:
$query= "insert blah...;"; $query = $query."insert newblah...;"; etc.
wouldn't work tbh (totally untested, but in other SQL environments you can "queue up" the statements before executing them...).....but very rarely will you be entering more than one record from a page, or if you are it will be done in a loop.
__________________
--SYMM--
Ba Ba Ti Ki Di Do
|
|
|
19 Oct 2003, 14:58
|
#13
|
Registered User
Join Date: Oct 2000
Posts: 4,944
|
Re: PHP forms and mysql database?
what i have is a form with a number of fields and i want the value of each field to be entered into a different table
using multiple instances of mysql_query will work i think but i'd read i could do it all on one query
__________________
I find it kind of funny
I find it kind of sad
The dreams in which i'm dying
Are the best i've ever had
|
|
|
19 Oct 2003, 15:29
|
#14
|
Love's Sweet Exile
Join Date: May 2001
Location: Living on a Stair (Now Sword-less)
Posts: 2,371
|
Re: PHP forms and mysql database?
What you had though, replaced the value of $sql with each new line, so only the last line would be sent.
If it works, i still didn't try, you would at least need to send all the queries, separated by ;, to the DB
[edit]
http://aspn.activestate.com/ASPN/Mai...php-db/1795279 says it can't be done. Apparently via command-line it can, but not via web
[/edit]
__________________
--SYMM--
Ba Ba Ti Ki Di Do
|
|
|
19 Oct 2003, 18:37
|
#15
|
Registered User
Join Date: Oct 2000
Posts: 4,944
|
Re: PHP forms and mysql database?
ok that's all working now, thanks
next thing, the form variable can take a value of 0 and when it does i don't want any data to be inserted into the database
i know i need an IF statement somewhre but not sure where or what form it should take :/
__________________
I find it kind of funny
I find it kind of sad
The dreams in which i'm dying
Are the best i've ever had
|
|
|
19 Oct 2003, 19:12
|
#16
|
Street Tramp
Join Date: Apr 2000
Location: Street Gutter
Posts: 341
|
Re: PHP forms and mysql database?
Quote:
Originally Posted by Dave
What the hell are you trying to do? You cannot run multiple queries in one query to the server.
|
Almost correct, certain DB servers still allow you to do this. Versions of MySQL definately allow it (though im unaware which). However, a considerable number of database drivers/providers (ie the process that connects your shiney webpage to the database) no longer allow you to do this. They have 'wizened up' to the fact a lot of people were too crap to parse their own input. This simple step negates a fair amount of sql injection ala " '; DROP table -- " input.
__________________
Chimney Pots.
|
|
|
19 Oct 2003, 19:42
|
#17
|
Registered User
Join Date: Jan 2003
Posts: 4,290
|
Re: PHP forms and mysql database?
Quote:
Originally Posted by Ragnarak
ok that's all working now, thanks
next thing, the form variable can take a value of 0 and when it does i don't want any data to be inserted into the database
i know i need an IF statement somewhre but not sure where or what form it should take :/
|
no insert statement at all or no insert int one field?
i would simply do
if( isset($data) && ($data != 0)){
mysql_query("insert into table(a,b,c) values ($a, $b, $data)");
} else{
mysql_query("insert into table(a,b) values ($a,$b)");
}
or without the else-part if you want no insert at all.
__________________
im not tolerant, i just dont care.
|
|
|
19 Oct 2003, 21:31
|
#18
|
Registered User
Join Date: Oct 2000
Posts: 4,944
|
Re: PHP forms and mysql database?
thanks guys
__________________
I find it kind of funny
I find it kind of sad
The dreams in which i'm dying
Are the best i've ever had
|
|
|
20 Oct 2003, 20:44
|
#19
|
Registered User
Join Date: Oct 2000
Posts: 4,944
|
Re: PHP forms and mysql database?
come across the next thing
i have a 'score' field with lots of values in, i now want to get an 'average score' value for these values.
i have an auto-incrementing 'id' field so what i need to do is total score/id but i'm not sure how to sum all of the values in the field together
am sure it's a simple loop or something but i don't know what.
__________________
I find it kind of funny
I find it kind of sad
The dreams in which i'm dying
Are the best i've ever had
|
|
|
20 Oct 2003, 20:52
|
#20
|
Registered User
Join Date: Jan 2003
Posts: 4,290
|
Re: PHP forms and mysql database?
select avg(scorefield) as avg from tablename
sql does everything for you
there is also max() and min() if you need those values aswell.
__________________
im not tolerant, i just dont care.
|
|
|
20 Oct 2003, 20:55
|
#21
|
Registered User
Join Date: Oct 2000
Posts: 4,944
|
Re: PHP forms and mysql database?
Quote:
Originally Posted by wu_trax
select avg(scorefield) as avg from tablename
sql does everything for you
there is also max() and min() if you need those values aswell.
|
wow
cool
cheers
<edit> Erm how do i get that to display the value?</edit>
__________________
I find it kind of funny
I find it kind of sad
The dreams in which i'm dying
Are the best i've ever had
Last edited by Ragnarak; 20 Oct 2003 at 21:02.
|
|
|
20 Oct 2003, 22:11
|
#22
|
Love's Sweet Exile
Join Date: May 2001
Location: Living on a Stair (Now Sword-less)
Posts: 2,371
|
Re: PHP forms and mysql database?
PHP Code:
$query='SELECT AVG(score) "avg" from <table>'; $result = mysql_query($query); $row=mysql_fetch_array($result); $average=$row['avg']; echo $average;
...or similar...
__________________
--SYMM--
Ba Ba Ti Ki Di Do
|
|
|
20 Oct 2003, 22:17
|
#23
|
Registered User
Join Date: Oct 2000
Posts: 4,944
|
Re: PHP forms and mysql database?
Quote:
Originally Posted by SYMM
PHP Code:
$query='SELECT AVG(score) "avg" from <table>'; $result = mysql_query($query); $row=mysql_fetch_array($result); $average=$row['avg']; echo $average;
...or similar...
|
i'd missed our $row part
thanks
__________________
I find it kind of funny
I find it kind of sad
The dreams in which i'm dying
Are the best i've ever had
|
|
|
|
All times are GMT +1. The time now is 15:25.
| |