Click to See Complete Forum and Search --> : PHP, MySQL & Forms problem


Nate Hunter
March 20th, 2008, 01:56 PM
I am a newby to this so sorry it this is elementary.

I have a form that I am trying to get to write to a mysql database. The code is below. The problem I am having is I can write to the db but it writes all my values as blanks, no letter or no numbers. If I write in values in the php file it will write to the db but not from a form. Any Ideas what I am doing wrong.


<form action="insert.php" method="post">
<B>First Name: </B><INPUT TYPE="text" NAME="frmfirstname" SIZE=40>
<B>Last Name:</B><INPUT TYPE="text" NAME="frmlastname" SIZE=40><br><br>
<B>Email:</B><INPUT TYPE="text" NAME="frmemail" SIZE=40>
<B>Phone: </B><INPUT TYPE="text" NAME="frmphone" VALUE="(608) " SIZE=20><br><br>

<B>Position: </B><INPUT TYPE="text" NAME="frmposition" SIZE=40>
<B>Time: </B> <INPUT TYPE="text" NAME="frmtimeslot" SIZE=40>
<P><INPUT TYPE="submit" VALUE="Submit"></P>
</FORM>



<?php

// hostname or ip of server (for local testing, localhost should work)
$dbServer='myservername';

// username and password to log onto db server
$dbUser='myusername';
$dbPass='mypassword';

// name of database
$dbName='mydatabase';

$link = mysql_connect("$dbServer", "$dbUser", "$dbPass") or die("Could not connect");

mysql_select_db("$dbName", $link);

$sql="INSERT INTO Volunteers (FirstName, LastName, Email, Phone, Position, TimeSlot)
VALUES ('$_post[frmfirstname]', '$_post[frmlastname]', '$_post[frmemail]', '$_post[frmphone]', '$_post[frmposition]', '$_post[frmtimeslot]')";


if (!mysql_query($sql,$link))
{
die('Error: ' . mysql_error());
}

echo "1 record added";

mysql_close($link);
?>

PeejAvery
March 20th, 2008, 04:15 PM
Your problem is the quotes. You need to learn PHP quotes a little better. Below is the correctly adjusted code.

Notice that it is $_POST and not $_post. Also, since $_POST is an associative array, you need quotes inside the brackets to treat it as a string.

<?php
// hostname or ip of server (for local testing, localhost should work)
$dbServer = 'myservername';

// username and password to log onto db server
$dbUser = 'myusername';
$dbPass = 'mypassword';

// name of database
$dbName = 'mydatabase';

$link = mysql_connect($dbServer, $dbUser, $dbPass) or die("Could not connect");

mysql_select_db($dbName, $link);

$sql = "INSERT INTO Volunteers (FirstName, LastName, Email, Phone, Position, TimeSlot) VALUES ('" . $_POST['frmfirstname'] . "', '" . $_POST['frmlastname'] . "', '" . $_POST['frmemail'] . "', '" . $_POST['frmphone'] . "', '" . $_POST['frmposition'] . "', '" . $_POST['frmtimeslot'] . "')";

if (!mysql_query($sql,$link)) {
die('Error: ' . mysql_error());
}

echo "1 record added";

mysql_close($link);
?>

Nate Hunter
March 20th, 2008, 04:27 PM
this worked great, thanks for you help.