Jump to content


Photo

Won't Post To SQL


  • Please log in to reply
14 replies to this topic

#1 bxmachines77

bxmachines77
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 10 June 2006 - 05:06 AM

Hello,
I'm very new at PHP and SQL with enough knowledge to first build a table and try to fill it with the code below. The problem is even though it says your information was posted to the server, nothing is there. I don't know if I need to query it but it's suppose to post into the table which is in the script. I appreciate any help and thanks in advance.

<?php
$textbox1name = $_POST['textbox1name'];
$textbox2date = $_POST['textbox2date'];
$check1male = $_POST['check1male'];
$check2female = $_POST['check2female'];
$text3phone = $_POST['text3phone'];
$textbox4county = $_POST['textbox4county'];
$check3personwithimpairment = $_POST['check3personwithimpairment'];
$check4family_member_significant_other = $_POST['check4family_member_significant_other'];
$textbox5other_specify = $_POST['textbox5other_specify'];
$textbox6business_agency = $_POST['textbox6business_agency'];
$chech5advocacy_legal_services = $_POST['chech5advocacy_legal_services'];
$check9_architectual_barrier_services = $_POST['check9_architectual_barrier_services'];
$check7_assistive_devices_equipment_services = $_POST['check7_assistive_devices_equipment_services'];
$check8_benefits_advisment = $_POST['check8_benefits_advisment'];
$check10_business_industry_agency_services = $_POST['check10_business_industry_agency_services'];
$check11_childrens_services = $_POST['check11_childrens_services'];
$check12_communication_services = $_POST['check12_communication_services'];
$check13_family_services = $_POST['check13_family_services'];
$check14_housing_home_modification_other_shelter_services = $_POST['check14_housing_home_modification_other_shelter_services'];
$chech15_il_skills_traing_life_skills_training = $_POST['chech15_il_skills_traing_life_skills_training'];
$check16_inforamtion_referral = $_POST['check16_inforamtion_referral'];
$check17_mobility_training = $_POST['check17_mobility_training'];
$check18_peer_counseling_services= $_POST['check18_peer_counseling_services'];
$check19_preventative_services = $_POST['check19_preventative_services'];
$check20_prostheses_orthics_other_appliances = $_POST['check20_prostheses_orthics_other_appliances'];
$check21_recreational_services = $_POST['check21_recreational_services'];
$check22_rehabilitation_technology_services = $_POST['check22_rehabilitation_technology_services'];
$check24_transportation_services = $_POST['check24_transportation_services'];
$check25_youth_services = $_POST['check25_youth_services'];
$check26_vocational_services = $_POST['check26_vocational_services'];
$check27_voter_reistration = $_POST['check27_voter_reistration'];
$textbox7_staff_person_date1 = $_POST['textbox7_staff_person_date1'];
$textbox8_staff_person_date2 = $_POST['textbox8_staff_person_date2'];
mysql_connect("localhost.org.com", "61606", "password") or die(mysql_error());
mysql_select_db("61606") or die(mysql_error());
mysql_query("INSERT INTO `servicerecord` VALUES ('$textbox1name', '$textbox2date', '$check1male', '$check2female', '$text3phone', '$textbox4county', '$check3personwithimpairment', '$check4family_member_significant_other', '$textbox5other_specify', '$textbox6business_agency', '$chech5advocacy_legal_services', '$check9_architectual_barrier_services', '$check10_business_industry_agency_services', '$check11_childrens_services', '$check12_communication_services', '$check13_family_services', '$check14_housing_home_modification_other_shelter_services', '$chech15_il_skills_traing_life_skills_training', '$check16_inforamtion_referral', '$check17_mobility_training', '$check18_peer_counseling_services', '$check19_preventative_services', '$check20_prostheses_orthics_other_appliances', '$check21_recreational_services', '$check22_rehabilitation_technology_services', '$check24_transportation_services', '$check25_youth_services', '$check26_vocational_services', '$check27_voter_reistration', '$textbox7_staff_person_date1', '$textbox8_staff_person_date2')");
Print "Your information has been successfully added to the database.";
?>

#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 10 June 2006 - 06:46 AM

your query needs to be like this:

[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']INSERT[/span] INTO tablename (column1, column2, column3) VALUES ('$value1','$value2','$value3')
[!--sql2--][/div][!--sql3--]
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 joquius

joquius
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 10 June 2006 - 04:43 PM

This script is a bit harsh. Too much code
I would advise using the following method:

$sql = "";
foreach ($_POST as $field => $value)
{
if ($value != "")
{
$append = ($sql == "") ? "" : ", ";
$sql .= "$append'$value'";
}
}
$query = "INSERT INTO `db` VALUES ($sql)";

and if that does work out add another $sql for the field names according to the form input names, and match them with the db field names.
z..z..z..z..z..z..z..z..

#4 bxmachines77

bxmachines77
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 10 June 2006 - 09:02 PM

[!--quoteo(post=382261:date=Jun 10 2006, 12:43 PM:name=joquius)--][div class=\'quotetop\']QUOTE(joquius @ Jun 10 2006, 12:43 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
This script is a bit harsh. Too much code
I would advise using the following method:

$sql = "";
foreach ($_POST as $field => $value)
{
if ($value != "")
{
$append = ($sql == "") ? "" : ", ";
$sql .= "$append'$value'";
}
}
$query = "INSERT INTO `db` VALUES ($sql)";

and if that does work out add another $sql for the field names according to the form input names, and match them with the db field names.
[/quote]


Could you please tell me what I need to insert into this and where? I'm really new at this. The colum didn't work. I appreciate your help. Thanks.

#5 bxmachines77

bxmachines77
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 11 June 2006 - 04:44 AM

Could someone please explain what to put into and were to put it into the above code so it works? I'm desperate to get this working for a non profit agency. Thanks in advance for your input.


#6 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 11 June 2006 - 05:56 AM

you pretty much throw away your old code and use that instead. except, use the correct table name in your sql query (servicerecord)
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#7 bxmachines77

bxmachines77
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 11 June 2006 - 08:16 PM

[!--quoteo(post=382421:date=Jun 11 2006, 01:56 AM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ Jun 11 2006, 01:56 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
you pretty much throw away your old code and use that instead. except, use the correct table name in your sql query (servicerecord)
[/quote]
Could some one teel me what to insert into the new code and were. Please be specific, I'm really new at this. Thanks in advance.

#8 WendyLady

WendyLady
  • Members
  • PipPipPip
  • Advanced Member
  • 38 posts

Posted 11 June 2006 - 10:56 PM

bxmachines,

I have been as much a noobie as you, so I will make this simple.

1) All of the variable declarations -- $variable = $_POST['variable']; -- you can either leave all of these in (which is very verbose), or you can delete all of them & insert joquius's snippet of code instead. It really doesn't matter. To first get it working, you might leave this part as-is, then try his more efficient code later.

2) After you declare all your variables, your INSERT statement is incorrect. It is going to be a very, very LONG statement. Following Violet's suggestion:

$query = "INSERT INTO servicerecord (textbox1name, textbox2name, check1male, check1female ...) VALUES ('$textbox1name', '$textbox2name', '$check1male', '$check1female', ...)";

EVERY table cell you want to insert data into must be listed in the first set of parentheses(), THEN its matching $variable must be listed in the second set of parentheses(), IN THE SAME ORDER. Also, be sure to follow my (or Violet's) lead as to which things have single & double quotes. Obviously, do not leave the "..." in -- this is just to demonstrate that there will be more entries after it.

3) It is not a good idea to simply spit out "This worked" at the end without checking it somehow. At least until you have debugged your script, add a simple IF statement to check it:

if (mysql_affected_rows == 1) {
   echo "Succeeded";
   }else{
   echo "Failed: ".mysql_affected_rows()." affected rows with Query: ".$query."<br />Error:".mysql_error();
   }

The above will help you figure out what is going on by giving you the returned error messages (if any). It is also a good idea to leave these statements in, but change them to something like "An Error occurred: please contact the webmaster" so that the poor non-profit people aren't thinking things always worked if they DIDN'T.

Wendy

Can I make a suggestion?

Instead of having checkboxes for Male & Female, instead make these radio buttons. Give them the same name but values of M or F, so that no one can check BOTH of them!

<input type="radio" name="gender" value="M"> Male
<input type="radio" name="gender" value="F"> Female

Then you can simplify to ONE table in your database called "Gender" and assign in the value of $_POST['gender'].

Just an idea --

Wendy

--Always assume the user will screw it up somehow.

#9 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 12 June 2006 - 12:28 AM

Try this ok.
<?php

$textbox1name = $_POST['textbox1name'];
$textbox2date = $_POST['textbox2date'];
$check1male = $_POST['check1male'];
$check2female = $_POST['check2female'];
$text3phone = $_POST['text3phone'];
$textbox4county = $_POST['textbox4county'];
$check3personwithimpairment = $_POST['check3personwithimpairment'];
$check4family_member_significant_other = $_POST['check4family_member_significant_other'];
$textbox5other_specify = $_POST['textbox5other_specify'];
$textbox6business_agency = $_POST['textbox6business_agency'];
$chech5advocacy_legal_services = $_POST['chech5advocacy_legal_services'];
$check9_architectual_barrier_services = $_POST['check9_architectual_barrier_services'];
$check7_assistive_devices_equipment_services = $_POST['check7_assistive_devices_equipment_services'];
$check8_benefits_advisment = $_POST['check8_benefits_advisment'];
$check10_business_industry_agency_services = $_POST['check10_business_industry_agency_services'];
$check11_childrens_services = $_POST['check11_childrens_services'];
$check12_communication_services = $_POST['check12_communication_services'];
$check13_family_services = $_POST['check13_family_services'];
$check14_housing_home_modification_other_shelter_services = $_POST['check14_housing_home_modification_other_shelter_services'];
$chech15_il_skills_traing_life_skills_training = $_POST['chech15_il_skills_traing_life_skills_training'];
$check16_inforamtion_referral = $_POST['check16_inforamtion_referral'];
$check17_mobility_training = $_POST['check17_mobility_training'];
$check18_peer_counseling_services= $_POST['check18_peer_counseling_services'];
$check19_preventative_services = $_POST['check19_preventative_services'];
$check20_prostheses_orthics_other_appliances = $_POST['check20_prostheses_orthics_other_appliances'];
$check21_recreational_services = $_POST['check21_recreational_services'];
$check22_rehabilitation_technology_services = $_POST['check22_rehabilitation_technology_services'];
$check24_transportation_services = $_POST['check24_transportation_services'];
$check25_youth_services = $_POST['check25_youth_services'];
$check26_vocational_services = $_POST['check26_vocational_services'];
$check27_voter_reistration = $_POST['check27_voter_reistration'];
$textbox7_staff_person_date1 = $_POST['textbox7_staff_person_date1'];
$textbox8_staff_person_date2 = $_POST['textbox8_staff_person_date2'];

mysql_connect("localhost.org.com", "61606", "password") or die(mysql_error()); 
mysql_select_db("61606") or die(mysql_error()); 

$query="INSERT INTO servicerecord VALUES
('$textbox1name', '$textbox2date', '$check1male', '$check2female', '$text3phone', '$textbox4county', '$check3personwithimpairment', '$check4family_member_significant_other', '$textbox5other_specify', '$textbox6business_agency', '$chech5advocacy_legal_services', '$check9_architectual_barrier_services', '$check10_business_industry_agency_services', '$check11_childrens_services', '$check12_communication_services', '$check13_family_services', '$check14_housing_home_modification_other_shelter_services', '$chech15_il_skills_traing_life_skills_training', '$check16_inforamtion_referral', '$check17_mobility_training', '$check18_peer_counseling_services', '$check19_preventative_services', '$check20_prostheses_orthics_other_appliances', '$check21_recreational_services', '$check22_rehabilitation_technology_services', '$check24_transportation_services', '$check25_youth_services', '$check26_vocational_services', '$check27_voter_reistration', '$textbox7_staff_person_date1', '$textbox8_staff_person_date2')"; 

$check="select * from servicerecord where id='$id' ";

$check_result=mysql_query($check);

while($record=mysql_fetch_assoc($check_result) {

if($record["id"]==1) {

echo " Thank You Data Entered Into Database";
exit;


}else{

echo"Sorry There Was A Problam Please Try Agin";
exit;
}
?>


ps. program how you want ok good luck mate.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#10 bxmachines77

bxmachines77
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 12 June 2006 - 03:50 AM

Hello all,
Thanks so much for your kind help. It was at least giving me error messages with the added error code but now I get the white screen of death with my code and the one above. The error code was that the values couldn't have '', so I tried nothing and; [''],[],[];['']; and all gave a sytax error but nothing but a white screen now. I'm doing this for a non profit place so any more ideas would be great. Thanks in advance.

#11 bxmachines77

bxmachines77
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 12 June 2006 - 05:20 AM

Hello,
I figured out the syntax error and it now posts the check boxes to the server but not any of the text. I appreciate your help if anyone has any ideas. The server side looks like it's setup right and it wants to give an error message after posting but it's blank after the word error. Thanks again,

#12 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 12 June 2006 - 05:26 AM

are you sure your columns are the correct data type for what you are trying to insert into them? that is, you cannot insert text into a column that is a number type, like int.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#13 bxmachines77

bxmachines77
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 12 June 2006 - 10:12 PM

[!--quoteo(post=382762:date=Jun 12 2006, 01:26 AM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ Jun 12 2006, 01:26 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
are you sure your columns are the correct data type for what you are trying to insert into them? that is, you cannot insert text into a column that is a number type, like int.
[/quote]


Hello,
Yes, I'm certain that part is right except I didn't know what to use for check marks and have it as bool. I don't know if that's right. It doesn't post any of the info for some reason.

#14 WendyLady

WendyLady
  • Members
  • PipPipPip
  • Advanced Member
  • 38 posts

Posted 12 June 2006 - 11:40 PM

For checkmarks, it will submit the value=" " in your <input> area.

If you have <input type="checkbox" value="male" name="gender" /> then it will try to submit "male" if that box is checked. Those should be char or varchar. Look up the definitions of what each type will hold.

Try taking out your "insert" query for a moment & just ask it to echo back to you what was submitted in the form. When you can debug & get it to echo back properly, take out the echo & put your insert command back in.

Wendy

#15 bxmachines77

bxmachines77
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 13 June 2006 - 07:07 AM

Thanks to everyone it's now up and running fine. I couldn't have done it without your kind help. It's for a good cause. Best regards,
Bill




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users