html form data into mysql via php

This should be simple. I’ve read 10 different tutorials online of how to do it, but I can’t.
I created a simple html test form and a php file for processing the info. I can connect to MySQL, and I can connect to a database. But when I try to insert my data into a table (which already exists), it just won’t go. The mysql section of the phpadmin tells me there are zero rows in the table.

I think the way to insert the data is something like
[php]$query = “INSERT INTO monkey VALUES (’$username’, ‘$password’)”;
mysql_query($query) or die(‘Error, query failed’);[/php]
where monkey is the name of my table. I know that $username and $password are correctly defined, because I lopped off part of the code and just echoed these back to myself. I did similar things with the mysql connection and database location. But the darn tables are causing me to pull my hair out! I’ve tried using apostrophes and quotes and the little ` thingy in different places and I can’t get anything to work. I’ll post the rest of the code if people want it. Thanks all.

We need all code, because with this information only can suggest.

but i think the problem must be one of this (or some of this):
1 - mysql_connection doesn’t work
2 - Table monkey contains many fields that this can be null.
3 - Table monkey has other fields and username not the first, in this case use :
$query = “INSERT INTO monkey (username,password) VALUES (’$username’, ‘$password’)”;
mysql_query($query);
username and password must be field of table at database.

I hope this works.

This is the form I use for inserting data into my databse

[php]<?php
// Connection to database
$con = mysql_connect(“database”,“user”,“pass”);
if (!$con)
{
die('Could not connect: ’ . mysql_error());
}

mysql_select_db(“database”) or die(mysql_error());

$sql=“INSERT INTO table (column1, column2) //change for your column names
VALUES
(’$_POST[field1]’,’$_POST[field2]’)”; // change for your html form names

if (!mysql_query($sql,$con))
{
die('Error: ’ . mysql_error());
}
echo “Thank You for posting”;

mysql_close($con);

?>[/php]

Hope it helps.

Sam

I can’t get the error, Is this code printing an error?
if it’s not printing error, try to write the insert like this:

$sql=“INSERT INTO table (column1, column2)
VALUES
(’”.$_POST[‘field1’]."’,’".$_POST[‘field2’]."’)";

Thanks for the replies so far.
[php]<?php

$username=$_POST[‘username’];
$password=$_POST[‘password’];

$link = mysql_connect(‘127.0.0.1’,‘root’,’’);
if (!$link) { die('Could not connect to MySQL: ’ . mysql_error()); }

echo “Found MySQL…”;

$db_selected = mysql_select_db(‘test’);
if (!$db_selected) {
die ('Can’t use db : ’ . mysql_error());
} else
echo “Found database…”;

$sql=“INSERT INTO monkey (username, password)
VALUES
(’$username’,’$password’)”;
$sql
or die(‘Error, query failed’);
echo “success.”;[/php]
And all three of my echoes happen!!! But then I go to the EasyPHPMyAdmin, and the MySQL section, and if I look at that table, it’s empty!!!

Here’s MySQL info…
database: test
table: monkey
(there are two columns)
#:1
Column: username
Type: varchar(20)
Collation: latin1_swedish_ci {it did this automatically, I left it blank}
everything else is null, none or blank.

#:2
Column: password
Type: varchar(20
Collation: latin1_swedish_ci {it did this automatically, I left it blank}
everything else is null, none or blank.

p.s. I tried switching the collation of both columns to utf8_general_ci and it still won’t insert.

Sponsor our Newsletter | Privacy Policy | Terms of Service