PDA

View Full Version : Form to mySQL Database


mccspe
6-30-05, 10:35 AM
Form to database:

I have a simple form, contains only
yourName
and Submit

I need to collect a list of names to a database...but I do not know how to get this to point to my database.

I have loaded the database with this table:

CREATE TABLE Supporters (
yourName VARCHAR(50),
PRIMARY KEY(yourName) )

and this is what I have in the form

Quote:
<form method="post" action="??????????????">
<input type="text" name="yourName" font color="green" font face="Verdana" font
background-color="#ffffff" size="45 maxlength="50" /><br />
<input type="submit" value="Submit" border="0"
STYLE="color: #ffffff; font-family: Tahoma; background-color: green;" />
</form>

So what do I put in the 'action'= ...the part I don't have a clue about is setting up the directing of the page to the database.
Could someone please help me with this.
Thanks
Monica

satis
6-30-05, 12:48 PM
the action you specify is the page you're going to do the processing on. Usually I do the processing on the same page as the form, but if you're starting out you may just want to go to a new page to better segregate all your code till you're more comfortable with it. So, let's do this:


<form method="post" action="addstuff.php">
<input type="text" name="yourName" font color="green" font face="Verdana" font
background-color="#ffffff" size="45 maxlength="50" /><br />
<input type="submit" value="Submit" border="0"
STYLE="color: #ffffff; font-family: Tahoma; background-color: green;" />
</form>


so we're redirecting to another page called addstuff.php. In that, I'd have code similar to this.


<?
if ($_POST[yourName]){ //verify someone didn't submit an empty field
include("connect.php"); //this is your connection stuff
$select = "INSERT INTO Supporters (yourName) VALUES ($_POST[yourName])"; //builds insert statement
$result = mysql_query($select); //executes insert statement against hte database
if(!$result){ //if you don't get a result
die("Failed to insert row");
}
echo 'Name added to database';
}
else{
echo 'You didn\'t put anything in the blank. Please <a href=whatever.php>try again</a>.'
}
?>


I'd probably put more code in there, but that's the basics. For instance, you may want to run a select against the database to make sure the name hasn't already been added. Since it's the key, you can't have duplicates and will get errors if you try.

*edit* fixed some errors

mccspe
6-30-05, 10:42 PM
Thanks for your reply:)

I have followed the part correctly to the index.htm page as in your 1st part of the quote

<form method="post" action="addstuff.php">
<input type="text" name="yourName" font color="green" font face="Verdana" font
background-color="#ffffff" size="45 maxlength="50" /><br />
<input type="submit" value="Submit" border="0"
STYLE="color: #ffffff; font-family: Tahoma; background-color: green;" />
</form>

This works to lead me to a new page called 'addstuff.php'

<?
if ($_POST[yourName]){ //verify someone didn't submit an empty field
include("connect.php"); //this is your connection stuff
$select = "INSERT INTO Supporters (yourName) VALUES ($_POST[yourName])"; //builds insert statement
$result = mysql_query($select); //executes insert statement against the database
if(!$result){ //if you don't get a result
die("Failed to insert row");
}
echo 'Name added to database';
}
else{
echo 'You didn\'t put anything in the blank.<a href="">Please try again</a>.';
}
?>

and it comes back EVERY time with the ERROR message:
You didn't put anything in the blank.Please try again. (http://url)

I also made a 'connect.php' page:
<?php
// -- mySQL database details

$db_host = "localhost"; // Your database host server, eg. db.server.com
$db_user = "user"; // User who has access to the database
$db_pass = "password"; // User password to access database
$db_name = "databasename"; // Existing database name

// Please, make sure the defined above user have all previleges granted
// and can SELECT, INSERT, UPDATE, ALTER, DELETE, CREATE, DROP

$db_result = $db->connect ( $db_host, $db_user, $db_pass, $db_name );

if ( !$db_result )

{

error ( "mySQL error", mysql_error() );

}

?>

So what step am I missing now, to get this to write to the database...please?

Monica

mitchind
6-30-05, 11:41 PM
Instead of $db_host = "localhost";use$db_host = "mysql##.powweb.com";

Alli
7-1-05, 12:04 AM
The only possible reason for the error is that $_POST[yourName] is not returning true. Try changing it to $_POST['yourName']. If that doesn't help hopefully some of the experts can.

mccspe
7-1-05, 01:00 AM
I tried that change, but that didn't work:((

Any other suggestions please
Monica

Alli
7-1-05, 04:12 AM
Ok, after viewing your source, I've determined the very simple root of the problem. Unlike in the code you pasted here, in the form on your html page you called the input field "yourname". It needs to be "yourName" like you have in the php script. Change that and it should fix the problem. :)

mccspe
7-1-05, 10:38 AM
Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/wearethe/public_html/addstuff.php on line 4

Alli, I changed the 'name' in the index.htm page to read 'yourName' and then got this error above. Here is what I now have in the 'addstuff.php page...

<?
if ($_POST['yourName']){ //verify someone didn't submit an empty field
include("connect.php"); //this is your connection stuff
$select = "INSERT INTO Supporters (yourName) VALUES ($_POST['yourName'])"; //builds insert statement
$result = mysql_query($select); //executes insert statement against the database
if(!$result){ //if you don't get a result
die("Failed to insert row");
}
echo 'Name added to database';
}
else{
echo 'You didn\'t put anything in the blank.<a href="">Please try again</a>.';
}
?>

mitchind
7-1-05, 12:53 PM
Change the line
$select = "INSERT INTO Supporters (yourName) VALUES ($_POST['yourName'])";
to
$select = "INSERT INTO Supporters (yourName) VALUES ('" . $_POST['yourName'] . "')";

mccspe
7-1-05, 11:19 PM
<form method="post" action="addstuff.php">
<input type="text" name="yourName" font color="green" font face="Verdana" font
background-color="#ffffff" size="45 maxlength="50" /><br />
<input type="submit" value="Submit" border="0"
STYLE="color: #ffffff; font-family: Tahoma; background-color: green;" />
</form>

<?php
if ($_POST[yourName]){ //verify someone didn't submit an empty field
include("connect.php"); //this is your connection stuff
$select = "INSERT INTO Supporters (yourName) VALUES ('" . $_POST['yourName'] . "')"; //builds insert statement
$result = mysql_query($select); //executes insert statement against the database
if(!$result){ //if you don't get a result
die("Failed to insert row");
}
echo 'Name added to database';
}
else{
echo 'You didn\'t put anything in the blank.<a href="http://url">Please try again.</a>';
}
?>


<?
// -- mySQL database details

$db_host = "mysql##.powweb.com"; // Your database host server, eg. db.server.com
$db_user = "user"; // User who has access to the database
$db_pass = "password"; // User password to access database
$db_name = "db_name"; // Existing database name

$db_result = $db->connect ( $db_host, $db_user, $db_pass, $db_name );

?>

As you all can tell, I am a rank amatuer to this stuff, so all help is really appreciated.
What am I missing here to make this work, OR am I way off base with the connect.php page...please?

Monica

mitchind
7-1-05, 11:41 PM
Is that all the code in your whole file "connect.php"?
Where are you getting $db from? You haven't volunteered that but I'm assuming it is there?

I was making some assumptions, but let's start from scratch here....

1. mysql##.powweb.com - replace the ## with your MySQL server (check Member Ops)
2. Same place you check to get your correct username, password and database name
3. In the function I assume you have somewhere, it should be doing something like...
$db = mysql_connect($db_host, $db_user, $db_pass);
and then should be selecting your database with ...
mysql_select_db($database);

mitchind
7-2-05, 12:33 PM
Didn't realize this was your own site - it helps us to figure out things if you can say that up front. :)

Here's a quick and dirty connect.php that should work for you. But I'd suggest you look up the many php/MySQL database examples out there. There's probably some good starting URLs in the PHP forum. And there's lots of sample code there too if you want to search the threads there.

<?php
$db_host = "localhost"; // for database on your own machine
$db_user = "user"; // User who has access to the database
$db_pass = "password"; // User password to access database
$db_name = "db_name"; // Existing database name

$db = mysql_connect($db_host, $db_user, $db_pass) or die ("Error Connecting to MySQL Host: $host\n" . mysql_error() . "\n");

mysql_select_db($database) or die ("Error Connecting to MySQL Database: $database\n" . mysql_error()) . "\n";
?>

If you have more questions, the PHP forum is probably a better place to start. Good luck! :)