Page 1 of 1

Having trouble with INSERT INTO

Posted: Fri Sep 15, 2006 12:48 am
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]

Posted: Fri Sep 15, 2006 1:35 am
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);

Posted: Fri Sep 15, 2006 2:05 am
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?

Posted: Fri Sep 15, 2006 3:06 am
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"

Posted: Fri Sep 15, 2006 3:27 am
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...

Posted: Fri Sep 15, 2006 4:51 am
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...

Posted: Sat Sep 16, 2006 7:14 pm
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]