Forum Moderators: coopster

Message Too Old, No Replies

Insert into Databases

Trying to insert information from a form into a database.

         

lori

4:00 pm on Jul 21, 2006 (gmt 0)

10+ Year Member




<?
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO tbl_cabins VALUES ('', '$name', '$type', '$description', '$location', '$owner', '$bed_number', '$bed1_type', '$bed2_type', '$bed3_type', '$bed4_type', '$bed5_type', '$bed1_location', '$bed2_location', '$bed3_location', '$bed4_location', '$bed5_location', '$bath_number', '$bath1_type', '$bath2_type', '$bath3_type', '$bath4_type', '$bath5_type', '$bath1_location', '$bath2_location', '$bath3_location', '$bath4_location', '$bath5_location', '$sleep_number', '$sleeper1_type', '$sleeper2_type', '$sleeper3_type', '$sleeper1_location', '$sleeper2_location', '$sleeper3_location', '$hottub', '$hottub_desc', '$jacuzzi', '$jacuzzi_desc', '$sauna', '$sauna_desc', '$pool', '$pool_desc', '$pooltable', '$pooltable_desc', '$airhockey', '$airhockey_desc', '$foosball', '$foosball_desc', '$view', '$view_desc', '$privacy', '$internet', '$internet_desc', '$slots', '$othergames', '$handicap', '$pets', '$smoking', '$pricing', '$cleaningfee', '$taxrate', '$other', '$heartlandrentals', '$heartlandcabins', '$gatlinburgrentals_realty', '$tennesseeromance', '$agatlinburgromance', '$pigeonforgeromance', '$countrybearcabins', '$amazinghillbillyhideaways', '$pic1', '$pic2', '$pic3', '$pic4', '$pic5', '$pic6', '$pic7', '$pic8', '$pic9', '$pic10', '$availabilty', '$tv1', '$tv2', '$tv3', '$tv4', '$tv5', '$tv6', '$tv7', '$tv1_location', '$tv2_location', '$tv3_location', '$tv4_location', '$tv5_location', '$tv6_location', '$tv7_location', '$dvd1', '$dvd1', '$dvd2', '$dvd3', '$dvd4', '$dvd5', '$dvd6', '$dvd7', '$vcr1', '$vcr2', '$vcr3', '$vcr4', '$vcr5', '$vcr6', '$vcr7', '$tv1_desc', '$tv1_desc', '$tv2_desc', '$tv3_desc', '$tv4_desc', '$tv5_desc', '$tv6_desc', '$tv7_desc', '$sleeps_number', '$christmas')";
mysql_query($query);
echo "record added!";
mysql_close();
?>

It comes up that the record was added but it's not in the database! Help :(

eelixduppy

4:07 pm on Jul 21, 2006 (gmt 0)



Try adding this line:

mysql_query($query) or die(mysql_error());

Also, do you have register globals enabled? If not, the variable should be something like this:


$query = "INSERT INTO tbl_cabins VALUES ('', '$_POST[name]', '$_POST[type]', '$_POST[description]', '$_POST[location]'.....etc

Good luck!

lori

4:19 pm on Jul 21, 2006 (gmt 0)

10+ Year Member



It's still not adding it into the database

<?
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "INSERT INTO tbl_cabins VALUES ('', '$_POST[name]', '$_POST[type]', '$_POST[description]', '$_POST[location]', '$_POST[owner]', '$_POST[bed_number]', '$_POST[bed1_type]', '$_POST[bed2_type]', '$_POST[bed3_type]', '$_POST[bed4_type]', '$_POST[bed5_type]', '$_POST[bed1_location]', '$_POST[bed2_location]', '$_POST[bed3_location]', '$_POST[bed4_location]', '$_POST[bed5_location]', '$_POST[bath_number]', '$_POST[bath1_type]', '$_POST[bath2_type]', '$_POST[bath3_type]', '$_POST[bath4_type]', '$_POST[bath5_type]', '$_POST[bath1_location]', '$_POST[bath2_location]', '$_POST[bath3_location]', '$_POST[bath4_location]', '$_POST[bath5_location]', '$_POST[sleep_number]', '$_POST[sleeper1_type]', '$_POST[sleeper2_type]', '$_POST[sleeper3_type]', '$_POST[sleeper1_location]', '$_POST[sleeper2_location]', '$_POST[sleeper3_location]', '$_POST[hottub]', '$_POST[hottub_desc]', '$_POST[jacuzzi]', '$_POST[jacuzzi_desc]', '$_POST[sauna]', '$_POST[sauna_desc]', '$_POST[pool]', '$_POST[pool_desc]', '$_POST[pooltable]', '$_POST[pooltable_desc]', '$_POST[airhockey]', '$_POST[airhockey_desc]', '$_POST[foosball]', '$_POST[foosball_desc]', '$_POST[view]', '$_POST[view_desc]', '$_POST[privacy]', '$_POST[internet]', '$_POST[internet_desc]', '$_POST[slots]', '$_POST[othergames]', '$_POST[handicap]', '$_POST[pets]', '$_POST[smoking]', '$_POST[pricing]', '$_POST[cleaningfee]', '$_POST[taxrate]', '$_POST[other]', '$_POST[heartlandrentals]', '$_POST[heartlandcabins]', '$_POST[gatlinburgrentals_realty]', '$_POST[tennesseeromance]', '$_POST[agatlinburgromance]', '$_POST[pigeonforgeromance]', '$_POST[countrybearcabins]', '$_POST[amazinghillbillyhideaways]', '$_POST[pic1]', '$_POST[pic2]', '$_POST[pic3]', '$_POST[pic4]', '$_POST[pic5]', '$_POST[pic6]', '$_POST[pic7]', '$_POST[pic8]', '$_POST[pic9]', '$_POST[pic10]', '$_POST[availabilty]', '$_POST[tv1]', '$_POST[tv2]', '$_POST[tv3]', '$_POST[tv4]', '$_POST[tv5]', '$_POST[tv6]', '$_POST[tv7]', '$_POST[tv1_location]', '$_POST[tv2_location]', '$_POST[tv3_location]', '$_POST[tv4_location]', '$_POST[tv5_location]', '$_POST[tv6_location]', '$_POST[tv7_location]', '$_POST[dvd1]', '$_POST[dvd1]', '$_POST[dvd2]', '$_POST[dvd3]', '$_POST[dvd4]', '$_POST[dvd5]', '$_POST[dvd6]', '$_POST[dvd7]', '$_POST[vcr1]', '$_POST[vcr2]', '$_POST[vcr3]', '$_POST[vcr4]', '$_POST[vcr5]', '$_POST[vcr6]', '$_POST[vcr7]', '$_POST[tv1_desc]', '$_POST[tv1_desc]', '$_POST[tv2_desc]', '$_POST[tv3_desc]', '$_POST[tv4_desc]', '$_POST[tv5_desc]', '$_POST[tv6_desc]', '$_POST[tv7_desc]', '$_POST[sleeps_number]', '$_POST[christmas]')";
mysql_query($query);
echo "record added!";
mysql_close();
?>

Where should I add that additional line of code? It says the record is added but then i go to the database and there is NOTHING!

jatar_k

4:31 pm on Jul 21, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld lori,

you would change your line that says

mysql_query($query);

to

mysql_query($query) or die(mysql_error());

what is most likely happening is you are getting an error from mysql but you are not handling it. The code eelixduppy posted will get the error from mysql and kill the script.

You can post the result, error or not, here and then that will help diagnose the problem.

lori

4:36 pm on Jul 21, 2006 (gmt 0)

10+ Year Member



Yeah, that's what I figured after I posted it. So I did what you just said and I get:

Column count doesn't match value count at row 1

:S I'm going to make sure I have all the fields first. I have the '' for my auto increment though.

eelixduppy

4:46 pm on Jul 21, 2006 (gmt 0)



Deleted comment...I don't know what I was reading. I think I'm starting to lose it.

[edited by: eelixduppy at 5:00 pm (utc) on July 21, 2006]

lori

4:49 pm on Jul 21, 2006 (gmt 0)

10+ Year Member



hehhe, love when i make dumb mistakes. :( i had tv1_desc and dvd1 in there twice! IT WORKS! Thanks for the help, the other stuff was issues too. :D Still learning, slowly but surely!

jatar_k

4:56 pm on Jul 21, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



nice work

coopster

4:37 pm on Jul 22, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Sidebar coming, but very important ...
$query = "INSERT INTO tbl_cabins VALUES ('', '$_POST[name]', ...

Never inject $_POST variables or any other user-supplied input directly into SQL statements. Very insecure. At the very least you should be escaping the string values. For example, with MySQL you would use the mysql_real_escape_string() [php.net] function:
$name = mysql_real_escape_string($_POST[name]); 
$query = "INSERT INTO tbl_cabins VALUES ('', '$name', ...

User-supplied data should be edited before inserting into your database. If possible, make sure it contains that which you believe it should contain. But always, always escape the data prior to insertion!

jatar_k

6:30 pm on Jul 22, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



good catch coop