Forum Moderators: coopster

Message Too Old, No Replies

insert row through php in data base

         

rekhad

7:48 pm on Feb 27, 2006 (gmt 0)

10+ Year Member



Can some one help me out in finding what's wrong in the following code?

I see the out put data not inserted?

<html>
<head>
<title> database</title>
</head>
<body>
<pre>
<?php
$connect=mysql_connect("localhost","root","*****");
mysql_select_db("mydatabase",$connect);

$insert_query="INSERT INTO users VALUES ('big','boy',
'big@monkees.com',PASSWORD('small'),NOW())";
if(mysql_query($insert_query,$connect))

print("data inserted");

else
print("data not inserted");

?>
</pre>
</body>
</html>

TommyWeb

8:26 pm on Feb 27, 2006 (gmt 0)

10+ Year Member



Hello, I don't speak English very well so excuse me for the errors.

Try this code :

<?php
$connect=mysql_connect("localhost","root","*****");
mysql_select_db("mydatabase",$connect);

$insert_query="INSERT INTO users VALUES ('big','boy',
'big@monkees.com',PASSWORD('small'),NOW())";
$sql = mysql_query($insert_query) or die("SQL ERROR : ".mysql_error());
if($sql)
print("data inserted");

else
print("data not inserted");

?>

With that, if you've an error in mysql query, you'll see the error

rekhad

8:47 pm on Feb 27, 2006 (gmt 0)

10+ Year Member



Thanks. help me again
I get the following error.

SQL ERROR : Column count doesn't match value count at row 1
I have 5 columns
user_id,first_name,last_name,email,password,registration_date.
the user_id is MEDIUMINT NOT NULL AUTO_INCREMENT,
In my insert query I have tried

"INSERT INTO users VALUES (",'big','boy',
'big@monkees.com',PASSWORD('small'),NOW())";

but that doesn't work
please help...

jatar_k

8:50 pm on Feb 27, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> I have 5 columns

but you're inserting 6

INSERT INTO users VALUES (", 'big', 'boy', 'big@monkees.com', PASSWORD('small'), NOW())

and you say 6

user_id
first_name
last_name
email
password
registration_date

I would look at your table and see what's what

rekhad

9:32 pm on Feb 27, 2006 (gmt 0)

10+ Year Member



Sorry about .I have six columns
When I give SELECT query,It works o.k.I can see my table properly.
But INSERT does not add row to my table

jatar_k

9:33 pm on Feb 27, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



have you checked your table to be absolutely sure

mysql is telling you, with that error, that the number of columns doesn't match

rekhad

9:43 pm on Feb 27, 2006 (gmt 0)

10+ Year Member



Yes I have six columns.The columns have data with 13 rows that is visible with SELECT query.
I dont know how to send you the table so that you can see it?

jatar_k

9:50 pm on Feb 27, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you could paste here the result of this command

SHOW CREATE TABLE users;

rekhad

9:55 pm on Feb 27, 2006 (gmt 0)

10+ Year Member



The out put is here.It is not in a table format and will be hard to read.But you can see the number of columns.

The column headings are
user_id,first name,last name,email,password,registrationdate

user id: first name: last name: email: password registration date:
2 John Lennon john@beatles.com 734d56ec0089bb3a 2006-02-25 17:54:16
3 Paul McCartney paul@beatles.com 2c20438b7ed80185 2006-02-25 17:59:00
4 George Harrison george@authors.com 16b4c38141c119f9 2006-02-25 18:00:32
5 Ringo Starr ringo@beatles.com 49f6f01e5ab37a30 2006-02-26 17:05:10
6 David Jones davey@monkees.com 552151cf55e12624 2006-02-26 17:05:10
7 Peter Tork peter@monkees.com 7b48edff227042d5 2006-02-26 17:28:59
9 Mike Nesmith miky@monkees.com 7473f3886b36fdac 2006-02-26 17:38:50
10 David Sedaris david@authors.com 226ca5b57bc32575 2006-02-26 17:39:40
11 Nick Hornby nick@authors.com 62a21a7067a772ba 2006-02-26 17:40:32
12 Millesia Bank melly@monkees.com 3d91077c5470eef7 2006-02-26 17:41:27
13 Tony Morrrison tony@authors.com 789554fa2632d865 2006-02-26 17:42:16

[edited by: jatar_k at 10:22 pm (utc) on Feb. 27, 2006]
[edit reason] removed specifics [/edit]

rekhad

10:18 pm on Feb 27, 2006 (gmt 0)

10+ Year Member



Hi Jatar,
are you there to help me out?

jatar_k

10:27 pm on Feb 27, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



use a different syntax then

specify the columns these values go into

INSERT INTO users (user_id, first_name, last_name, email, password, registration_date) VALUES (", 'big', 'boy', 'big@monkees.com', PASSWORD('small'), NOW())

there are a couple different syntaxes for INSERT [dev.mysql.com]

TommyWeb

10:32 pm on Feb 27, 2006 (gmt 0)

10+ Year Member



Try this

INSERT INTO users(first_name,last_name,email,password,registration_date) VALUES ('big', 'boy', 'big@monkees.com', PASSWORD('small'), NOW());

Normally, you shouldn't have any errors because I suppose the user_id is in auto-increment and primary key...

rekhad

10:37 pm on Feb 27, 2006 (gmt 0)

10+ Year Member



When I gave

INSERT INTO users (user_id, first_name, last_name, email, password, registration_date) VALUES (", 'big', 'boy', 'big@monkees.com', PASSWORD('small'), NOW())
It gave me the same error

but when I did

INSERT INTO users (user_id, first_name, last_name, email, password, registration_date) VALUES ('14', 'big', 'boy', 'big@monkees.com', PASSWORD('small'), NOW())

row 14th was inserted.

ButI dont want to give a numerical value at user_id because that should come from AUT0_INCREMENT
Any clue?

TommyWeb

10:45 pm on Feb 27, 2006 (gmt 0)

10+ Year Member



user_id isn't in auto_increment?

Try it :

INSERT INTO users(first_name,last_name,email,password,registration_date) VALUES ('big', 'boy', 'big@monkees.com', PASSWORD('small'), NOW());

rekhad

10:54 pm on Feb 27, 2006 (gmt 0)

10+ Year Member



Thanks guys the following command worked.user_id was incremented in the table.I dont have to put in INSERT ..

INSERT INTO users(first_name,last_name,email,password,registration_date) VALUES ('big', 'boy', 'big@monkees.com', PASSWORD('small'), NOW());