Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Can't submit data to mysql via PHP Form
Old 08-26-2010, 12:18 PM Can't submit data to mysql via PHP Form
Novice Talker

Posts: 10
Name: Allen Summy
Trades: 0
I am a newbie at PHP and mysql, and I can't seem to get a PHP form to communicate with my mysql database. I would like to be able to fill out the form and hit the submit button and have the form fields entered into the database. When I hit the submit button, the information seems to just disappear and the page refreshes, but nothing shows up in my database.

Here is the code I am working with:

HTML Code:
<form action="insert.php" method="post"> 
	<p>Title:<br /><input type="text" name="Title" /></p> 
	<p>First Name:<br /> <input type="text" name="First_name" /></p> 
	<p>Last Name:<br /> <input type="text" name="Last_name" /></p> 
	<p>Date:<br /> <input type="text" name="Date" /></p> 
	<p>Weekday:<br /> <input type="text" name="Weekday" /></p> 
	<p>Time:<br /> <input type="text" name="Time" /></p> 
	<p>Series:<br /> <input type="text" name="Series" /></p> 
	<p>URL:<br /> <input type="text" name="URL" /></p> 
	<p>Comments:<br /> <input type="text" name="Comments" /></p> 
	<p><input type="Submit" value="Update Database" /></p> 
</form>
PHP Code:
<?php

$Title 
$_POST['Title'];
$First_name $_POST['First_name'];
$Last_name $_POST['Last_name'];
$Date $_POST['Date'];
$Weekday $_POST['Weekday'];
$Time $_POST['Time'];
$Series $_POST['Series'];
$URL $_POST['URL'];
$Comments $_POST['Comments'];

mysql_connect ("localhost""christia_web""fellowship") or die ('Error: ' .mysql_error());
mysql_select_db ("christia_sermons");

$query="INSERT INTO sermons (ID, Title, First_name, Last_name, Date, Weekday, Time, Series, URL, Comments ('NULL', '".$Title"', '".$First_name"', '".$Last_name"', '".$Date"', '".$Weekday"', '".$Time"', '".$Series"', '".$URL"', '".$Comments"')";

mysql_query($query) or die ('Error updating database');

echo 
"Database Updated With: ".$Title" ".$First_name" ".$Last_name" ".$Date" ".$Weekday" ".$Time" ".$Series" ".$URL" ".$Comments.";

?>
Any help would be greatly appreciated.
Adsummy is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-26-2010, 03:25 PM Re: Can't submit data to mysql via PHP Form
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
It looks like you have a syntax error in your sql query:
PHP Code:
$query="INSERT INTO sermons (ID, Title, First_name, Last_name, Date, Weekday, Time, Series, URL, Comments ('NULL', '".$Title"', '".$First_name"', '".$Last_name"', '".$Date"', '".$Weekday"', '".$Time"', '".$Series"', '".$URL"', '".$Comments"')"
should be

PHP Code:
$query="INSERT INTO sermons (ID, Title, First_name, Last_name, Date, Weekday, Time, Series, URL, Comments) VALUES ('NULL', '".$Title"', '".$First_name"', '".$Last_name"', '".$Date"', '".$Weekday"', '".$Time"', '".$Series"', '".$URL"', '".$Comments"')"
It seems, however, that there must be another issue. This line should be generating an error message:
PHP Code:
mysql_query($query) or die ('Error updating database'); 
I'm not sure what is causing the behavior you describe. Do you have error reporting enabled?

By the way, whenever you post code that involves connecting to a database you may want to remove the database password before posting.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 08-26-2010 at 03:29 PM..
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 08-26-2010, 03:52 PM Re: Can't submit data to mysql via PHP Form
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
There must be other code that you're not showing us, because from what you describe, the PHP code you showed is never executing.

Even with error reporting turned off, the line:
PHP Code:
or die ('Error updating database'
Will kill the page with that error if there's a problem with the query, which there is.
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.
wayfarer07 is online now
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 08-26-2010, 03:57 PM Re: Can't submit data to mysql via PHP Form
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
I pasted your code into my IDE, which highlighted your syntax mistake. The problem is this line:
PHP Code:
$query="INSERT INTO sermons (ID, Title, First_name, Last_name, Date, Weekday, Time, Series, URL, Comments ('NULL', '".$Title"', '".$First_name"', '".$Last_name"', '".$Date"', '".$Weekday"', '".$Time"', '".$Series"', '".$URL"', '".$Comments"')"
You're not concatenating your string correctly. For example, ".$URL" Should be ".$URL." <-- You need the dot (.) on the right side of all the variables to connect them to the next part of the string. Your server has error reporting turned off so is not reporting this. Any PHP after this sort of error will not execute.
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.
wayfarer07 is online now
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 08-26-2010, 05:16 PM Re: Can't submit data to mysql via PHP Form
Novice Talker

Posts: 10
Name: Allen Summy
Trades: 0
Okay, thanks for your replies. I implemented the changes you suggested and I still am not able to do it. I have an html form on my webpage, and here is the code for that:
HTML Code:
<form action="insert.php" method="post"> 
<p>Title:<br /><input type="text" name="Title" /></p> 
<p>First Name:<br /> <input type="text" name="First_name" /></p> 
<p>Last Name:<br /> <input type="text" name="Last_name" /></p> 
<p>Date:<br /> <input type="text" name="Date" /></p> 
<p>Weekday:<br /> <input type="text" name="Weekday" /></p> 
<p>Time:<br /> <input type="text" name="Time" /></p> 
<p>Series:<br /> <input type="text" name="Series" /></p> 
<p>URL:<br /> <input type="text" name="URL" /></p> 
<p>Comments:<br /> <input type="text" name="Comments" /></p> 
<p><input type="Submit" value="Update Database" /></p> 
</form>
I have a separate php file called insert.php, and here is everything that is in that file:
PHP Code:
<?php

$Title 
$_POST['Title'];
$First_name $_POST['First_name'];
$Last_name $_POST['Last_name'];
$Date $_POST['Date'];
$Weekday $_POST['Weekday'];
$Time $_POST['Time'];
$Series $_POST['Series'];
$URL $_POST['URL'];
$Comments $_POST['Comments'];

mysql_connect ("localhost""christia_form",
"**********") or die('Cannot connect to the database because: ' mysql_error());
mysql_select_db ("christia_sermons");


$query="INSERT INTO sermons (ID, Title, First_name, Last_name, Date, Weekday, Time, Series, URL, Comments) VALUES ('NULL.', '".$Title."', '".$First_name."', '".$Last_name."', '".$Date."', '".$Weekday."', '".$Time."', '".$Series."', '".$URL."', '".$Comments."')";   

mysql_query($query) or die ('Error updating database');

echo 
"Database Updated With: ".$Title" ".$First_name" ".$Last_name" ".$Date" ".$Weekday" ".$Time" ".$Series" ".$URL" ".$Comments.";

?>
Again, I appreciate any assistance.
Adsummy is offline
Reply With Quote
View Public Profile
 
Old 08-26-2010, 05:21 PM Re: Can't submit data to mysql via PHP Form
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Check your last line... You're trying to connect $Comments to an open string (which never closes).
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.
wayfarer07 is online now
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 08-26-2010, 05:26 PM Re: Can't submit data to mysql via PHP Form
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Turning on display_errors will make fixing these types of syntax errors much easier.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 08-26-2010, 05:28 PM Re: Can't submit data to mysql via PHP Form
Novice Talker

Posts: 10
Name: Allen Summy
Trades: 0
Okay, I removed the "." after ".$Comments."

Still no go.
Adsummy is offline
Reply With Quote
View Public Profile
 
Old 08-26-2010, 05:37 PM Re: Can't submit data to mysql via PHP Form
Novice Talker

Posts: 10
Name: Allen Summy
Trades: 0
How can I turn on display_errors?
Adsummy is offline
Reply With Quote
View Public Profile
 
Old 08-26-2010, 05:39 PM Re: Can't submit data to mysql via PHP Form
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
You need to remove the final " as well:
PHP Code:
echo "Database Updated With: ".$Title" ".$First_name" ".$Last_name" ".$Date" ".$Weekday" ".$Time" ".$Series" ".$URL" ".$Comments
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 08-26-2010, 05:39 PM Re: Can't submit data to mysql via PHP Form
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by Adsummy View Post
How can I turn on display_errors?
Find the display_errors directive in your php.ini and set it to 1.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 08-26-2010, 05:54 PM Re: Can't submit data to mysql via PHP Form
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Quote:
Originally Posted by Adsummy View Post
How can I turn on display_errors?
PHP Code:
<?php
ini_set
('display_errors'1);
?>
is one way
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.
wayfarer07 is online now
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 08-26-2010, 06:11 PM Re: Can't submit data to mysql via PHP Form
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Using ini_set to enable display errors at runtime won't work if there are fatal errors (such as syntax errors) in the script.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 08-26-2010, 06:30 PM Re: Can't submit data to mysql via PHP Form
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Quote:
Originally Posted by NullPointer View Post
Using ini_set to enable display errors at runtime won't work if there are fatal errors (such as syntax errors) in the script.
That actually depends on the hierarchy of files and how they are loaded. If you have one initializing file (with ini_set('display_errors', 1)) that then loads some function which in turn includes a page which it turns out has a fatal error on it, setting display_errors with ini_set is just fine. This is how I've organized my framework so that it will control error reporting in any situation, no matter what the php.ini is in whatever server it is dropped into.

I've just done some testing on a page where I intentionally created a fatal error. Although the documentation states explicitly that ini_set() cannot be used to control error reporting if there is a fatal error, my testing just told me there are actually some exceptions to this. The server I was using had errors set to on by default, and I was turning it off.

The error I created was a require_once('somefilethatdoesnotexits.asdf'); That's a fatal error because it's require_once. ini_set('display_errors', 0) suppressed it. So it's not a perfect rule. Parse errors are not affected by ini_set, but some fatal errors actually are, despite what the documentation says. I also found a simple warning that was not affected by the rule, so it's not only fatal errors that are affected here.

Another way to set error reporting is with .htaccess: http://perishablepress.com/press/200...-via-htaccess/ This might be useful for anyone with a host that does not allow you access to php.ini. Most hosts let you edit this, but not all.
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.

Last edited by wayfarer07; 08-26-2010 at 06:40 PM..
wayfarer07 is online now
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 08-26-2010, 07:12 PM Re: Can't submit data to mysql via PHP Form
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
I've tested this with an error handler and it appears that it is possible to catch the error generated by require_once. What's interesting is that the error level is 2, which is a warning. The documentation for require says that it issues a E_COMPILE_ERROR (64). If for some reason it is issuing both, I'm only able to catch the warning.

In any case I suspect Allen will have to either use the htaccess method or edit his php.ini since his problem seems to be syntax related.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 08-26-2010, 09:22 PM Re: Can't submit data to mysql via PHP Form
Novice Talker

Posts: 10
Name: Allen Summy
Trades: 0
Okay, I started from scratch with the php file where the form results are sent to. I tried to follow W3Schools examples, but I still can't make it work. Any ideas why this wouldn't work?

PHP Code:
<?php

$dbh
=mysql_connect ("localhost""christia_form",
"********") or die('Cannot connect to the database because: ' mysql_error());

mysql_select_db ("christia_sermons");

$sql="INSERT INTO sermons (ID, Title, Firstname, Lastname, Date, Weekday, Time, Series, URL, Comments)
VALUES
('NULL','
$_POST[Title]','$_POST[Firstname]','$_POST[Lastname]','$POST[Date]','$POST[Weekday]','$POST[Time]','$POST[Series]','$POST[URL]','$POST[Comments]')";

if (!
mysql_query($sql,$dbh))
  {
  die(
'Error: ' mysql_error());
  }
echo 
"1 record added";

mysql_close($dbh)
?>
Adsummy is offline
Reply With Quote
View Public Profile
 
Old 08-26-2010, 10:11 PM Re: Can't submit data to mysql via PHP Form
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
You make several references to $POST instead of $_POST, but that shouldn't prevent the script from executing (though it wouldn't work correctly). Have you enabled error reporting? Debugging would be a lot easier with error messages.

Also, this may not be relevant right now, but taking data from $_POST and inserting putting it directly in a query, without any sanitizing or validation opens you up to sql injection.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 08-27-2010, 08:06 AM Re: Can't submit data to mysql via PHP Form
Novice Talker

Posts: 10
Name: Allen Summy
Trades: 0
Okay, I fixed the references from "$POST" to $_POST. And I'll enable display_error as soon as I get a chance. It might be a little while but I'll post whatever I find. Thanks for the tips.
Adsummy is offline
Reply With Quote
View Public Profile
 
Old 08-28-2010, 01:31 PM Re: Can't submit data to mysql via PHP Form
Novice Talker

Posts: 10
Name: Allen Summy
Trades: 0
Okay, I've been working on different options for getting my php form to insert data into mysql, and I got it to work by inserting the php script directly into the form action. In other words, it does not work if I reference the php externally as in <form action="insert.php" method="get">, but it does work if I insert the php code(the contents of insert.php) where "insert.php" is. It seems like what is happening, is that it never accesses the insert.php file. I am unable to determine why it behaves like this. Please help.
Adsummy is offline
Reply With Quote
View Public Profile
 
Old 08-28-2010, 02:18 PM Re: Can't submit data to mysql via PHP Form
Sleeping Troll's Avatar
Ultra Talker

Posts: 351
Name: Butch Begy
Trades: 0
Save yourself some trouble, php will properly parse vars inside double quotes without concatenation.

"Whatever my literal is $myvar and some more stuff!"

I recommend using $_REQUEST

Also, rather than using IE, Try using FireFox with FireBug for debugging, bet your wondering how I knew? reference "IE get post"
__________________
Sleeping Troll, EMUSE, Mind Expansion...Truly serendipity!

Last edited by Sleeping Troll; 08-28-2010 at 02:22 PM..
Sleeping Troll is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Can't submit data to mysql via PHP Form

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.92999 seconds with 12 queries