468,247 Members | 1,302 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,247 developers. It's quick & easy.

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 1963
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Wayne Aprato | last post: by
11 posts views Thread by John Ortt | last post: by
30 posts views Thread by MikeC | last post: by
5 posts views Thread by Slavan | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.