Jump to content

Archived

This topic is now archived and is closed to further replies.

ebh

mysql_query("INSERT x") via php form issues...

Recommended Posts

First off, let me state that as of two days ago I had never touched SQL -or- PHP and at this point all I\'ve managed to do is create my database, populate it with a small amount of data so I can work on it, and I\'ve learned how to query it via a little php page. Most of my work has been with phpMyAdmin although I did create my initial table via command line.

 

That said, what I\'m working on now is trying to create a form I can hit from the web to insert data into my db.

 

Currently I’ve got my insert.html form, and my insert.php files separated – I believe I can make it a single file if I’m not mistaken, but for now it seems easier for me to keep them split.

 

As it stands right now, when I try to submit my form the javascript kicks me back and tells me I haven’t entered all the required values – It’s supposed to do this if the user leaves out a field, but since I’m completing my form then I assume it’s dying because the values aren’t being passed as variables to the php script.

 

The purpose of this small db/form is simply to track a list of employees, a date, and if they submitted their report (or why they didn\'t) on that date.

 

-- paste of insert.html --

<html>

<head>

<title>insert form</title>

</head>

<body>

<form method="POST" action="./insert.php" name="form">

Advisor\'s Name<BR><input name="adv_name" type="text" id="adv_name" size="50"><BR>

Record Date<BR><input name="rec_date" type="text" id="rec_date" size="10"><BR>

Record Status<select name="rec_status" id="rec_status">

       <option selected>Yes</option>

       <option value="No">No</option>

       <option value="Off Work">Off Work</option>

       <option value="Out Sick">Out Sick</option>

       <option value="Exempt">Exempt</option>

       </select>

       <BR>

<input type="submit" name="Submit" value="Submit">

</form>

</body>

</html>

 

-- paste of insert.php --

[php:1:d09189e900]<?php

// Check if the variables exist (if the form fields have been filled in)

if (!$adv_name && !$rec_date && !$rec_status) {

echo \"You failed to complete all required fields.\";

echo \"Click <A HREF=\"javascript:history.go(-1);\">« here</A> to try again\";

}

else

{

// Connect to the sql database

$link = mysql_connect(\"localhost\", \"viewer\");

$db = mysql_select_db(\"aiu\", $link);

 

// The \'insert\' that will insert the data as a new row into your table

$query = \"INSERT INTO adls_records (adv_name, rec_date, rec_status) VALUES (\'$_POST[adv_name]\', \'$_POST[rec_date]\', \'$_POST[rec_status]\')\";

$result = mysql_query($query, $link);

 

// Echo this message to allow the user to access a new form

echo \"Click <A HREF=\"./insert.html\">here</A> to add another record.\";

}

?>[/php:1:d09189e900]

 

In the PHP script above, & is correctly entered as &

 

I finally got to the point where I\'ll admit I\'m stuck and ask for help, and this seems to be the place for it. Anyone have some advice?

Share this post


Link to post
Share on other sites

I think you need to provide more information. But if you look at your code you are specifying that insert.php is in the directory above, I think you need to change:

<form method="POST" action="./insert.php" name="form">

to

<form method="POST" action="insert.php" name="form"> 

 

Quick google search returned this tutorial: http://hotwired.lycos.com/webmonkey/99/21/...?tw=programming

Share this post


Link to post
Share on other sites

What additional information should I provide?

 

Your suggestion was a good one, however I would point out that ./ references the current directory. To reference one directory higher it would be ../

 

Just by trying to submit data to my insert.html form, the javascript to report errors in insert.php gets triggered, so I know at the very least insert.php is being parsed.

 

Reading the tutorial you linked now...

Share this post


Link to post
Share on other sites

[php:1:4ab6ff7490]<?php

if (!$adv_name && !$rec_date && !$rec_status) {

?>[/php:1:4ab6ff7490]

needs to be

[php:1:4ab6ff7490]<?php

if (empty($adv_name) || empty($rec_date) || empty($rec_status)) {

?>[/php:1:4ab6ff7490]

Share this post


Link to post
Share on other sites

Do you have register_globals on or off? If they\'re off, then you need to use the $_POST superglobal.

 

[php:1:dd7aa15af7]<?php

if (!$adv_name &amp;&amp; !$rec_date &amp;&amp; !$rec_status) {

?>[/php:1:dd7aa15af7]

 

Would need to be changed to this:

 

[php:1:dd7aa15af7]<?php

if (empty( $_POST[\'adv_name\'] ) || empty( $_POST[\'rec_date\'] ) || empty( $_POST[\'rec_status\'] )) {

?>[/php:1:dd7aa15af7]

Share this post


Link to post
Share on other sites

register_globals is on. I commented out my if statement and the insertion code worked fine. After changing it per XSpikeX\'s suggestion (and trying his code with $_POST as well) the if statement still kicked me in the junk, so I just removed it.

 

If I\'m designing an app and I\'m also the only user, I should be smart enough to fill in all the fields without having to babysit myself, right? :?

 

Maybe I\'ll revisit checking field completion once I work out deleting bad entries and customizing the queries to show only what I want, when I want it.

 

Fun stuff, this SQL and PHP... fun stuff. My fiancée is alreaddy hating how late I get to bed now... Ah well.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.