Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP Databases
PHP Databases Using PHP in conjunction with databases. PHP questions not specific to databases should be directed to one of the other PHP forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old September 25th, 2003, 12:54 PM
Authorized User
Join Date: Aug 2003
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default info from pg won't post to db

I am using MySQL on a non-local server.

My table "hai1" is not accepting input. Here is my table DESC:

pagefrom ENUM('Need','Donate','Copay Need','Copay Donation')
contact VARCHAR(50)
company VARCHAR(255)
address VARCHAR(255)
telephone VARCHAR(20)
needdonortype ENUM('Assistive Devices','Medical Devices','Emergency Cell Phone','All Other')
needdonorspecs VARCHAR(255) NOT NULL
copayamtreqd DOUBLE NOT NULL
copaymedcondtns VARCHAR(255) NOT NULL
copaydonationamt DOUBLE
copaydonationspecs VARCHAR(255) NOT NULL
approved ENUM('Y','N') NOT NULL DEFAULT 'N'
My include has the db connect (which has worked before on this page). On this particular page, I check for blanks from the previous page, then I want it to write to the db, post the db-inserted info onto a new php page, and email the administrator with the information so they can process it. The company, copayamtreqd, copaymedcondtns, copaydonationamt, and copaydonationspecs aren't asked for on this page, so I added "NULL"s to the INSERT INTO command.

<title>Need Help?</title>
if ($email=="") {
    echo "<p>Your email address is required so you can be contacted. Click the Back button to fill in this blank.</p>";

if ($needdonorspecs=="") {
    echo "<p>Specifics are required so someone can meet your need. Click the Back button to fill in this blank.</p>";

include "common.inc";

$link_id = db_connect();

if(!$link_id) die(sql_error());
else {
    $mail_to = "me@mydomain.com";
    $mail_subject = "Need Help Form (WS)";
    $mail_body = "Contact name: ";
    $mail_body .= $contact;
    $mail_body .= "\n";
    $mail_body .= "Address: ";
    $mail_body .= $address;
    $mail_body .= "\n";
    $mail_body .= "Telephone: ";
    $mail_body .= $telephone;
    $mail_body .= "\n";
    $mail_body .= "Email: ";
    $mail_body .= $email;
    $mail_body .= "\n";
    $mail_body .= "I need: ";
    $mail_body .= $needdonortype;
    $mail_body .= "\n";
    $mail_body .= "Please describe what exactly you need and why: ";
    $mail_body .= $needdonorspecs;
    $mail_body .= "\n";
    $mail_from = "From: ".$email."\nReply-To: ".$mail_from;

    if(mail($mail_to, $mail_subject, $mail_body, $mail_from)) {
        $result = mysql_query("INSERT INTO hai1 VALUES(NULL, CURDATE(), 'Need', '$contact', NULL, '$address', '$telephone', '$email', '$needdonortype', '$needdonorspecs', NULL, NULL, NULL, NULL, 'N')",$link_id);
        echo "<h1>Thank you!</h1>";
        echo "<p>The following information was entered into our database:</p>";
        echo "<p>Contact name: ";
        echo $contact;
        echo "<br>";
        echo "Address: ";
        echo $address;
        echo "<br>";
        echo "Telephone: ";
        echo $telephone;
        echo "<br>";
        echo "Email: ";
        echo $email;
        echo "<br>";
        echo "I need: ";
        echo $needdonortype;
        echo "<br>";
        echo "Please describe what exactly you need and why: ";
        echo $needdonorspecs;
        echo "</p>";
        echo "<p>::::::: End Data Transmission :::::::</p>";
        echo "<p><b>Remember:</b> Your submission has been sent to our administrator for posting approval. It will be available for viewing for approximately 30 days once it is posted. You will be contacted around that time to see if your request needs to be extended.</p>";
    else echo "<p>Operation failed. Please contact us via <a href='mailto:me@mydomain.com?subject=Need_Help_Form-em'>email</a> instead. We are sorry for the inconvenience.</p>";

It produces the php page and emails the administrator, but it won't insert the info into the db.

I don't know where I've gone wrong and would appreciate another pair or two of eyes to help me troubleshoot my coding.

aka Scottiegirl

\"I was put on Earth to accomplish a certain number of things. Right now I am so far behind, I will never die.\" - Calvin, Calvin and Hobbs
Old September 25th, 2003, 11:24 PM
Friend of Wrox
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts

Okay, let's take a look at what you're doing..

If you connect to the database, then
   If you send the mail properly, then
      perform your query, and
      output a message saying you performed the query.

See anything suspicious?

1: You're trying to insert NULL values into "NOT NULL" columns.
2: You send the mail before you perform the query.
3: You never test the result of that query to see if it was successful or not, you just assume that it works and output your message to the screen.

There's nothing in your code to suggest that the mail should be sent or the output should be displayed ONLY if your query succeeds.

You can use mysql_error()* to get the actual error message from MySQL and verify my guess.

* You should probably use db_error() and db_query instead of the mysql_xxx() functions.

Take care,

Old September 26th, 2003, 09:02 AM
Authorized User
Join Date: Aug 2003
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts


Thanks for the keen eye. You haven't let me down since I've been posting, even in the pre-renovated P2P forum.

1. I will insert "N/A" into the NOT NULL fields that won't get info from that particular screen.

2. I see where my "logic tree" is faulty.

3. My mysql_error() function is buried in my include file. No error pops up, just the "Thank you!" message.

You said "You should probably use db_error() and db_query instead of the mysql_xxx() functions." In all of the online documentation and books I have, there is no mention of "db_error" or "db_query." Where can I find info on those or are you up to giving me a primer?

Thanks again for all you've done to make my programming better! :D

Old September 26th, 2003, 12:48 PM
Friend of Wrox
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts

db_error()nd db_query() are not built-in functions. They provide what's called a "layer of abstraction". By writing your code to use "db_xxxx()" instead of "mysql_xxxx()", you allow yourself to write an application that can run on any number of databases; whenever you install it, you just make sure the right "db_xxxx()" functions are being used.

At their simplest, DB wrapper functions look like this:

function db_query($query)
    return mysql_query($query);

Since you're already using a function called "db_connect()", I had assumed that you're using some sort of complete DB abstraction library. If you're not, continue as you were, but be aware that layers of abstraction are a Good Thing. =)

Take care,


Similar Threads
Thread Thread Starter Forum Replies Last Post
Opening new window to update db info... Danielav21 Classic ASP Professional 3 April 19th, 2007 06:14 PM
ServerFileReportService Db Info at Runtime jason_l_riley BOOK: Professional Crystal Reports for VS.NET 0 January 15th, 2006 09:29 PM
unable to post info into access. teknet Classic ASP Databases 3 February 24th, 2005 02:22 PM
error while trying to display db info darkhalf Classic ASP Databases 1 March 14th, 2004 06:59 AM
Store info into a MySQL DB table KoRn PHP How-To 1 February 17th, 2004 03:00 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.