Jump to content


Photo

php+mysql is killing me


  • Please log in to reply
26 replies to this topic

#1 xux

xux
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 18 October 2006 - 02:18 PM

:-[ hi,
        I Need help and it is urgent,I want to collect email from a form and save to a database and later output the collected emails in a page(meant for a small website )but I cant just view it,I later went to run select* with phpMyAdmin and I discovered that the email column is empty.Please help me out  here are the codes below
to collect from the form

<?php 
include('header1.tpl');
    $email=$HTTP_POST_VARS['email'];
    
  
 // open connection to MySQL server
$connection = mysql_pconnect('localhost', '', '') 
or die ('Unable to connect!');
// select database for use
mysql_select_db('db') or die ('Unable to select database!');
 $query="INSERT INTO newsletter(email) VALUES ('$email')";
$result=mysql_query($query);
 if ($result)
{
echo'<br/>';
echo'<center>';
echo 'Thank you for signing up for smeresources newsletter';
echo'</center>';
}
else
{
echo'<br/>';
echo'<center>';
echo 'You cant be Registered,try again later';
echo'</center>';

}

?>
</div>


here is the code to display the registered emails

<?php
include('header1.tpl');
// connecting to MySQL server
$connection = mysql_connect('localhost', '', '') 
or die ('Unable to connect!');
// selecting database for use
mysql_select_db('db') or die ('Unable to select database!');
// create and execute query
$query = "SELECT *  FROM newsletter";
$result = mysql_query($query) 
or die ('Error in query: $query. ' . mysql_error());
// check if records were returned
if (mysql_num_rows($result) > 0)
{
// print HTML table
echo'<br/>';
echo '<table width=80% cellpadding=10 cellspacing=0 border=1 align=center>';
echo
'<tr><td><b><center>Email Address of Subscribers</center></b></td></tr>';
echo '<ul>';
// iterate over record set
// print each field
while($row = mysql_fetch_array($result)) // using mysql_fetch_row
{
// prints in format "email"
echo "<tr><td>"; 
echo $row['id'];
echo "</td><td>"; 
echo $row["email"];
echo "</td></tr>"; 
}
echo'</ul>';
echo '</table>';
}
else
{
// print error message
echo 'No rows found!';
}
// once processing is complete
// free result set
mysql_free_result($result);
// close connection to MySQL server
mysql_close($connection);
?>


here is the sql for the table
create TABLE newsletter(
email varchar(60) not null
);
I will appreciate your assistance.Thanks in advance

#2 Destruction

Destruction
  • Members
  • PipPipPip
  • Advanced Member
  • 108 posts

Posted 18 October 2006 - 03:26 PM

Question: why using mysql_fetch_array then using $row as an associative array?  You need to use mysql_fetch_assoc($result) or mysql_fetch_array($result, MYSQL_ASSOC) (I believe, you may want to check the MYSQL_ASSOC though as I'm going from memory from a fair while ago).  Also, your database schema does not have an ID column, so why are you calling $row['id'] ?

If you correct these, it should work for you,

Hope this helps,

Dest

#3 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 18 October 2006 - 03:37 PM

Destruction is right about the 'id' column, it doesn't exist, as for the mysql_fetch_array(), there's nothing wrong with the way you have that.  It will return both a numeric and associative array.  I don't know what the performance overhead on that is, but I always use mysql_fetch_array($result, MYSQL_ASSOC) as opposed to on its own.

I believe it's faster to retrieve as just a numeric array, but I can't remember (MYSQL_NUM).

Also, your code looks ok, so can you post your form.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#4 xux

xux
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 19 October 2006 - 08:02 AM

Thanks,
          Here is my form
<form method="post" action="signup.php" style="display:inline;">
					Enter Email:<br />
					<input type="text" size="20" style="font-size: 10px;"  /><br />
					<img src="images/spacer.gif" width="1" height="9" /><br/>
					<a href="signup.php?a=subscribe"><img src="images/buttons_r1_c1.gif" width="74" height="18" border="0" alt="Submit" /></a>
									</form>

After rectifying the id column in the mysql,it is just outputting numbers and leaving the email section blank.whatelse do you think can be done?Thanks in advance


#5 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 19 October 2006 - 08:21 AM

Your problem is in your form...

Change this:
<input type="text" size="20" style="font-size: 10px;"  />

To this:
<input type="text" name="email" size="20" style="font-size: 10px;"  />

You didn't put the name in the form, so this line:
$email=$HTTP_POST_VARS['email'];
is looking for a field named 'email' and it isn't getting passed one.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#6 xux

xux
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 19 October 2006 - 01:23 PM

Hi,
  After Implementing the change it is still not displaying well.I later realised that the problem is from the intake maybe the signup.php has the problem (cos I have fixed the form),so what do you suggest?
My Regards

#7 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 19 October 2006 - 01:44 PM

What version of PHP are you using?

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#8 xux

xux
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 19 October 2006 - 02:27 PM

version 4

#9 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 19 October 2006 - 02:34 PM

Version 4 what?

Version 4.1.0, or 4.3.2 or something else?

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#10 xux

xux
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 19 October 2006 - 02:51 PM

4.1.0

#11 xux

xux
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 19 October 2006 - 03:08 PM

Huggie,
        I actually populated the database from the phpMyAdmin but the subsrciber file cant display the data and am even suspecting that the emails are not even getting inserted from the form too even after the modification you suggested.Thanks in advance for your help.
My Regards,
XUX

#12 xux

xux
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 19 October 2006 - 03:13 PM

Huggie,
        I got to go,but I wil back online probably in some hours.Thanks for your help

#13 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 19 October 2006 - 03:14 PM

Give this a try:

<?php 
include('header1.tpl');

$email = $_POST['email'];
    
// open connection to MySQL server
$connection = mysql_pconnect('localhost', '', '') or die ('Unable to connect!');

// select database for use
mysql_select_db('db') or die ('Unable to select database!');

// Run the query and get the result
$query = "INSERT INTO newsletter(email) VALUES ('$email')";
$result = mysql_query($query) or die ("Unable to run the following query:<br>\n$query<br>\n" . mysql_error());
if ($result){
   echo'<br/>';
   echo'<center>';
   echo 'Thank you for signing up for smeresources newsletter';
   echo'</center>';
}
else {
   echo'<br/>';
   echo'<center>';
   echo 'You cant be Registered,try again later';
   echo'</center>';
}
?>

Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#14 xux

xux
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 20 October 2006 - 01:26 PM

Hi,
    I tried out your suggestion but it wasnt populating the database out when I run select* query on phpMyAdmin is was outputing the id with out any email.Thanks
My Regards


#15 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 20 October 2006 - 01:40 PM

In that case it's your form...

Can you export the table structure from phpMyAdmin by using the export tab, and then post it here along with the code and I'll get on the case.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#16 xux

xux
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 25 October 2006 - 08:25 AM

hi huggie,
              Hi,I try exporting the schema of the table but I dont know which format I should use to export it.One interesting thing happened,I tried to use insert statement to move data into the database but it was complaining of an error but when it was very obvious that it is error free.So I will really appreciate it if you can tell me how I should set the table or which format I should export the table schema so I can move it down here ?thanks

#17 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 25 October 2006 - 10:21 AM

OK, on the left hand side where you select the tables to export, select type is sql.  On the right hand side, where you have the sql options, select export type INSERT from the drop down.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#18 xux

xux
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 27 October 2006 - 01:46 PM

Hi,
  Huggie I finally got it done.the codes are below
-- phpMyAdmin SQL Dump
-- version 2.9.0.2
-- http://www.phpmyadmin.net
-- 
-- Host: localhost
-- Generation Time: Oct 27, 2006 at 07:41 AM
-- Server version: 4.1.21
-- PHP Version: 4.4.2
-- 
-- Database: `sme_sme2`
-- 

-- --------------------------------------------------------

-- 
-- Table structure for table `newsletter`
-- 

CREATE TABLE `newsletter` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `email` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

-- 
-- Dumping data for table `newsletter`
-- 

INSERT INTO `newsletter` VALUES (1, '');
INSERT INTO `newsletter` VALUES (2, '');
INSERT INTO `newsletter` VALUES (3, '');

I really appreciate your efforts.thanks and cheers
My Regards
XUX

#19 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 27 October 2006 - 02:08 PM

ok, table created, now can you post the code you're using, all php and (if separate) your form.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#20 xux

xux
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 27 October 2006 - 07:01 PM

Hi,
    Thanks,here are the codes.first for the php file that will be triggered when the form is submitted i.e signup.php

<?php 
include('header1.tpl');
    $email=$HTTP_POST_VARS['email'];
    $email=trim($email);
    $email=addslashes($email);
 // open connection to MySQL server
$connection = mysql_pconnect('localhost', '', '') 
or die ('Unable to connect!');
// select database for use
mysql_select_db('sme_sme2') or die ('Unable to select database!');
 $query="INSERT INTO newsletter(id,email) VALUES ('','$email')";
$result=mysql_query($query);
 if ($result)
{
echo'<br/>';
echo'<center>';
echo 'Thank you for signing up for smeresources newsletter';
echo $email;
echo'</center>';
}
else
{
echo'<br/>';
echo'<center>';
echo 'You cant be Registered,try again later';
echo'</center>';

}

?>
</div>

<?php
include('footer.tpl');
?>
then here are the codes for the form
<div class="newsletter">
				<img src="images/spacer.gif" width="1" height="30" /><br />
				<form method="post" action="signup.php" style="display:inline;">
					Enter Email:<br />
					<input type="text" size="20"  name="email" style="font-size: 10px;"  /><br />
					<img src="images/spacer.gif" width="1" height="9" /><br/>
					<a href="signup.php?a=subscribe"><img src="images/buttons_r1_c1.gif" width="74" height="18" border="0" alt="Subscribe to Newsletter" /></a>
					<img src="images/spacer.gif" width="6" height="1" />
					<a href="#"><img src="images/buttons_r1_c3.gif" width="72" height="18" border="0" alt="Unsubscribe from Newsletter" /></a><br />
					<img src="images/spacer.gif" width="1" height="5" />
				</form>
			</div>	
hope to hear from you soon.
My Regards
XUX




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users