PDA

View Full Version : forms in php


Adam_C
07-22-2009, 05:45 PM
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/add-story.php Please help !

Adam Carter :)

edbr
07-23-2009, 01:54 AM
if i see it correctly ypu are passing a value using $_POST['category']
and depending on the value of it selects where you insert
i would look at 1 insert using a variable like
&table=$_POST['category'];

$sql1="INSERT INTO $table (first,last,email,story) VALUES ('$first','$last','$email','$story')";
you need only 1 if then

if(&table==""){echo "please insert selection in category";}
else .....here you add your insert statement