Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

INSERT and UPDATE

Status
Not open for further replies.

M2E

IS-IT--Management
Aug 1, 2005
28
GB
I am creating a questionnaire on my site. When it is filled out, it checks to see if the person filling out the form is already in my database. If they're email address is already there, I perform an update statement, and update that email with the extra questionnaire information.

If the email address is not I simply do an insert statement with all the new information.

That's the theory - however in practice when I submit the questionnaire it says that I have submitted, however the database remains empty - can't seem to work it out. My Code is below.

Also it seems to always be running the UPDATE statement - as this was the message that appeared

Please someone tell me where I have gone wrong!!

----

Code:
 <?php

#ensure all fields have entries
if ($email and $PostCode and $Gender and $OwnHome and $OwnCar and $Children and $HomeInternet and $ClubSociety and $BarsClubs and $Cinema and $Theatre and $GoodNightOut and $M2EThoughts and $M2EFavEvent and $M2ENewEvents and $M2EPricing and $M2EImprove)
{
#connect to mysql
$conn=@mysql_connect("localhost", "rgevans_adminuse", "excellent") or die ("Err:Conn");

#select the specified database
$rs = @mysql_select_db("rgevans_m2e", $conn) or die ("Err:Conn");

#create the 1st query - check if this email address is in the database already
$sql="SELECT * FROM m2e_db WHERE email = \"".$email."\"";

#execute 1st query
$rs=mysql_query($sql,$conn);

#If NO matching record is found, INSERT new entry into table
if (mysql_num_rows($rs)) {
$sql="INSERT INTO m2e_db(email, PostCode, Gender, OwnHome, OwnCar, Children, HomeInternet, ClubSociety, BarsClubs, Cinema, Theatre, GoodNightOut, M2EThoughts, M2EFavEvent, M2ENewEvents, M2EPricing, M2EImprove) VALUES ( \"$email\", \"$PostCode\", \"$Gender\", \"$OwnHome\", \"$OwnCar\", \"$Children\", \"$HomeInternet\", \"$ClubSociety\", \"$BarsClubs\", \"$Cinema\", \"$Theatre\", \"$GoodNightOut\", \"$M2EThoughts\", \"$M2EFavEvent\", \"$M2ENewEvents\", \"$M2EPricing\", \"$M2EImprove\" )";
echo "INSERT: Thankyou for submitting this questionnaire";
exit;
}
else {
#if the email address is present then update that record with the questionnaire values
$sql="UPDATE m2e_db SET Gender = '$Gender', PostCode = '$PostCode', OwnHome = '$OwnHome', OwnCar = '$OwnCar', HomeInternet = '$HomeInternet', ClubSociety = '$ClubSociety', BarsClubs = '$BarsClubs', Cinema = '$Cinema', Theatre = '$Theatre', GoodNightOut = 'GoodNightOut', M2EThoughts = '$M2EThoughts', M2EFavEvent = '$M2EFavEvent', M2ENewEvents = '$M2ENewEvents', M2EPricing = '$M2EPricing', M2EImprove = '$M2EImprove' WHERE email = '$email'";

#execute the query
$rs=mysql_query($sql,$conn);
}
#confirm the added record details
if($rs) {echo ("UPDATE:Thankyou for submitting this questionnaire"); }
}
?>

----Ment2Excel (M2E)----
  --LEARN BY EXAMPLE--
 
Change this:
$sql="SELECT * FROM m2e_db WHERE email = \"".$email."\"";
to:
$sql="SELECT * FROM m2e_db WHERE email = '".$email."'";


Then, add a line after this:
if (mysql_num_rows($rs)) {
$sql="INSERT INTO m2e_db(email, PostCode, Gender, OwnHome, OwnCar, Children, HomeInternet, ClubSociety, BarsClubs, Cinema, Theatre, GoodNightOut, M2EThoughts, M2EFavEvent, M2ENewEvents, M2EPricing, M2EImprove) VALUES ( \"$email\", \"$PostCode\", \"$Gender\", \"$OwnHome\", \"$OwnCar\", \"$Children\", \"$HomeInternet\", \"$ClubSociety\", \"$BarsClubs\", \"$Cinema\", \"$Theatre\", \"$GoodNightOut\", \"$M2EThoughts\", \"$M2EFavEvent\", \"$M2ENewEvents\", \"$M2EPricing\", \"$M2EImprove\" )";

$rs=mysql_query($sql,$conn);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top