Having trouble with INSERT INTO

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
trip272
Forum Newbie
Posts: 2
Joined: Fri Sep 15, 2006 12:45 am

Having trouble with INSERT INTO

Post by trip272 »

Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I've tried just about everything I can think of. I have a form that posts to this script. It seems to work (I don't get error connecting to mysql) but what I'm doing isnt showing up in the database. I am new to php so it's probably something stupid.

Code: Select all

<?php
$dbhost = 'localhost';
$dbuser = $_POST["username"];
$dbpass = $_POST["pw"];

$dateStamp = date("Y-m-d");
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die                      ('Error connecting to mysql');
$dbname = 'games';
mysql_select_db('$dbname');

mysql_query("INSERT INTO games (Name, Description, Author)
VALUES ('$_POST[subname]','$_POST[desc]','$_POST[auth]')");

mysql_close();
echo "Success";
echo $dateStamp;
?>
The " and ' stuff confuses me. So I'm guessing it has to do with that somehow.


Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
ody
Forum Contributor
Posts: 147
Joined: Sat Mar 27, 2004 4:42 am
Location: ManchesterUK

Post by ody »

For a start your are litarly asking mysql to open a database called $dbname, NOT 'games', so change:

Code: Select all

mysql_select_db('$dbname');
to

Code: Select all

mysql_select_db($dbname);
trip272
Forum Newbie
Posts: 2
Joined: Fri Sep 15, 2006 12:45 am

Post by trip272 »

Damn thanks a bunch man. Worked like a charm. I'm still confused about what I use ' for and what I don't but I'm sure I'll get the hang of it.

I have another question if its not too much of a bother...
I want to give all the items in my table an ID number starting from 1 and going up as I insert new rows over time. Is there a built in way to do this or do I have to do it manually from php?
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

trip272 wrote:Damn thanks a bunch man. Worked like a charm. I'm still confused about what I use ' for and what I don't but I'm sure I'll get the hang of it.
http://www.evilwalrus.org/scripts/Diffe ... _and_print
trip272 wrote:I have another question if its not too much of a bother...
I want to give all the items in my table an ID number starting from 1 and going up as I insert new rows over time. Is there a built in way to do this or do I have to do it manually from php?
Google "mysql auto_increment"
xfuture
Forum Newbie
Posts: 6
Joined: Wed Sep 13, 2006 2:27 am

Post by xfuture »

yeah.. define your MySQL primary key (ID) with feature auto_increment will do the job..
or do in complex way... use a SQL command, which search for the max value of ID column, then +1...
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

do in complex way... use a SQL command, which search for the max value of ID column, then +1...
... and get ready to debug elusive bugs due to the race conditions...
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I've recently started learning PHP & MySQL and a simple way I see of doing it at the moment is:

Code: Select all

$numRows = mysql_numrows($DBresults); // Returns the amount of rows currently in the DB //
$myCounter = $numRows + 1; // Adds 1 to the counter for this current record //

//Insert $myCounter as the primary key in the MySQL DB. //
Works a treat.


Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Post Reply