Jump to content


Photo

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


  • Please log in to reply
5 replies to this topic

#1 ebh

ebh
  • New Members
  • Pip
  • Newbie
  • 3 posts
  • LocationPalatine, IL

Posted 29 October 2003 - 08:23 PM

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);\">&laquo; 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, &amp; 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?

#2 GeoffOs

GeoffOs
  • Members
  • PipPip
  • Member
  • 24 posts
  • LocationCheshire, England

Posted 30 October 2003 - 08:40 PM

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.lyco...?tw=programming
Beyond a critical point within a finite space, freedom diminishes as numbers increase....[br]The human question is not how many can possibly survive within the system, but what kind of existence is possible for those who do survive."[br]-- Frank Herbert - Dune

#3 ebh

ebh
  • New Members
  • Pip
  • Newbie
  • 3 posts
  • LocationPalatine, IL

Posted 31 October 2003 - 01:20 AM

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...

#4 XSpikeX

XSpikeX
  • Members
  • Pip
  • Newbie
  • 2 posts

Posted 31 October 2003 - 03:22 PM

[php:1:4ab6ff7490]<?php
if (!$adv_name &amp;&amp; !$rec_date &amp;&amp; !$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]

#5 Dissonance

Dissonance
  • Members
  • PipPip
  • Member
  • 12 posts
  • LocationChicago, IL

Posted 02 November 2003 - 04:02 AM

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;&amp;amp; !$rec_date &amp;amp;&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]
Friends don't let friends vote Republican.

#6 ebh

ebh
  • New Members
  • Pip
  • Newbie
  • 3 posts
  • LocationPalatine, IL

Posted 02 November 2003 - 05:09 PM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users