Page 1 of 1

form php dropdown menu

Posted: Wed Jul 22, 2009 1:49 pm
by Adam_C
Hi,

<?
$user="username";
$password="password";
$database="dbname";
$con = mysql_connect (localhost,$user,$password);
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("adamc_stories", $con);
$first=mysql_real_escape_string($_POST['first']);
$last=mysql_real_escape_string($_POST['last']);
$email=mysql_real_escape_string($_POST['email']);
$story=mysql_real_escape_string($_POST['story']);
$title = $_POST['category'];
if ($title == $_POST['home']) {
$sql1="INSERT INTO home_stories (first,last,email,story) VALUES ('$first','$last','$email','$story')";
}
if ($title == $_POST['work']) {
$sql2="INSERT INTO work_stories (first,last,email,story) VALUES ('$first','$last','$email','$story')";
}
if ($title == $_POST['family']) {
$sql3="INSERT INTO family_stories (first,last,email,story) VALUES ('$first','$last','$email','$story')";
}
if ($title == $_POST['friends']) {
$sql4="INSERT INTO friends_stories (first,last,email,story) VALUES ('$first','$last','$email','$story')";
}
if ($title == $_POST['strangers']) {
$sql5="INSERT INTO strangers_stories (first,last,email,story) VALUES ('$first','$last','$email','$story')";
}
if ($title == $_POST['transport']) {
$sql6="INSERT INTO transport_stories (first,last,email,story) VALUES ('$first','$last','$email','$story')";
}
if (!mysql_query($sql1,$con)) {
die('Error: ' . mysql_error());
}
if (!mysql_query($sql2,$con)) {
die('Error: ' . mysql_error());
}
if (!mysql_query($sql3,$con)) {
die('Error: ' . mysql_error());
}
if (!mysql_query($sql4,$con)) {
die('Error: ' . mysql_error());
}
if (!mysql_query($sql5,$con)) {
die('Error: ' . mysql_error());
}
if (!mysql_query($sql6,$con)) {
die('Error: ' . mysql_error());
}
echo "The form data was successfully added to your database.";
mysql_close($con);
?>

I have the above php file called post.php to post the code:

<form method="post" action="post.php" name="update">
<textarea name="story" onclick="if(this.value == 'Input Story Here . . .'){this.value = ''};" style="width:553px; height:110px;color:#666666">Input Story Here . . .</textarea>
<br />
<br />
<br />
<input type="text" name="first" value="First Name" style="color:#666666;" onclick="if(this.value == 'First Name'){this.value = ''};" align="middle" />
<input type="text" name="last" value="Last Name" style="color:#666666;" onclick="if(this.value == 'Last Name'){this.value = ''};" align="middle" />
<input type="text" name="email" value="Email" style="color:#666666;" onclick="if(this.value == 'Email'){this.value = ''};" align="middle" />
<select name="category" title="category" style="color:#666666;" >
<option value="home">Home</option>
<option value="work">Work</option>
<option value="family">Family</option>
<option value="friends">Friends</option>
<option value="strangers">Strangers</option>
<option value="transport">Transport</option>
<option value="choose" selected>Please Select...</option>
</select>
<img src="images/spacer.png" width="15" height="1" />
<input type="image" src="images/go.png" name="submit" align="middle" />
</form>

However for some reason I can't add anything into the database according to which table i would like the data to be added to.

Basically I want my drop down menu to correspond to which table i want the record to be entered into.

by the way the website can be found here: http://www.selfamusingstories.elementfx.com/ Please help !

Adam Carter

Re: form php dropdown menu

Posted: Wed Jul 22, 2009 2:01 pm
by kaszu
Link you gave doesn't work and you haven't used 'code' BB tags
 
$_POST values after form submission are: $_POST['first'], $_POST['last'], $_POST['email'], $_POST['category'], $_POST['story']
but not $_POST['home'], $_POST['work'], etc.
 
Instead of

Code: Select all

if ($title == $_POST['home']) {
you should have

Code: Select all

if ($title == 'home') {
 
Also $title value can be only 'home' or 'work' or any other, but not all at the same time, you need only 1 mysql_query call, not 6. It should be:

Code: Select all

 
$sql = null;
if ($title == 'home') {
$sql="INSERT INTO home_stories (first,last,email,story) VALUES ('$first','$last','$email','$story')";
}
if ($title == 'work') {
$sql="INSERT INTO work_stories (first,last,email,story) VALUES ('$first','$last','$email','$story')";
}
...
if ($sql) {
    if (!mysql_query($sql,$con)) {
        die('Error: ' . mysql_error());
    } 
}
 

Re: form php dropdown menu

Posted: Wed Jul 22, 2009 3:04 pm
by Adam_C
thank you so much for your reply and help ! :) :)

there are two things however i would like help with.

(1) send an email to me when a new record is entered into the database with it set out like this:

Dear Adam,

The user 'first' 'last' has added data to the 'table name', this data is from 'email' and they have added this story:
'story'

thats not a final draft just a rough email so i know when they have sent it.

(2) how do i read this information in each section of the site? e.g when someone adds their information it comes up on the appropriate part for example if they add to 'home_story' then it comes up on the 'storyhome.html' as it would like on a site such as twitter or facebook etc.

does this make sense? and here is the link to the site: http://www.selfamusingstories.elementfx.com/index.html