Problem posting to Data Base-finally solved

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
planethax
Forum Newbie
Posts: 21
Joined: Thu Sep 11, 2003 9:34 am

Problem posting to Data Base-finally solved

Post by planethax »

I have been trying to solve this bug since Thursday night, I am new at PHP and must be overlooking something.
I am tring to take some values from registration.php and post them to #1 Data Base, #2 Send a message to user #3 email value to user and myself.
#2 and 3 work evertime.
As for #1 and posting values to Data base, there are 28 values, If I dont fill in all of the first 13 values, it doesnt post to data base and I can not figure why?
Below is the PHP code for both registration.php and reg.php

Registration.php

Code: Select all

<?php
include("config.php");
if($loged=='yes')
{
echo "$lang[registerloged]";
}
else
{
?>
<font size="2">Only the Username, Passwords and first Email Inputs are required.<br>
Phone, Fax, Street address and Postal code will <b>NOT</b> be visable to others.</font>
<form action="reg.php" method="post">
<table><tr><td><?php echo $lang[username] ?> :</td>
<td><input type="text" name="username"></td></tr>
<tr><td><?php echo $lang[password] ?> :</td> 
<td><input type="password" name="password"><BR></td></tr>
<tr><td><?php echo $lang[passwordagain] ?> :</td>
<td><input type="password" name="password2"></td></tr>
<tr><td><?php echo $lang[email] ?> : </td>
<td><input type="text" name="emaila"></td></tr>
<tr><td><?php echo $lang[fname] ?> : </td>
<td><input type="text" name="fname"></td></tr>
<tr><td><?php echo $lang[email] ?> : </td>  
<td><input type="text" name="emailz"> 
<font size="-2">Input Email here <i>again</i> to make Public.</font></td></tr>
<tr><td><?php echo $lang[lname] ?> : </td> 
<td><input type="text" name="sname"></td></tr> 
<tr><td><?php echo $lang[street] ?> : </td>  
<td><input type="text" name="street"></td></tr>
<tr><td><?php echo $lang[street2] ?> : </td>  
<td><input type="text" name="street2"></td></tr>
<tr><td><?php echo $lang[city] ?> : </td>
<td><input type="text" name="city"></td></tr>
<tr><td><?php echo $lang[prov] ?> : </td>
<td><input type="text" name="prov"></td></tr>
<tr><td><?php echo $lang[post] ?> : </td>  
<td><input type="text" name="post"></td></tr>
<tr><td><?php echo $lang[country] ?> : </td> 
<td><input type="text" name="country"></td></tr>
<tr><td colspan="2"><b>Misc. Info </b>(not required)<br></td></tr>
<tr><td><?php echo $lang[phone] ?> : </td> 
<td><input type="text" name="phone"></td></tr>
<tr><td><?php echo $lang[fax] ?> : </td>
<td><input type="text" name="fax"></td></tr>
<tr><td><?php echo $lang[hpage] ?> : </td>
<td><input type="text" name="hpage"></td></tr></table>

 <b><?php echo $lang[minterests] ?></b>
 Check all that apply:<br>
<?php echo $lang[pubs] ?> : &nbsp;<input type="checkbox" name="pubs" value="Pubs," > &nbsp;&nbsp;&nbsp;&nbsp; 
<?php echo $lang[bgrills] ?> : <input type="checkbox" name="bgrills" value="Bars & Grills,"><br>
 <?php echo $lang[clubs] ?> : <input type="checkbox" name="clubs" value="Clubs"> 
 &nbsp;&nbsp;&nbsp;
<?php echo $lang[dances] ?> :  &nbsp;&nbsp;  &nbsp;&nbsp;&nbsp; 
<input type="checkbox"
 name="dances" value="Dances,">
<br>
<br>
<?php echo $lang[xdancers] ?> : <br>
<?php echo $lang[emale] ?> :<input type="checkbox"  name="emale" value="Male,"> 
 <?php echo $lang[efemale] ?> : <input type="checkbox" name="efemale" value="Female,">
 <?php echo $lang[eboth] ?> :<input type="checkbox" name="eboth" value="Both Male & Female,"><br><br>
 <?php echo $lang[entertain] ?> :<br>
 &nbsp;<input type="checkbox"  name="mcountry" value="Country,"><?php echo $lang[mcountry] ?> 
 <input type="checkbox" name="mrock" value="Rock,"><?php echo $lang[mrock] ?>  <input type="checkbox"
 name="mhiphop" value="Hip Hop,"><?php echo $lang[mhiphop] ?><br>
 &nbsp;<input type="checkbox"  name="mdance" value="Dance,"> 
 <?php echo $lang[mdance] ?>: 
  <input type="checkbox" name="mrap" value="Rap,">
  <?php echo $lang[mrap] ?>  
  <input type="checkbox"
 name="mmetal" value="Metal,"><?php echo $lang[mmetal] ?><br>

<?php echo $lang[interests] ?> : <TEXTAREA NAME="interests" COLS="40" ROWS="8">
</TEXTAREA><BR>
 

<input type="submit" value="<?php echo $lang[registerbutton] ?>">

</form>
<?php

}
?>
reg.php

Code: Select all

<?
include("config.php");
        $connection = mysql_connect("$server", "$db_user", "$db_pass");
        $db = mysql_select_db("$database", $connection);
        $query = "SELECT * FROM login where username='$username'";
        $result = mysql_query($query, $connection);
	$exuser=mysql_numrows($result);


?>
<?php
if($username=='' OR $password=='' OR $password2=='' OR $fname=='')
{
 echo "$lang[fillfields]";
}
elseif($password!=$password2)
{
echo "$lang[passdontmatch]";
}
elseif($exuser=='1')
{
echo "$lang[usernameregistered]";
}
else
{
include("config.php");
$connection = mysql_connect("$server", "$db_user", "$db_pass");
$db = mysql_select_db("$database", $connection);
$query = "INSERT INTO login (`username`, `password`, `fname`, `sname`, `email`, `country`, `city`, `interests`, `street`, `prov`, `street2`, `post`, `phone`, `fax`, `hpage`, `pubs`, `clubs`, `dances`, `emale`, `efemale`, `eboth`, `mcountry`, `mrock`, `mhiphop`, `mdance`, `mrap`, `bgrills`, `mmetal`)".
  "VALUES ('$username', '$password', '$fname', '$sname', '$emaila', '$country', '$city', '$interests', '$street', '$prov', '$street2', '$post', '$phone', '$fax', '$hpage', '$pubs', '$clubs', '$dances', '$emale', '$efemale', '$eboth', '$mcountry', '$mrock', '$mhiphop', '$mdance', '$mrap', '$bgrills', '$mmetal')";
$result = mysql_query($query, $connection);

echo "$lang[thanksregister]";

	$ida= uniqid('message');
	$ida2= uniqid('$ida');
	$nr= uniqid($ida);
	$date=date('m.j.Y');

$connection = mysql_connect("$server", "$db_user", "$db_pass");
$db = mysql_select_db("$database", $connection);
$query = "SELECT * FROM usermessages";
$query = "INSERT INTO usermessages (`nomer`, `to`, `from`, `date`, `title`, `message`, `new`) VALUES ('$nr', '$username', 'admin', '$date', '$lang[adminwelcome]', '$lang[adminwelcomemessage]', 'yes')";
$result = mysql_query($query, $connection);
}
?>
<?php
$subject="New Member";

//Build your email BODY and assign to var $message
$message.="Thank you for Registering in the TorontoDrinks.com Membership Club \n";
$message.=" Here are the Details of your account.\n";
$message.="Username: $username\n";
$message.="Password: $password\n";
$message.="First Name: $fname\n";
$message.="Sir Name Name: $sname\n";
$message.="EmailA : $emaila\n";
$message.="EmailZ: $emailz\n";
$message.="Street: $street\n";
$message.="Street2: $street2\n";
$message.="City: $city\n";
$message.="Prov: $prov\n";
$message.="Country: $country\n";
$message.="Postal Code: $post\n";
$message.="Misc: $phone $fax $hpage\n";
$message.="Interests: $pubs $clubs $dances $bgrills\n";
$message.="Exotic Dancers: $emale $efemale $eboth\n";
$message.="Bands: $mcountry $mrock $mhiphop $mdance $mrap $mmetal\n";
$message.="Comments: $interests\n";
//Continue until you are satisfied with your message body
$headers.="From: $username <$emaila>\r\n";
$headers.="Reply-To: $emaila\r\n";
$headers.="CC: $emaila\r\n";

// You can find more mail headers at PHP.net

//Formulate mail function here
$send=mail("tdclub@planethax.com",$subject,$message,$headers);
?>
If some one can point me in the right direction I'd appreciate it, thank you.
:(
Last edited by planethax on Tue Sep 23, 2003 10:10 pm, edited 1 time in total.
planethax
Forum Newbie
Posts: 21
Joined: Thu Sep 11, 2003 9:34 am

Post by planethax »

:(
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

You have possible security issues, but aswell perhaps the actual problem within the same...

Check the last link in my signature, and read up on $_POST. If this doesn't apply to you, let us know.
planethax
Forum Newbie
Posts: 21
Joined: Thu Sep 11, 2003 9:34 am

Post by planethax »

I read that topic and changed to $_post, but still have the same results.
If I fill out the first 13 fields the script works right and posts to data base, but if I only fill out the first 4 it does not post?
I have been trying to figure this one out for a week now :)
tsg
Forum Contributor
Posts: 142
Joined: Sun Jan 12, 2003 9:22 pm
Location: SE, Alabama
Contact:

Post by tsg »

This is just a suggestion, don't know if it is your problem .. but I would try changing this line:

Code: Select all

if($username=='' OR $password=='' OR $password2=='' OR $fname=='') {
To:

Code: Select all

if((empty($username)) OR (empty($password)) OR (empty($password2)) OR (empty($fname)) == TRUE) {
Other than that ... maybe try a die statement with the variables to print where you are entering the information to the database and see if you see anything funky.
planethax
Forum Newbie
Posts: 21
Joined: Thu Sep 11, 2003 9:34 am

Post by planethax »

:( still no luck, it makes no sense to me why this is happening
tsg
Forum Contributor
Posts: 142
Joined: Sun Jan 12, 2003 9:22 pm
Location: SE, Alabama
Contact:

Post by tsg »

Are you sure the info is not going to the database? Or is the info going to the database and just not in the email?
planethax
Forum Newbie
Posts: 21
Joined: Thu Sep 11, 2003 9:34 am

Post by planethax »

the email works everytime, even the admin welcome message works, but the variable dont post to data base unless all the first 13 fields are filled
planethax
Forum Newbie
Posts: 21
Joined: Thu Sep 11, 2003 9:34 am

Post by planethax »

if I remove fileds #4 to 13 everything works, but I need those fields to be there but only optional. Can anyone see why the script is doing this?
tsg
Forum Contributor
Posts: 142
Joined: Sun Jan 12, 2003 9:22 pm
Location: SE, Alabama
Contact:

Post by tsg »

If I remember correctly, there are certain words / field names that won't work.

Rename your "post" field, or try it by remove that field.

Tim
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Code: Select all

$query = "INSERT INTO login (`username`, `password`, `fname`, `sname`, `email`, `country`, `city`, `interests`, `street`, `prov`, `street2`, `post`, `phone`, `fax`, `hpage`, `pubs`, `clubs`, `dances`, `emale`, `efemale`, `eboth`, `mcountry`, `mrock`, `mhiphop`, `mdance`, `mrap`, `bgrills`, `mmetal`)".
  "VALUES ('$username', '$password', '$fname', '$sname', '$emaila', '$country', '$city', '$interests', '$street', '$prov', '$street2', '$post', '$phone', '$fax', '$hpage', '$pubs', '$clubs', '$dances', '$emale', '$efemale', '$eboth', '$mcountry', '$mrock', '$mhiphop', '$mdance', '$mrap', '$bgrills', '$mmetal')";
$result = mysql_query($query, $connection) or die(mysql_error()); // to die or not to die  ... crude but effective

...

$query = "INSERT INTO usermessages (`nomer`, `to`, `from`, `date`, `title`, `message`, `new`) VALUES ('$nr', '$username', 'admin', '$date', '$lang[adminwelcome]', '$lang[adminwelcomemessage]', 'yes')";

$result = mysql_query($query, $connection) or die(mysql_error());
maybe mysql wants to tell you something ;-)

see also: http://php.net/mysql_error
planethax
Forum Newbie
Posts: 21
Joined: Thu Sep 11, 2003 9:34 am

Post by planethax »

ok I added

Code: Select all

echo mysql_errno() . ": " . mysql_error(). "\n";
to my code and got the error code#
1062: Duplicate entry '' for key 2
Problem is I am not sure what key2 is, and what it is telling me?
I am getting closer but not quite, thanks for all ur help!!

Code: Select all

<?
include("config.php");
        $connection = mysql_connect("$server", "$db_user", "$db_pass");
        $db = mysql_select_db("$database", $connection);
        $query = "SELECT * FROM login where username='$username'";
        $result = mysql_query($query, $connection);
	$exuser=mysql_numrows($result);


?>
<?php
if((empty($username)) OR (empty($password)) OR (empty($password2)) OR (empty($fname)) == TRUE)
{
 echo "$lang[fillfields]";
}
elseif($password!=$password2)
{
echo "$lang[passdontmatch]";
}
elseif($exuser=='1')
{
echo "$lang[usernameregistered]";
}
else
{
include("config.php");
$connection = mysql_connect("$server", "$db_user", "$db_pass");
$db = mysql_select_db("$database", $connection);
$query = "INSERT INTO login (`username`,`password`,`fname`,`sname`,`email`,`country`,`city`,`interests`,`street`,`prov`,`street2`,`post`,`phone`,`fax`,`hpage`,`pubs`,`clubs`,`dances`,`emale`,`efemale`,`eboth`,`mcountry`,`mrock`,`mhiphop`,`mdance`,`mrap`,`bgrills`,`mmetal`)".
  "VALUES ('$username', '$password', '$fname', '$sname', '$emaila', '$country', '$city', '$interests', '$street', '$prov', '$street2', '$post', '$phone', '$fax', '$hpage', '$pubs', '$clubs', '$dances', '$emale', '$efemale', '$eboth', '$mcountry', '$mrock', '$mhiphop', '$mdance', '$mrap', '$bgrills', '$mmetal')";
$result = mysql_query($query, $connection);
echo mysql_errno() . ": " . mysql_error(). "\n";

echo "$lang[thanksregister]";

	$ida= uniqid('message');
	$ida2= uniqid('$ida');
	$nr= uniqid($ida);
	$date=date('m.j.Y');

$connection = mysql_connect("$server", "$db_user", "$db_pass");
$db = mysql_select_db("$database", $connection);
$query = "SELECT * FROM usermessages";
$query = "INSERT INTO usermessages (`nomer`, `to`, `from`, `date`, `title`, `message`, `new`) VALUES ('$nr', '$username', 'admin', '$date', '$lang[adminwelcome]', '$lang[adminwelcomemessage]', 'yes')";
$result = mysql_query($query, $connection);
}
?>


<?php
$subject="New Member";

//Build your email BODY and assign to var $message
$message.="Thank you for Registering in the TorontoDrinks.com Membership Club \n";
$message.=" Here are the Details of your account.\n";
$message.="Username: $username\n";
$message.="Password: $password\n";
$message.="First Name: $fname\n";
$message.="Sir Name Name: $sname\n";
$message.="EmailA : $emaila\n";
$message.="EmailZ: $emailz\n";
$message.="Street: $street\n";
$message.="Street2: $street2\n";
$message.="City: $city\n";
$message.="Prov: $prov\n";
$message.="Country: $country\n";
$message.="Postal Code: $post\n";
$message.="Misc: $phone $fax $hpage\n";
$message.="Interests: $pubs $clubs $dances $bgrills\n";
$message.="Exotic Dancers: $emale $efemale $eboth\n";
$message.="Bands: $mcountry $mrock $mhiphop $mdance $mrap $mmetal\n";
$message.="Comments: $interests\n";
//Continue until you are satisfied with your message body
$headers.="From: $username <$emaila>\r\n";
$headers.="Reply-To: $emaila\r\n";
$headers.="CC: $emaila\r\n";

// You can find more mail headers at PHP.net

//Formulate mail function here
$send=mail("tdclub@planethax.com",$subject,$message,$headers);
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

it's probably the field password that's causing trouble but what does

Code: Select all

EXPLAIN login
give you?

http://www.mysql.com/doc/en/EXPLAIN.html
planethax
Forum Newbie
Posts: 21
Joined: Thu Sep 11, 2003 9:34 am

Post by planethax »

ok, went into database, noticed fields 9-13 were type "text" all others were varchar. So I change 9-13 to varchar and now everything is working correctly, thanks everyone for your help!
User avatar
Leviathan
Forum Commoner
Posts: 36
Joined: Tue Sep 23, 2003 7:00 pm
Location: Waterloo, ON (Currently in Vancouver, BC)

Post by Leviathan »

That sound a bit hackish. While I think that varchar(255) is the best choice for all your fields, there's no reason why your stuff shouldn't have worked for text fields as well. Are you sure that all the text fields in the database were set to be optional (ie they can contain null)?

One technical point (since presentation is important for a website): I believe you mean to use Surname instead of Sir Name Name for your e-mail that you send :)
Post Reply