|
 |
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
|
 |
|
|
 |

September 25th, 2003, 12:54 PM
|
Authorized User
|
|
Join Date: Aug 2003
Location: Schuylerville, NY, USA.
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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:
Code:
id MEDIUMINT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY
date DATE NOT NULL
pagefrom ENUM('Need','Donate','Copay Need','Copay Donation')
contact VARCHAR(50)
company VARCHAR(255)
address VARCHAR(255)
telephone VARCHAR(20)
email VARCHAR(255) NOT NULL
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.
Code:
<html>
<head>
<title>Need Help?</title>
</head>
<body>
<?php
if ($email=="") {
echo "<p>Your email address is required so you can be contacted. Click the Back button to fill in this blank.</p>";
exit;
}
if ($needdonorspecs=="") {
echo "<p>Specifics are required so someone can meet your need. Click the Back button to fill in this blank.</p>";
exit;
}
include "common.inc";
error_reporting(0);
$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>";
}
mysql_close($link_id);
?>
<p> </p>
<p> </p>
<p> </p>
</body>
</html>
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.
Thanks!
__________________
HollyAnn
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
|

September 25th, 2003, 11:24 PM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Location: San Diego, CA, USA
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,
Nik
http://www.bigaction.org/
|

September 26th, 2003, 09:02 AM
|
Authorized User
|
|
Join Date: Aug 2003
Location: Schuylerville, NY, USA.
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
quote:
Nik,
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
HollyAnn
|
|

September 26th, 2003, 12:48 PM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Location: San Diego, CA, USA
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,
Nik
http://www.bigaction.org/
|
Thread Tools |
Search this Thread |
|
|
Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |