0

I'm learning php and I'm having trouble inserting into a MySQL database (for testing purposes). I run the code below and I'm not faced with an error, however the records are not visible in my tables; It's like I anything never happened in the db. Please help, code below:

<?php

//Variable - Declaring the connection
$con=mysql_connect('localhost','joe','12345678') 
or die(mysql_error());

//selecting the database
mysql_select_db('users',$con);

//Variable - Declare SQL statement to insert
$sql="INSERT INTO users (name,lastname) VALUES('carl','')";

mysql_query($sql,$con);

?>

The database details are:

server : localhost
user : joe (full permissions)
password : 12345678
database name : test
table name : users
field names : name and lastname\

Help is highly appreciated.

Thanks in advance.

Regards,

Joseph

4
  • 1
    Just some clarification: PHPMyAdmin is the front-end for the MySQL database you have. Commented Sep 14, 2013 at 21:44
  • 1
    You're not looking for any errors, so it's not surprising that you don't see any. Start by checking the return value of mysql_query(), and if it's false look at mysql_error for the error message. Commented Sep 14, 2013 at 21:46
  • Do a SELECT * FROM users; Commented Sep 14, 2013 at 21:46
  • Read the manual and take note of the huge red box: php.net/manual/en/function.mysql-select-db.php Commented Sep 14, 2013 at 21:50

3 Answers 3

4

You are selecting db user but have DB test...

Sign up to request clarification or add additional context in comments.

2 Comments

Hey thanks for the instant reply. I've changed that since it was a silly mistake and it worked.
@user2612009: You should accept this (or any other) answer if it answers your question (when you are able to do so, of course).
4

I think this might happen to be your immediate issue:

mysql_select_db('users',$con);

You select the DB named users, whereas you stated the DB name to be test. Try this instead

mysql_select_db('test',$con);

However, based on your question, I think it would be very useful to read up on the basic components you are dealing with: the DBMS, the objects used in MySQL (database, table, user, etc...), the front end...

Comments

0

By the looks of it this line;

$sql="INSERT INTO users (name,lastname) VALUES('carl','')";

Should be changed to this;

$sql="INSERT INTO users (`name`, `lastname`) VALUES('carl','')";

To clarify the " ` " were missing from the field names of the table.

Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.