INSERT INTO not working (PHP+MySQL)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MarkAshley
Forum Commoner
Posts: 34
Joined: Fri Nov 18, 2005 1:36 am

INSERT INTO not working (PHP+MySQL)

Post by MarkAshley »

twigletmac | Please use

Code: Select all

tags when posting PHP code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Hi all

I'm just starting to learn PHP and have used a tutorial to help me come up with the following code:

Code: Select all

<?php
$username="mark";
$password="****";
$database="gamecal";
mysql_connect(127.0.0.1,$username,$password) or die( "Unable to connect");
@mysql_select_db($database) or die( "Unable to select database");
$query = "INSERT INTO games VALUES ('abc','123')";
mysql_query($query);
mysql_close;
?>
When I open the page, I get a blank page (to be expected) but the row is not inserted in to the table. To check the script is actually running, I stopped the MySQL server and refreshed the page, and got the result of the die() function ("Unable to connect" was printed on the screen).

I have checked the syntax of the commands and it all seems ok. I have also checked the names of the database, the table, and the user's login credentials.

Any help would be appreciated.

Many thanks
Mark
joecrack
Forum Commoner
Posts: 99
Joined: Mon Oct 31, 2005 9:17 pm

Post by joecrack »

first always write

Code: Select all

error_reporting(E_ALL);
at the beginning of the php script - then php tells you what the problem is!!!
do the insert like this:

Code: Select all

$query = "INSERT INTO games (columname1,columname2) VALUES ('abc','123')";
MarkAshley
Forum Commoner
Posts: 34
Joined: Fri Nov 18, 2005 1:36 am

Post by MarkAshley »

Thanks Joe. I'll try that when I get home tonight. If I use error_reporting(E_ALL), do I need the die() statements? Or will PHP automatically display the errors at these points for me?

Also, should the field names be in quotes? (single or double?)

Thanks
Mark
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

MarkAshley wrote:If I use error_reporting(E_ALL), do I need the die() statements? Or will PHP automatically display the errors at these points for me?
No, the error_reporting(E_ALL) function call will just ensure that you see all of PHP's errors - it will not capture your MySQL errors so keep the or die()s. But to make your MySQL errors more useful you will need the mysql_error() function and you would add this to the or die() statements:

Code: Select all

<?php
$username = 'mark';
$password = '****';
$database = 'gamecal';

// it may be the 127.0.0.1 in this function causing the problem, try
// changing it to 'localhost' (in quotes) and see if it makes any
// difference
@mysql_connect(127.0.0.1, $username,$password)
	or die('Unable to connect: '.mysql_error());

@mysql_select_db($database)
	or die('Unable to select database: '.mysql_error());

$query = "INSERT INTO games VALUES ('abc','123')";

mysql_query($query)
	or die('Query (<b>'.$query.'</b>) Failed: '.mysql_error());

mysql_close;
?>
MarkAshley wrote:Also, should the field names be in quotes? (single or double?)
Neither, but you can use backticks (`) around them.

Mac
MarkAshley
Forum Commoner
Posts: 34
Joined: Fri Nov 18, 2005 1:36 am

Post by MarkAshley »

Thanks everyone. I tried the code suggested but it still didn't work I then tried changing 127.0.0.1 to localhost, and got the following error:

Client does not support authentication protocol requested by server; consider upgrading MySQL client

I added "echo mysql_get_client_info();" to the beginning of the php script and the version was reported as 3.23.49. When I installed MySQL I downloaded MySQL Standard 4.1.15. I'm quessing I need to upgraded PHP on my iBook, but I can't find a version that matches the verison of MySQL. Anybody know what version of PHP will match my version of MySQL?
MarkAshley
Forum Commoner
Posts: 34
Joined: Fri Nov 18, 2005 1:36 am

Post by MarkAshley »

I just downloaded the version of PHP from here and everything is working now :D

Thanks again guys!
User avatar
khaki_monster
Forum Commoner
Posts: 73
Joined: Tue Oct 11, 2005 12:36 am
Location: Philippines
Contact:

Post by khaki_monster »

Sami | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


hi,

im also new to PHP and MySQL, but i dont received any problem such as yours. could you atlist show us the changes you made after the sugestions from other user? so that we can follow.

[color=orange]// try again with this code[/color]
[color=red]<?php[/color]
   $host [color=blue]=[/color] [color=brown]"localhost"[/color];
   $username [color=blue]=[/color] [color=brown]"mark"[/color];
   $password [color=blue]=[/color] [color=brown]"somepass"[/color];
   $database [color=blue]=[/color] [color=brown]"gamecal"[/color];
   [color=blue]mysql_connect[/color]($host,$username,$password) or [color=blue]die[/color]( "Unable to connect");
   [color=orange]// if you notice i romove the '@' symbol[/color]
   [color=blue]mysql_select_db[/color]($database) or [color=blue]die[/color]( "Unable to select database");
   [color=orange]// columnames... are the receiving variable from your database like ex: id, name, address, etc.[/color]
   $query = [color=brown]"INSERT INTO games (columname1,columname2,columnames...) VALUES ('abc','123')"[/color];
   [color=blue]mysql_query[/color]($query);
   [color=blue]mysql_close[/color];
[color=red]?> [/color]


[color=red][b]Sami[/b] | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Post Reply