form data isn't making it to the database. any help?

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

form data isn't making it to the database. any help?

Postby rcayea » Mon Aug 15, 2011 11:48 pm

Hey everyone,

I am in need of that high quality help I receive when visiting this website. In short, I have a php script that is supposed to automatically submit data to a MySQL database but the data doesn't seem to make it to the database. I am not good at debugging and I am not getting any errors on the pages when I submit an entry.

Basically I have this code which takes in the information and as you know/can tell it calls on another php script to do the auto submit to the database. Here is this code:


</head>


<?php
include('../templates/bodyandlogo.htm');
?>


<?php
include('../templates/navmenu.htm');
?>
<br />
<br />


<h1>PoemScribe: Order Form</h1>
<br />
<br />
<p>Please complete the form below and click submit to order your personalized poem.</p>
<br />
<br />

<div id="form">
<form action="../php_scripts/orderform.php" method="post">

<label for="firstname">First Name:</label>
<div class="input"><input type="text" id="firstname" name="firstname" /></div><br />

<label for="lastname">Last Name:</label>
<div class="input"><input type="text" id="lastname" name="lastname" /></div><br />

<label for="email">Email:</label>
<div class="input"><input type="text" id="email" name="email" /></div><br />

<label for="selectpoemtype">Select Poem Type:<br />
4-Line Poem: $15
8-Line Poem: $25
12-Line Poem: $40
16-Line Poem: $50
20-Line Poem: $75
Please type in your desired poem length. Example: "4line".
</label><br />
<div class="input"><input type="text" id="selectpoemtype" name="selectpoemtype" /><br />

<br />
<br />

<label for="keynames">"Key names to include?"</label>
<div class="input"><input type="text" id="keynames" name="keynames"/></div><br />

<label for="specificwords">Any specific words you would like to include in your poem?</label>
<div class="input"><input type="text" id="specificwords" name="specificwords" /></div><br />

<label for="poemisfor">Who is the poem for?</label>
<div class="input"><input type="text" id="poemisfor" name="poemisfor" /></div><br />

<label for="relationship">Relationship to you?</label>
<div class="input"><input type="text" id="relationship" name="relationship" /></div><br />

<label for="occasion">What is the occasion for this poem?</label>
<div class="input"><input type="text" id="occasion" name="occasion" /></div><br />

<label for="overallmessage">What do you want the overall message of this poem to be?</label>
<div class="input"><input type="text" id="overallmessage" name="overallmessage" /></div><br />

<label for="anythingelse">Any other information you would like for me to know?</label>
<div class="input"><input type="text" id="anythingelse" name="anythingelse" /></div><br />

<div class="input"><input type="submit" value="Submit" name="submit"</div>
</form>
</div><!--form-->

<br />
<br />

<?php
include('../templates/footer.htm');




And then the code that is called on (the script called orderform.php):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head><title>PoemScribe Order Form</title>
</head>
<body>

<h2>PoemScribe</h2>

<?php
$email = "myemail@gmail.com"; // Email to notify on error
$first_name = $_POST['firstname'];
$last_name = $_POST['lastname'];
$email = $_POST['email'];
$select_poem_type = $_POST['selectpoemtype'];
$key_names = $_POST['keynames'];
$specific_words = $_POST['specificwords'];
$poem_is_for = $_POST['poemisfor'];
$relationship = $_POST['relationship'];
$occasion = $_POST['occasion'];
$over_all_message = $_POST['overallmessage'];
$anything_else = $_POST['anythingelse'];


$dbc = mysqli_connect('localhost', 'login', 'password', 'database'); " .
or die('Error connecting to MySQL server.');

$query = INSERT INTO poem_orders '(firstname, lastname, email, selectpoemtype, keynames, specificwords, poemisfor, " .
"relationship, occasion, overallmessage, anythingelse)" .

"VALUES ('$first_name', '$last_name', '$email', '$select_poem_type' '$key_names', '$specific_words', '$poem_is_for', " .
"'$relationship', '$occasion', '$over_all_message', '$anything_else')";


echo 'Thank you, ' . $firstname . $lastname;
echo 'Thanks for submitting the form.<br />';
echo ' Poem Type: ' . $selectpoemtype;
echo ' Key names to include: ' . $keynames . '<br />';
echo 'Specific Words To Include: '. $specificwords . '<br />';
echo 'Poem is for: ' . $poemisfor . '<br />';
echo 'Relationship: ' . $relationship . '<br />';
echo 'Occasion: ' . $occasion . '<br />';
echo 'Overall Message: ' . $overallmessage . '<br />';
echo 'You also added: ' . $anything_else;



mysqli_close($dbc);
?>
</body>
</html>


Again, I am trying to figure out why my database isn't being populated when someone clicks on the submit button.

Thanks in advance,
Randy
0
rcayea
 
Posts: 60
Joined: Sun Apr 19, 2009 12:51 am

Postby johnhudson » Tue Aug 16, 2011 6:49 am

There are some obvious imbalances of single and double quotes in the INSERT command; I also don't understand why the values are not part of the same paragraph of text.
johnhudson
LXF regular
 
Posts: 884
Joined: Wed Aug 03, 2005 1:37 pm

Postby rcayea » Tue Aug 16, 2011 1:54 pm

Thanks for the reply. Here is some corrected code. It is getting better but I still get error that query wasn't successful. I fixed the quotes issue, I think.

Code: Select all
<?php
      $email = "email@gmail.com"; // Email to notify on error
        $first_name = $_POST['firstname'];
        $last_name = $_POST['lastname'];
      $email = $_POST['email'];
      $select_poem_type = $_POST['selectpoemtype'];
      $key_names = $_POST['keynames'];
        $specific_words = $_POST['specificwords'];
      $poem_is_for = $_POST['poemisfor'];
      $relationship = $_POST['relationship'];
        $occasion  = $_POST['occasion'];
      $over_all_message = $_POST['overallmessage'];
      $anything_else = $_POST['anythingelse'];








      

$dbc = mysqli_connect('localhost', 'login', 'password', 'database')
   or die('Error connecting to MySQL server.');

$query = "INSERT INTO poem_orders (firstname, lastname, email, selectpoemtype, keynames, specificwords, poemisfor, relationship, occasion, overallmessage, anythingelse) VALUES ('$first_name', '$last_name', '$email', '$select_poem_type' '$key_names', '$specific_words', '$poem_is_for', '$relationship', '$occasion', '$over_all_message', '$anything_else')";

"VALUES ('$first_name', '$last_name', '$email', '$select_poem_type' '$key_names', '$specific_words', '$poem_is_for', " .
"'$relationship', '$occasion', '$over_all_message', '$anything_else')";

$insert = mysqli_query($dbc, $query);




// if query was successful, display message to user:
if($success)
{
     echo 'Thank you, ' . $firstname . $lastname;
     echo 'Thanks for submitting the form.<br />';
     echo ' Poem Type: ' . $selectpoemtype;
     echo ' Key names to include: ' . $keynames . '<br />';
     echo 'Specific Words To Include: '. $specificwords . '<br />';
     echo 'Poem is for: ' . $poemisfor . '<br />';
     echo 'Relationship: ' . $relationship . '<br />';
     echo 'Occasion: ' . $occasion . '<br />';
     echo 'Overall Message: ' . $overallmessage . '<br />';
     echo 'You also added: ' . $anything_else;
}
else
{
     echo 'Error: Query failed.';
}




mysqli_close($dbc);
   ?>
</body>
</html>


rcayea
 
Posts: 60
Joined: Sun Apr 19, 2009 12:51 am

Postby bobthebob1234 » Tue Aug 16, 2011 4:41 pm

Code: Select all
$query = "INSERT INTO poem_orders (firstname, lastname, email, selectpoemtype, keynames, specificwords, poemisfor, relationship, occasion, overallmessage, anythingelse) VALUES ('$first_name', '$last_name', '$email', '$select_poem_type' '$key_names', '$specific_words', '$poem_is_for', '$relationship', '$occasion', '$over_all_message', '$anything_else')";

"VALUES ('$first_name', '$last_name', '$email', '$select_poem_type' '$key_names', '$specific_words', '$poem_is_for', " .
"'$relationship', '$occasion', '$over_all_message', '$anything_else')";


I think something is not quite right here.... if you print $query I think you will find that it just says
Code: Select all
INSERT INTO poem_orders (firstname, lastname, email, selectpoemtype, keynames, specificwords, poemisfor, relationship, occasion, overallmessage, anythingelse) VALUES ('$first_name', '$last_name', '$email', '$select_poem_type' '$key_names', '$specific_words', '$poem_is_for', '$relationship', '$occasion', '$over_all_message', '$anything_else')


I don't think that is what you want...
For certain you have to be lost to find the places that can't be found. Elseways, everyone would know where it was
User avatar
bobthebob1234
LXF regular
 
Posts: 1373
Joined: Thu Jan 03, 2008 9:38 pm
Location: A hole in a field

Postby johnhudson » Tue Aug 16, 2011 6:18 pm

Try this: I don't understand the point of the second VALUES statement and I have made one subtle change, putting the semi-colon inside the quotation marks around the query. Your previous query was incomplete because the semi-colon was not part of it.

Code: Select all
$query = "INSERT INTO poem_orders (firstname, lastname, email, selectpoemtype, keynames, specificwords, poemisfor, relationship, occasion, overallmessage, anythingelse) VALUES ('$first_name', '$last_name', '$email', '$select_poem_type' '$key_names', '$specific_words', '$poem_is_for', '$relationship', '$occasion', '$over_all_message', '$anything_else');"
johnhudson
LXF regular
 
Posts: 884
Joined: Wed Aug 03, 2005 1:37 pm

Postby ollie » Wed Aug 17, 2011 2:55 am

You are also using the variable $email twice. You are setting $email to "myemail@gmail.com" and then 4 lines later setting $email to the submitted email from the form - "$email = $_POST['email'];"

Are you sure this is what you want to do?
User avatar
ollie
Moderator
 
Posts: 2749
Joined: Mon Jul 25, 2005 11:26 am
Location: Bathurst NSW Australia

Postby rcayea » Fri Aug 19, 2011 7:13 pm

Thanks for the tips. They helped solve the problem.
rcayea
 
Posts: 60
Joined: Sun Apr 19, 2009 12:51 am


Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest