473,224 Members | 1,957 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 software developers and data experts.

PHP won't post to database

Hi Guys,

I really am a novice and found a script on webmonkey. Basically all I want
to do is to add records to my database. In my database I have a table called
research_main with the fields already created.

I have tried to bodge a script to work for me. it doesn't. It looks as
though it works but I get no data posted. What have I done wrong?

If you want me to put a statement in anywhere to check something please let
me know where you want me to put it as I really am useless with this.

can you tell me what is wrong with this script.

:-( I am trying honest

<html>

<body>

<?php

if ($submit) {

// process form

$db = mysql_connect("localhost", "uname", "password");

mysql_select_db("dbtouse",$db);

$sql = "INSERT INTO research_main
(date_inv,info,medium_name,place_name,date_death,v erified_event,closed_event
) VALUES
('$date_inv','$info','$medium_name','$place_name', '$date_death','$verified_e
vent','$closed_event')";

$result = mysql_query($sql);

echo "Thank you! Information entered.\n";

} else{

// display form

?>

<form method="post" action="<?php echo $PHP_SELF?>">

<p>Date Of Event:
<input name="date_inv" type="Text" id="date_inv">
<br>

Information to be researched:
<textarea name="info" cols="75" rows="5" id="info"></textarea>
<br>

Medium or Researchers name: :
<input name="medium_name" type="Text" id="medium_name" maxlength="30">
<br>

Place Name: :
<input name="place_name" type="Text" id="place_name" maxlength="50">
<br>
Date of Investigation:
<input name="date_death" type="text" id="date_death">
<br>
Verified Event:
<input name="verified_event" type="radio" value="radiobutton">
Yes
<input name="verified_event" type="radio" value="radiobutton" checked>
No<br>
Event Closed:
<input name="closed_event" type="radio" value="radiobutton">
Yes
<input name="closed_event" type="radio" value="radiobutton" checked>
No<br>

<input type="Submit" name="submit" value="Enter information">
</p>
</form>
<?php

} // end if

?>

</body>

</html>

Jul 17 '05 #1
10 2224
Dan Weeb wrote:
Hi Guys,

I really am a novice and found a script on webmonkey. Basically all I want
to do is to add records to my database. In my database I have a table
called research_main with the fields already created.

I have tried to bodge a script to work for me. it doesn't. It looks as
though it works but I get no data posted. What have I done wrong?

If you want me to put a statement in anywhere to check something please
let me know where you want me to put it as I really am useless with this.

can you tell me what is wrong with this script.

:-( I am trying honest

<html>

<body>

<?php

if ($submit) {

// process form

$db = mysql_connect("localhost", "uname", "password");

mysql_select_db("dbtouse",$db);

$sql = "INSERT INTO research_main
(date_inv,info,medium_name,place_name,date_death,v erified_event,closed_event ) VALUES
('$date_inv','$info','$medium_name','$place_name', '$date_death','$verified_e vent','$closed_event')";

$result = mysql_query($sql);

echo "Thank you! Information entered.\n";

} else{

// display form

?>

<form method="post" action="<?php echo $PHP_SELF?>">

<p>Date Of Event:
<input name="date_inv" type="Text" id="date_inv">
<br>

Information to be researched:
<textarea name="info" cols="75" rows="5" id="info"></textarea>
<br>

Medium or Researchers name: :
<input name="medium_name" type="Text" id="medium_name" maxlength="30">
<br>

Place Name: :
<input name="place_name" type="Text" id="place_name" maxlength="50">
<br>
Date of Investigation:
<input name="date_death" type="text" id="date_death">
<br>
Verified Event:
<input name="verified_event" type="radio" value="radiobutton">
Yes
<input name="verified_event" type="radio" value="radiobutton" checked>
No<br>
Event Closed:
<input name="closed_event" type="radio" value="radiobutton">
Yes
<input name="closed_event" type="radio" value="radiobutton" checked>
No<br>

<input type="Submit" name="submit" value="Enter information">
</p>
</form>
<?php

} // end if

?>

</body>

</html>

Stick this in after your query is executed:

if(!$result)
{
$errMsg = "<div align=center>\n".
"<font size=6 color=red>Error!</font><br>\n".
"An error was detected in your request<br>\n".
"SQL used = '<u>$sql</u>'\n".
"<br>\n".
mysql_errno($db) . ": " .
mysql_error($db) . "\n";
exit($errMsg);
}

If your insert is blowing up this will show you want MySQL is puking on.
Jul 17 '05 #2
Dan Weeb wrote:
If you want me to put a statement in anywhere to check something please let
me know where you want me to put it as I really am useless with this.
Insert/replace the statements I marked with ###

<html>

<body>

<?php
### insert this one
echo 'register_globals is ', (ini_get('register_globals'))?('On'):('Off');
###
if ($submit) {

// process form

$db = mysql_connect("localhost", "uname", "password");
### replace the above with
$db = mysql_connect("localhost", "uname", "password") or die('No Connection!');
###
mysql_select_db("dbtouse",$db);
### replace the above with
mysql_select_db("dbtouse",$db) or die('No Database!');
###
$sql = "INSERT INTO research_main
(date_inv,info,medium_name,place_name,date_death,v erified_event,closed_event
) VALUES
('$date_inv','$info','$medium_name','$place_name', '$date_death','$verified_e
vent','$closed_event')";

$result = mysql_query($sql);
### replace the above with
$result = mysql_query($sql) or die('No Query!');
###
echo "Thank you! Information entered.\n";

(snip)

--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #3
"Dan Weeb" <da********@hotSPAMLESSmail.com> wrote in
news:c0*******************@news.demon.co.uk:
I really am a novice and found a script on webmonkey. Basically all I
want to do is to add records to my database. In my database I have a
table called research_main with the fields already created.

I have tried to bodge a script to work for me. it doesn't. It looks as
though it works but I get no data posted. What have I done wrong?

If you want me to put a statement in anywhere to check something
please let me know where you want me to put it as I really am useless
with this.

can you tell me what is wrong with this script.

:-( I am trying honest

<html>

<body>

<?php

if ($submit) {
There's your problem. Your script assumes that register_globals is set,
which it isn't by default in recent versions of PHP. Since your form is
using the POST method, you need to replace each reference to a variable
that's supposed to come from the form with $_POST['<variable>'], where
<variable> should be replaced with the name (without the $) of the
variable. For example, in the above statement you'd replace $submit with
$_POST['submit'].

// process form

$db = mysql_connect("localhost", "uname", "password");

mysql_select_db("dbtouse",$db);

$sql = "INSERT INTO research_main
(date_inv,info,medium_name,place_name,date_death,v erified_event,closed_
event ) VALUES
('$date_inv','$info','$medium_name','$place_name', '$date_death','$verif
ied_e vent','$closed_event')";
That's fine for learning, but in real life you'll want to escape the
various form parameters so the user can't do mischief like sending an info
value consisting of "');DROP TABLE research_main;".

$result = mysql_query($sql);


And in real life you'll want to check $result to make sure the insert
succeded.
Jul 17 '05 #4
Did you try changing the username and password parts of the connect to what
the username and password is?
Jul 17 '05 #5
I noticed that Message-ID:
<Xn*******************************@130.133.1.4> from Eric Bohlman
contained the following:
That's fine for learning, but in real life you'll want to escape the
various form parameters so the user can't do mischief like sending an info
value consisting of "');DROP TABLE research_main;".


Always thought MySql would not run multiple queries.

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #6
$result = mysql_query($sql) or die('No Query!');


OK Thanks It was a faulty query.

How do I make sue that my php fields match my database? I want the dates
and the radio buttons to put correct info into the database fields

For example in the database

date_event is type date
medium_name is type varchar
info_research is type blob
place_name is type varchar
date_invest is type date
date_death is type date
verified_event type char(3)
closed_event type char(3)

In the php script I have the fields setup as

date_event is type text <------------ Needs to be a date in format
dd/mm/yyyy
medium_name is type text
info_research is type textarea
place_name is type text
date_invest is type text <------------ Needs to be a date in format
dd/mm/yyyy
date_death is type text <------------ Needs to be a date in format
dd/mm/yyyy
verified_event radiobutton
closed_event type radiobutton

Currently if I enter date of 21/12/2004 it displays as 2112-20-04

Thanks so much

Paul

Jul 17 '05 #7
Dan Weeb wrote:
$result = mysql_query($sql) or die('No Query!');
OK Thanks It was a faulty query.

How do I make sue that my php fields match my database? I want the dates
and the radio buttons to put correct info into the database fields


You have to reformat them.
Currently if I enter date of 21/12/2004 it displays as 2112-20-04


Read how MySQL deals with DATE columns:
http://www.mysql.com/doc/en/DATETIME.html
MySQL does not care about anything other than digits in dates.

You *have* to insert them in the format YYYY-MM-DD (or similar ... the
year has to be first, then the month and then the day).
So you have to transform '21/12/2004' to '2004-12-21'.

Here's a fast and dirty way (no error-checking, no different format
allowed, no fancy stuff):
<?php
function dmy2ymd($dmy) {
return substr($dmy, 6, 4) . '-' . substr($dmy, 3, 2) . '-' . substr($dmy, 0, 2);
}
?>
Or you could do as I do and insert DATEs as if they were numbers (no
need to mess around with double and single quotes):

$sql = "insert into table (datecol) values (20041221)";

.... you still need to reformat though :-)
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #8
With total disregard for any kind of safety measures Geoff Berrow
<bl******@ckdog.co.uk> leapt forth and uttered:
Always thought MySql would not run multiple queries.


MySQL will, the mysql_query() API function will not.

--
Phil Roberts | Nobody In Particular | http://www.flatnet.net/
Jul 17 '05 #9
Hello!

If you are trying to run the program in LOCALHOST
you must left password blank.

Try this one for $db variable:

$db = mysql_connect("localhost", "root", "");

I dont know if is this your problem,
I hope this helps.

[]

CM

Mike Discenza <di******@sbcglobal.net> wrote in message news:<j7**********************@newssvr11.news.prod igy.com>...
Dan Weeb wrote:
Hi Guys,

I really am a novice and found a script on webmonkey. Basically all I want
to do is to add records to my database. In my database I have a table
called research_main with the fields already created.

I have tried to bodge a script to work for me. it doesn't. It looks as
though it works but I get no data posted. What have I done wrong?

If you want me to put a statement in anywhere to check something please
let me know where you want me to put it as I really am useless with this.

can you tell me what is wrong with this script.

:-( I am trying honest

<html>

<body>

<?php

if ($submit) {

// process form

$db = mysql_connect("localhost", "uname", "password");

mysql_select_db("dbtouse",$db);

$sql = "INSERT INTO research_main

(date_inv,info,medium_name,place_name,date_death,v erified_event,closed_event
) VALUES

('$date_inv','$info','$medium_name','$place_name', '$date_death','$verified_e
vent','$closed_event')";

$result = mysql_query($sql);

echo "Thank you! Information entered.\n";

} else{

// display form

?>

<form method="post" action="<?php echo $PHP_SELF?>">

<p>Date Of Event:
<input name="date_inv" type="Text" id="date_inv">
<br>

Information to be researched:
<textarea name="info" cols="75" rows="5" id="info"></textarea>
<br>

Medium or Researchers name: :
<input name="medium_name" type="Text" id="medium_name" maxlength="30">
<br>

Place Name: :
<input name="place_name" type="Text" id="place_name" maxlength="50">
<br>
Date of Investigation:
<input name="date_death" type="text" id="date_death">
<br>
Verified Event:
<input name="verified_event" type="radio" value="radiobutton">
Yes
<input name="verified_event" type="radio" value="radiobutton" checked>
No<br>
Event Closed:
<input name="closed_event" type="radio" value="radiobutton">
Yes
<input name="closed_event" type="radio" value="radiobutton" checked>
No<br>

<input type="Submit" name="submit" value="Enter information">
</p>
</form>
<?php

} // end if

?>

</body>

</html>

Stick this in after your query is executed:

if(!$result)
{
$errMsg = "<div align=center>\n".
"<font size=6 color=red>Error!</font><br>\n".
"An error was detected in your request<br>\n".
"SQL used = '<u>$sql</u>'\n".
"<br>\n".
mysql_errno($db) . ": " .
mysql_error($db) . "\n";
exit($errMsg);
}

If your insert is blowing up this will show you want MySQL is puking on.

Jul 17 '05 #10

"Dan Weeb" <da********@hotSPAMLESSmail.com> wrote in message
news:c0*******************@news.demon.co.uk...
$result = mysql_query($sql) or die('No Query!');
OK Thanks It was a faulty query.

How do I make sue that my php fields match my database? I want the dates
and the radio buttons to put correct info into the database fields


Take a look at http://www.tonymarston.co.uk/php-mysql/dateclass.html for a
class which both validates and formats dates.

--
Tony Marston
http://www.tonymarston.net
For example in the database

date_event is type date
medium_name is type varchar
info_research is type blob
place_name is type varchar
date_invest is type date
date_death is type date
verified_event type char(3)
closed_event type char(3)

In the php script I have the fields setup as

date_event is type text <------------ Needs to be a date in format
dd/mm/yyyy
medium_name is type text
info_research is type textarea
place_name is type text
date_invest is type text <------------ Needs to be a date in format
dd/mm/yyyy
date_death is type text <------------ Needs to be a date in format
dd/mm/yyyy
verified_event radiobutton
closed_event type radiobutton

Currently if I enter date of 21/12/2004 it displays as 2112-20-04

Thanks so much

Paul

Jul 17 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Wayne Aprato | last post by:
I have a database that was written in Access 2003 using the Access 2000 file format. When I run the mdb in Access 2003 everything works fine. If I run it in Access 2000 the timer event won't fire...
11
by: John Ortt | last post by:
Hi everyone. I have a database which I have developed in Access 2000 which is working nicely. The problem is that my customer only has Access 97. I tried to convert the database but the main...
30
by: MikeC | last post by:
Good People, I'm writing a backup utility that uses a chdir() to go into the source directory (in which the files reside that I want to back up), so I don't want to use chdir() to get into the...
5
by: Slavan | last post by:
I have an update statement that I'm executing against Oracle database from my C# code and it won't work. UPDATE MenuCaptions SET Caption = N@Caption WHERE MenuId = @MenuId AND CultureId =...
4
by: dac | last post by:
I am quietly going insane on this project. I've never worked on a project like this one before. All my previous sticky forms were for data entry, not editing. I don't know how to display the form...
2
by: adamace5o | last post by:
When i try to use post variables with php and mysql i can't get the insert into statement to accept varibles as values. If i use 'test' instead of $test it does work. I suspect it is something to do...
12
by: tekctrl | last post by:
Environment; Win2K PC with 1Gb of RAM and plenty of HD space running Access 2002 Issue; Access presents a blank data entry form in the Forms view when the New Record icon is used. However, it...
12
by: godiva | last post by:
Hi, Last week one client had errors caused by another program which led the IT guys to wipe out and rebuild the hard drive on one particular computer. Prior to this happening, the people in this...
2
jamwil
by: jamwil | last post by:
What's up guys. I'm having some issues... I've created a method as part of my lifestreaming class which takes an rss feed, and puts the data into a database... It's fairly simple... Check...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.