Home » Php » How to UPDATE data in MySQL – PHP

How to UPDATE data in MySQL – PHP

Posted by: admin February 22, 2020 Leave a comment

Q(Question):

Ok, i am new here and still learning sql, i have made a table called users and inside points default 5.

and when they submit the form i use this to update it +5 points.

[PHP]$sql = INSERT INTO ‘users’ SET ‘points’=[+5] WHERE ‘userid’= ‘$userid’;
$result = mysql_query($sql ,$db);[/PHP]

But the value never changes from 5 to 10?
Can anyone please help me!

A(Answer):

Ok, i am new here and still learning sql, i have made a table called users and inside points default 5.

and when they submit the form i use this to update it +5 points.

[PHP]$sql = INSERT INTO ‘users’ SET ‘points’=[+5] WHERE ‘userid’= ‘$userid’;
$result = mysql_query($sql ,$db);[/PHP]

But the value never changes from 5 to 10?
Can anyone please help me!

Try adding " around your query. The way it’s written it should throw an error at your browser.
Also, you should have set the default value in table shema, something like this:


CREATE TABLE users(
...
points INTEGER NOT NULL DEFAULT (5),
...
);

If what you want is to update the field for an existing user, you should use update query, insert always adds a new record, and i think it can’t have WHERE clause…

The query you need is:


UPDATE users SET points=points+5 WHERE userid= '$userid';

A(Answer):

SQL query:


ALTER TABLE users ADD points INTEGER NOT NULL DEFAULT( 5 )

MySQL said: Documentation
#1064 – You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘(5)’ at line 2

How? and do i need to make it auto increment…

and in the php:

$sql = "UPDATE users SET points=points+5 WHERE userid= '$userid'";
$result = mysql_query($sql ,$db);

——————————————————————————-
Is it mysql messing up? i also have an error in the comment form. it let me submit the data once but then never updates it.
1, breana, comment, date
2, mike, comment, date
its stuck at 1?

$commentid = nextid(COMMENTS, "commentid");
$commentdate = dbdate();
$newcomment = ereg_replace("\n", "<BR>\n", $comment);
$sql = "INSERT INTO comments (
`commentid` ,
`itemid` ,
`userid` ,
`date` ,
`name` ,
`comment` ,
`status`
) VALUES (
'$commentid',
'$itemid',
'$uid',
'$commentdate',
'$name',
'$newcomment',
'$status')";
$result = mysql_query($sql ,$db);

A(Answer):

Heya, Breana. Welcome to TSDN!

Please use CODE tags when posting source code. See the REPLY GUIDELINES on the right side of the page next time you post.

Try this instead:


ALTER TABLE
`users`
ADD
`points`
INT(5)
NOT NULL
DEFAULT;

A(Answer):

Error

SQL query:


ALTER TABLE `users` ADD `points` INT( 5 ) NOT NULL DEFAULT

MySQL said: Documentation
#1064 – You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ” at line 2

:(…. i just want these to things to work… my brain hurts!

A(Answer):

Hi, Breana. Welcome to TSDN!

I have edited the thread title. Please avoid using phrases like ‘please help’ when naming you threads!
Threads with meaningful titles are more likely to be found by our Experts as well as other people looking for solutions to similar problems.

Please take a look at the Posting Guidilines before posting.

Moderator

A(Answer):

Oh sorry, i dident know….

A(Answer):

Heya, Breana.

My bad. You need a default value to use the DEFAULT attribute. This should work:


ALTER TABLE
`users`
ADD
`points`
INT(5)
NOT NULL;

A(Answer):

oops double post sorry

A(Answer):

Cool it saved:

Your SQL query has been executed successfully (Query took 0.0429 sec)

let me test it by updating the points…nope it still at 0.

here is the code in the php:

$sql = "UPDATE users SET points=points+5 WHERE userid= '$userid'";
$result = mysql_query($sql ,$db);

is it because i all ready connected to the item table befor that one…?

A(Answer):

Ou yeah, my bad, you should say this when specifying default value:


...
points INTEGER NOT NULL DEFAULT 5,
...

When you give a number after field type it only means that much digits/charactes will that field be able to hold so INT(5) makes an int field that has max 5 digits…

That last alter didn’t set the default value, that’s why the php code didn’t update the field to +5 (or 10 if you want the default to be 5)

Summary:
php code seams to be fine,
change the points field as specified above.

A(Answer):

Ok… i am los lol i’m sorry i am just starting out, here is what i want it to do.

The data base, users, points to be at 0 by default and the max to be unlimited in points. so when he signes up it will be 0 in less i give 5 free at sign up.

and every time he post a code or dl it gives him +5 points.
So every update, 0,5,10,15,20, and so on…

So how do i need to set up the table points in users, do do this?
Please not i have the php update it every time if needed but i need it to allow this..

Maybe its because i have it connect to the item table first?
Here is the code in the php… last resort i guess i am goin to cry…

<?php
if (logincheck($uid, $upwd)) {
$error = false;
$errormessage = "";
if (strlen($itemtitle) < 1) {
$error = true;
$errormessage .= "<li><b>Sorry. You did not enter a <?php printf($itemlower); ?> title.</b><br>\n";
}
if (strlen($directions) < 1) {
$error = true;
$errormessage .= "<li><b>Sorry. You did not enter any directions.</b><br>\n";
}
if ($catid == "") {
$error = true;
$errormessage .= "<li><b>Sorry. You did not chose a category.</b><br>\n";
}
IF ($_FILES['frmpicture']['type'] != "") {
if (($_FILES['frmpicture']['type'] == "image/gif") || ($_FILES['frmpicture']['type'] == "image/jpeg") || ($_FILES['frmpicture']['type'] == "image/jpg") || ($_FILES['frmpicture']['type'] == "image/pjpeg")) {
printf("<br /><img src=\"icons/Checked_OK.gif\" width=\"19\" height=\"18\" /> <font face='Arial, Helvetica, sans-serif' size=2>Box Art Uploaded<br><br>");
printf("<img src=\"images/view_profile.png\" width=\"24\" height=\"24\" />" . $_FILES['frmpicture']['name']);
if ($_FILES['frmpicture']['type'] == "image/gif") {
$imageext = "gif";
} else {
$imageext = "jpg";
}
} else {
$error = true;
$errormessage .= "file: " . $filetest . "<li><b> Sorry. You did not enter a valid picture.</b><br>\n";
}
} else {
printf ("<br><img src=\"icons/Error_NotOK.gif\" width=\"18\" height=\"18\" /> <b>No Box Art Uploaded<br></b>");
}
if ($error) {
printf("<font face='Arial, Helvetica, sans-serif' size=2>There was a problem creating the <?php printf($itemlower); ?>. Please click on back in your browser to fix the problems listed below.<br><br>");
printf($errormessage);
} else {
$sql = "select approve_content from admin_control";
$result = mysql_query($sql ,$db);
if ($myrow = mysql_fetch_array($result)) {
do {
$approve = $myrow["approve_content"];
} while ($myrow = mysql_fetch_array($result));
}
if ($approve == "Y") {
$status = "W";
} else {
$status = "L";
}
$itemid = nextid("items", "itemid");
$date = dbdate();
$newdirections = ereg_replace("\n", "<BR>\n", $directions);
$sql = "INSERT INTO items (
`itemid`,
`title`,
`directions`,
`rating`,
`categoryid`,
`date`,
`status`,
`gameinfo`,
`gamerating`,
`publisher`,
`genar`,
`userid`
) VALUES (
'".(int)$itemid."',
'".mysql_real_escape_string($itemtitle)."',
'".mysql_real_escape_string($newdirections)."',
'0',
'".(int)$catid."',
'".mysql_real_escape_string($date)."',
'".mysql_real_escape_string($status)."',
'".mysql_real_escape_string($gameinfo)."',
'".mysql_real_escape_string($gamerating)."',
'".mysql_real_escape_string($publisher)."',
'".mysql_real_escape_string($genar)."',
'".(int)$uid."'
)";
$result = mysql_query($sql ,$db);
$sql = "UPDATE `users` SET `points`=[+5] WHERE `userid`= '$userid'";
if (($_FILES['frmpicture']['type'] == "image/gif") || ($_FILES['frmpicture']['type'] == "image/jpeg") || ($_FILES['frmpicture']['type'] == "image/pjpeg")) {
if ($_FILES['frmpicture']['type'] == "image/gif") {
$imageext = "gif";
} else {
$imageext = "jpg";
}
copy ($_FILES['frmpicture']['tmp_name'], "boxart/" . $itemid . "." . $imageext);
}
if ($approve == "Y") {
printf("<p><img src=\"icons/Checked_OK.gif\" width=\"18\" height=\"18\" /> <font face=arial size=-1>The <?php printf($itemlower); ?> has been added and is waiting approval.</font></p>");
} else {
printf("<p><font face=arial size=-1>The <?php printf($itemlower); ?> has been added to the database.</font></b</p>");
}
printf("<p><font face=arial size=-1><a href=additem.php>Add another <?php printf($itemlower); ?></a></font></p>");
}
?>

A(Answer):

OK, then alter your table points field like this:


ALTER TABLE users
CHANGE points points INTEGER NOT NULL DEFAULT 0;

(default value is the one after keyword DEFAULT)

You can specify the size like:


ALTER TABLE users
CHANGE points points INT(11) NOT NULL DEFAULT 0;

The thing is you can’t have unlimited numbers, after all they are limited by your memory. If you just say INTEGER (without size) it will default to INT(11).

Query which you could use for updates is, like i mentioned:


UPDATE users SET points=points+5 WHERE userid='$userID';

That’s all…
"and don’t you cry tonight…" 🙂

A(Answer):

Ok, so i can do this.

ALTER TABLE users
CHANGE points points INT(999) NOT NULL DEFAULT 0;

So the max will be 999 right? lol tha max is 255 lol so i guess i’ll have to set that as the limit sucks..

A(Answer):

I’m not sure if it can take that much
the max will be:

9999999…..99999999
|<——999 digits——>|

That’s a lot.
With size 11 the max is
99999999999
|<—-11——>|
You should be quite fine with that, unless you’re counting all the atoms in the known universe 🙂

Here’s a useful link you might wanna check out for details of mysql syntax:
http://dev.mysql.com/doc/refman/5.0/en/alter-table.html

A(Answer):

Oh ok, i thought 11 ment 11 max points lol thanks i am getting the ideah now lol dont smack me lol

A(Answer):

Heya, Breana.

Essentially, the number in the parenthesis tells MySQL how many places you want to save. So for example, INT(5) will save 5 places, which means that the maximum value is 99999 (5 nines).

This isn’t 100% true because of some very technical stuff; because of the size of a [signed] INT, the absolute maximum value, even if you declare INT(999), is 2147483647 (which is (2 ^ 8) ^ 4 / 2 – 1).

I won’t bore you with the details (too late?). If you want to make the maximum value 999, you would use INT(3).

A(Answer):

Ok, i did int 11 and done but i still cant get it to ad any points…

whis is how i have it set up, there has to be an error in there…

$sql = "INSERT INTO items (
`itemid`,
`title`,
`directions`,
`rating`,
`categoryid`,
`date`,
`status`,
`gameinfo`,
`gamerating`,
`publisher`,
`genar`,
`userid`
) VALUES (
'".(int)$itemid."',
'".mysql_real_escape_string($itemtitle)."',
'".mysql_real_escape_string($newdirections)."',
'0',
'".(int)$catid."',
'".mysql_real_escape_string($date)."',
'".mysql_real_escape_string($status)."',
'".mysql_real_escape_string($gameinfo)."',
'".mysql_real_escape_string($gamerating)."',
'".mysql_real_escape_string($publisher)."',
'".mysql_real_escape_string($genar)."',
'".(int)$uid."'
)";
$result = mysql_query($sql ,$db);
$sql = "INSERT INTO users SET points=points+5 WHERE userid='$userID'";
$result = mysql_query($sql ,$db);

Or is my code not ending or closing to soon to inset it?

A(Answer):

Heya, Breana.

Instead of:


$sql = "INSERT INTO users SET points=points+5 WHERE userid='$userID'";

Try this:


$sql = "UPDATE `users` SET `points`=`points`+5 WHERE `userid`='$userID' LIMIT 1";

A(Answer):

Insertions create new records, you need to update an existing one.

Use UPDATE query like many of us said.
Your script failing has nothing to do with max value of the field, you just need to UPDATE, not INSERT…

Sorry if this sounds a bit high toned, it’s not meant to, I’m just pointing it out:

UPDATE don’t INSERT

🙂

A(Answer):

$sql = "select `users` UPDATE `points`=`points`+5 WHERE `userid`='$userID' LIMIT 1";
$result = mysql_query($sql ,$db);

I GIVE UP, mysql hates me…. 🙁

I am giving up on user having points and try to fix the comment issue…

so maybe you can help with it?
i get it to submit to the data base but it only lets me do it once. after that it wont ad a new comment..

$commentid = nextid(COMMENTS, "commentid");
$commentdate = dbdate();
$newcomment = ereg_replace("\n", "<BR>\n", $comment);
$sql = "INSERT INTO comments (
`commentid` ,
`itemid` ,
`userid` ,
`date` ,
`name` ,
`comment` ,
`status`
) VALUES (
'$commentid',
'$itemid',
'$uid',
'$commentdate',
'$name',
'$newcomment',
'$status')";
$result = mysql_query($sql ,$db);

and the tabe was set up like this:

--
-- Table structure for table `comments`
--
CREATE TABLE `comments` (
`itemid` int(10) unsigned NOT NULL default '0',
`userid` int(10) unsigned default NULL,
`date` varchar(20) NOT NULL default '',
`name` varchar(30) NOT NULL default '',
`comment` text,
`status` char(1) default 'W',
`commentid` int(10) NOT NULL default '0',
PRIMARY KEY (`commentid`),
KEY `itemid` (`itemid`),
KEY `userid` (`userid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Dumping data for table `comments`
--
INSERT INTO `comments` (`itemid`, `userid`, `date`, `name`, `comment`, `status`, `commentid`) VALUES
(289, 2, '2007-08-22', 'Breana', 'fake comment lo sdssdhshsfhsfhhsfhsh', 'L', 2);

A(Answer):

$sql = "select `users` UPDATE `points`=`points`+5 WHERE `userid`='$userID' LIMIT 1";
$result = mysql_query($sql ,$db);

I GIVE UP, mysql hates me…. 🙁

Why the SELECT now? 🙂
You kinda hate UPDATE or something…

change that line to:
[PHP] $sql = "UPDATE `users` SET `points`=`points`+5 WHERE `userid`=’$userID’ LIMIT 1";
$result = mysql_query($sql ,$db);
[/PHP]

As for the other part… I’ll see that in a minute…

A(Answer):

Oh i changed it to select user after i tried the other wat thinking it wasent selecting the table users lol.

ok i will wait to hear help on the comments. part

A(Answer):

change line 12:

`commentid` int(10) NOT NULL default ‘0’,

to

commentid int(10) NOT NULL AUTO_INCREMENT,

don’t set the default, it’s a primary key and it should be unique for every record in the table…

A(Answer):

Ok, it changed the table to this:

--
-- Table structure for table `comments`
--
CREATE TABLE `comments` (
`itemid` int(10) unsigned NOT NULL default '0',
`userid` int(10) unsigned default NULL,
`date` varchar(20) NOT NULL default '',
`name` varchar(30) NOT NULL default '',
`comment` text,
`status` char(1) default 'W',
`commentid` int(10) NOT NULL auto_increment,
PRIMARY KEY (`commentid`),
KEY `itemid` (`itemid`),
KEY `userid` (`userid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

What to hear something weird, i tried this code in a diffrent form for the points and it works lol, weird….

$sql = "insert into users (points) values ('$points')";
$result = mysql_query($sql ,$db);

And a hidden form called points value 5 it worked lol..

A(Answer):

Ok, it changed the table to this:

--
-- Table structure for table `comments`
--
CREATE TABLE `comments` (
`itemid` int(10) unsigned NOT NULL default '0',
`userid` int(10) unsigned default NULL,
`date` varchar(20) NOT NULL default '',
`name` varchar(30) NOT NULL default '',
`comment` text,
`status` char(1) default 'W',
`commentid` int(10) NOT NULL auto_increment,
PRIMARY KEY (`commentid`),
KEY `itemid` (`itemid`),
KEY `userid` (`userid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

What to hear something weird, i tried this code in a diffrent form for the points and it works lol, weird….

$sql = "insert into users (points) values ('$points')";
$result = mysql_query($sql ,$db);

And a hidden form called points value 5 it worked lol..

It probably didn’t quite work, you just haven’t figured out the side effects yet. For starters, it would always create a new user. It would never update the score for an existing one. The default being set in table shema is a way better solution.

So, are the comments and the rest working now?

A(Answer):

Nope, i posed a comment, and it saved. logged out logged in as a diffrent user posted a comment dident save…

So i will include the page code maybe u can see an error or maybe i just suck at it.. sorry to bother u soo much i feel bad..

<?php include("rheader.php"); ?>
<TD align="left" valign="top">
<table width="600" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td valign="top">
<TABLE width=100% cellpadding=5 cellspacing=0 border=0>
<tr><td align=center><table cellspacing="1" cellpadding="1" bgcolor="#CBCBCB" width="100%">
<tr>
<td bgcolor="EBEBEB" align="left"><font face="Arial" size="1">Saving Comment: </font><font color="#FF0000" size="-2"><?php printf($username); ?></font></td>
</tr>
</table></td></tr>
<TR>
<TD valign=top>
<FONT SIZE=2>
<center>
<?php
if (logincheck($uid, $upwd)) {
$itemid = $_REQUEST['itemid'];
$name = $_POST['name'];
$comment = $_POST['comment'];
$error = false;
$errormessage = "";
if (strlen($name) < 1) {
$error = true;
$errormessage .= "<li><b>Sorry. You did not enter your name.</b><br>\n";
}
if (strlen($comment) < 1) {
$error = true;
$errormessage .= "<li><b>Sorry. You did not enter a comment.</b><br>\n";
}
if ($error) {
printf("<font face='Arial, Helvetica, sans-serif' size=2>There was a problem creating the Comment. Please click on back in your browser to fix the problems listed below.<br><br>");
printf($errormessage);
} else {
$sql = "select approve_content from " . ADMIN_CONTROL;
$result = mysql_query($sql ,$db);
if ($myrow = mysql_fetch_array($result)) {
do {
$approve = $myrow["approve_content"];
} while ($myrow = mysql_fetch_array($result));
}
if ($approve == "Y") {
$status = "W";
} else {
$status = "L";
}
$commentid = nextid(COMMENTS, "commentid");
$commentdate = dbdate();
$newcomment = ereg_replace("\n", "<BR>\n", $comment);
$sql = "INSERT INTO comments (
`commentid` ,
`itemid` ,
`userid` ,
`date` ,
`name` ,
`comment` ,
`status`
) VALUES (
'$commentid',
'$itemid',
'$uid',
'$commentdate',
'$name',
'$newcomment',
'$status')";
$result = mysql_query($sql ,$db);
if ($approve == "Y") {
printf("<p><font face=arial size=-1>The comment has been added and is waiting approval.</font></p>");
} else {
printf("<p><font face=arial size=-1>The comment has been added to the database.</font></p>");
}
}
?>
<?php
} else {
printf("<font size=2 face='Arial, Helvetica, sans-serif'><br>You need to be logged in before you can add a comment</font>");
}
?>
</center>
</FONT>
</TD>
</TR>
</TABLE>
</td>
</tr>
</table>
</TD>
<?php include("rfooter.php"); ?>

And instead of the first comment starting at 1 it started at 2?

A(Answer):

Ok, than you might wanna add AUTO_INCREMENT to your other KEY fields and remove DEFAULT from ’em.

Look, are those KEYs supposed to be foreign keys (they reference keys in other tables).
My spider sense tells me so, so this is how your table shema should look like:


--
-- Table structure for table `comments`
--
CREATE TABLE `comments` (
`itemid` int(10) unsigned NOT NULL default '0',
`userid` int(10) unsigned default NULL,
`date` varchar(20) NOT NULL default '',
`name` varchar(30) NOT NULL default '',
`comment` text,
`status` char(1) default 'W',
`commentid` int(10) NOT NULL auto_increment,
PRIMARY KEY (`commentid`),
FOREIGN KEY `itemid` REFERENCES `_items_table_name_' (`_items_table_primary_key_field_name_`),
FOREIGN KEY (`userid`) REFERENCES `users` (`userid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

If this is not the case, that is if you wanna a composite key
then all your KEY fields must be NOT NULL.

A(Answer):

Lol wow lots of errors

Error

There seems to be an error in your SQL query. The MySQL server error output below, if there is any, may also help you in diagnosing the problem

ERROR: Unclosed quote @ 461
STR: `
SQL:


CREATE TABLE `comments` (
`itemid` int(10) unsigned NOT NULL default '0',
`userid` int(10) unsigned default NULL,
`date` varchar(20) NOT NULL default '',
`name` varchar(30) NOT NULL default '',
`comment` text,
`status` char(1) default 'W',
`commentid` int(10) NOT NULL auto_increment,
PRIMARY KEY (`commentid`),
FOREIGN KEY `itemid` REFERENCES `_items_table_name_' (`_items_table_primary_key_field_name_`),
FOREIGN KEY (`userid`) REFERENCES `users` (`userid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3

SQL query:


CREATE TABLE `comments` ( `itemid` int(10) unsigned NOT NULL default '0', `userid` int(10) unsigned default NULL, `date` varchar(20) NOT NULL default '', `name` varchar(30) NOT NULL default '', `comment` text, `status` char(1) default 'W', `commentid` int(10) NOT NULL auto_increment, PRIMARY KEY (`commentid`), FOREIGN KEY `itemid` REFERENCES `_items_table_name_' (`_items_table_primary_key_field_name_`), FOREIGN KEY (`userid`) REFERENCES `users` (`userid`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3

MySQL said: Documentation
#1064 – You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘REFERENCES `_items_table_name_’ (`_items_table_primary_key_field_name_`),
FOREI’ at line 10

Man ur going to hate me soon lol, this aint my day…

A(Answer):

Of course it doesn’t work, there are table names you need to enter:
line saying FOREIGN KEY `itemid` REFERENCES `_items_table_name_’
_items_table_name needs to be changed to the name of your items table
and that apostrophe was my mistake i admit, it should be `
also change the _items_table_primary_key_field_name_ to the name of the primary key in your items table… those are all the info i don’t have and you do…

A(Answer):

Ok let me see:

--
-- Table structure for table `items`
--
CREATE TABLE `items` (
`itemid` int(10) unsigned NOT NULL default '0',
`title` varchar(255) default NULL,
`directions` text,
`userid` int(10) unsigned default NULL,
`rating` int(10) unsigned default '0',
`categoryid` int(10) unsigned default NULL,
`date` varchar(20) NOT NULL default '',
`story` text,
`hits` int(10) unsigned default '0',
`status` char(1) default 'W',
`gameinfo` text,
`gamerating` text,
`publisher` text,
`genar` text,
PRIMARY KEY (`itemid`),
UNIQUE KEY `recipieid` (`itemid`),
KEY `recipieid_2` (`itemid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Ut oh, i see an error, it should be itemid not recipe?

A(Answer):

Look, you might wanna check out some MYSQL tutorial or a book (i recommend Sql for mysql developers – a comprehensive tutorial and reference by Rick F. van der Lans)

try reading trough the shema i posted before…

Foreign keys are fields in one table that refer to primary keys in another, thus creating a link between them.
So, that last shema i wrote should have looked like this:


1.
--
-- Table structure for table `comments`
--
CREATE TABLE `comments` (
`itemid` int(10) unsigned NOT NULL default '0',
`userid` int(10) unsigned default NULL,
`date` varchar(20) NOT NULL default '',
`name` varchar(30) NOT NULL default '',
`comment` text,
`status` char(1) default 'W',
`commentid` int(10) NOT NULL auto_increment,
PRIMARY KEY (`commentid`),
FOREIGN KEY `itemid` REFERENCES `items` (`itemid`),
FOREIGN KEY (`userid`) REFERENCES `users` (`userid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

And, also in that items table set field itemid like this (the line 6):


itemid INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,

-it’s a primary key and it can’t have default value or it wont be unique for all the records.

A(Answer):

MyISAM tables support foreign keys? I thought that wasn’t implemented yet….

A(Answer):

Heya, Breana.

Ok let me see:

--
-- Table structure for table `items`
--
CREATE TABLE `items` (
`itemid` int(10) unsigned NOT NULL default '0',
`title` varchar(255) default NULL,
`directions` text,
`userid` int(10) unsigned default NULL,
`rating` int(10) unsigned default '0',
`categoryid` int(10) unsigned default NULL,
`date` varchar(20) NOT NULL default '',
`story` text,
`hits` int(10) unsigned default '0',
`status` char(1) default 'W',
`gameinfo` text,
`gamerating` text,
`publisher` text,
`genar` text,
PRIMARY KEY (`itemid`),
UNIQUE KEY `recipieid` (`itemid`),
KEY `recipieid_2` (`itemid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Ut oh, i see an error, it should be itemid not recipe?

Only what’s in the parenthesis is important. The ‘recipieid` is just the name of the index. However, you have a lot of redundancy in your indexes; you don’t need the UNIQUE KEY `recipieid` nor the KEY `recipieid_2`. They don’t do anything directly bad, but they do make your script run more slowly and take up extra disk space.

A(Answer):

Ok i changed it to:

CREATE TABLE `items` (
`itemid` INT( 10 ) UNSIGNED NOT NULL DEFAULT '0',
`title` VARCHAR( 255 ) DEFAULT NULL ,
`directions` TEXT,
`userid` INT( 10 ) UNSIGNED DEFAULT NULL ,
`rating` INT( 10 ) UNSIGNED DEFAULT '0',
`categoryid` INT( 10 ) UNSIGNED DEFAULT NULL ,
`DATE` VARCHAR( 20 ) NOT NULL DEFAULT '',
`story` TEXT,
`hits` INT( 10 ) UNSIGNED DEFAULT '0',
`status` CHAR( 1 ) DEFAULT 'W',
`gameinfo` TEXT,
`gamerating` TEXT,
`publisher` TEXT,
`genar` TEXT,
PRIMARY KEY ( `itemid` )
) ENGINE = MYISAM DEFAULT CHARSET = latin1

So what do i need to do for the comment part, and it wont let me import the 900 games i added so i goto re do those later it sucks. but i want it to be right and speedy lol.

So the comments table would have to be like this?

--
-- Table structure for table `comments`
--
CREATE TABLE `comments` (
`itemid` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`userid` INT(10) UNSIGNED DEFAULT NULL,
`DATE` VARCHAR(20) NOT NULL DEFAULT '',
`name` VARCHAR(30) NOT NULL DEFAULT '',
`comment` TEXT,
`status` CHAR(1) DEFAULT 'W',
`commentid` INT(10) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`commentid`),
FOREIGN KEY `itemid` REFERENCES `items` (`itemid`),
FOREIGN KEY (`userid`) REFERENCES `users` (`userid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

A(Answer):

MyISAM tables support foreign keys? I thought that wasn’t implemented yet….

Hm, yeah, that seams to be true… at least as i can conclude by first few hit titles on google…

The thing is I’m using them on my dev server (v 5.0.41)
And the shemas seam to work, at least for the creation part. Interestingly I never checked if it really checks those constraints…

On the other hand Breana might change db engine to InnoDB,
but I don’t know her/his design so that shema of mine was only an educated guess…

From what I can see, there are lots of places for improvements in her/his queries, and I think he/she should read some stuff about SQL before continuing… Even a short tutorial like the one on www.w3schools.com might help.

Database is a pretty slippery ground in the system, and it should be handled with care…

Well, that’s my closing for tonight, hope I was of some help…

ALL MY BASE ARE BELONG TO YOU!
And may the source be with you Breana!

A(Answer):

Heya, Breana.

Each table can only have one AUTO_INCREMENT column. That would be your primary key. Foreign keys do not AUTO_INCREMENT, as that is taken care of by the table that uses that key as its primary.

A(Answer):

I did it i got it working the comments i mean, i removed the old code i forgot about $commentid = nextid(COMMENTS, "commentid");
bacause i remember in the tutorial i used to make this he said remove it…
12 comments 🙂

i feel so stupid sorry guys, i really thank you for all your time u did with me i learned a lot lol.

And i did have to change the sql for comments to this:

--
-- Table structure for table `comments`
--
CREATE TABLE `comments` (
`itemid` int(10) unsigned NOT NULL,
`userid` int(10) unsigned default NULL,
`DATE` varchar(20) NOT NULL default '',
`name` varchar(30) NOT NULL default '',
`comment` text,
`status` char(1) default 'W',
`commentid` int(10) NOT NULL auto_increment,
PRIMARY KEY (`commentid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

A(Answer):

Ok i changed it to:

CREATE TABLE `items` (
`itemid` INT( 10 ) UNSIGNED NOT NULL DEFAULT '0',
`title` VARCHAR( 255 ) DEFAULT NULL ,
`directions` TEXT,
`userid` INT( 10 ) UNSIGNED DEFAULT NULL ,
`rating` INT( 10 ) UNSIGNED DEFAULT '0',
`categoryid` INT( 10 ) UNSIGNED DEFAULT NULL ,
`DATE` VARCHAR( 20 ) NOT NULL DEFAULT '',
`story` TEXT,
`hits` INT( 10 ) UNSIGNED DEFAULT '0',
`status` CHAR( 1 ) DEFAULT 'W',
`gameinfo` TEXT,
`gamerating` TEXT,
`publisher` TEXT,
`genar` TEXT,
PRIMARY KEY ( `itemid` )
) ENGINE = MYISAM DEFAULT CHARSET = latin1

So what do i need to do for the comment part, and it wont let me import the 900 games i added so i goto re do those later it sucks. but i want it to be right and speedy lol.

So the comments table would have to be like this?

--
-- Table structure for table `comments`
--
CREATE TABLE `comments` (
`itemid` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`userid` INT(10) UNSIGNED DEFAULT NULL,
`DATE` VARCHAR(20) NOT NULL DEFAULT '',
`name` VARCHAR(30) NOT NULL DEFAULT '',
`comment` TEXT,
`status` CHAR(1) DEFAULT 'W',
`commentid` INT(10) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`commentid`),
FOREIGN KEY `itemid` REFERENCES `items` (`itemid`),
FOREIGN KEY (`userid`) REFERENCES `users` (`userid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

Ok, here’s just one more:

No,
this: `itemid` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
goes into items table:

CREATE TABLE `items` (
`itemid` INT( 10 ) UNSIGNED NOT NULL AUTO_INCREMENT,
`title` VARCHAR( 255 ) DEFAULT NULL ,
`directions` TEXT,
`userid` INT( 10 ) UNSIGNED DEFAULT NULL ,
`rating` INT( 10 ) UNSIGNED DEFAULT '0',
`categoryid` INT( 10 ) UNSIGNED DEFAULT NULL ,
`DATE` VARCHAR( 20 ) NOT NULL DEFAULT '',
`story` TEXT,
`hits` INT( 10 ) UNSIGNED DEFAULT '0',
`status` CHAR( 1 ) DEFAULT 'W',
`gameinfo` TEXT,
`gamerating` TEXT,
`publisher` TEXT,
`genar` TEXT,
PRIMARY KEY ( `itemid` )
) ENGINE = MYISAM DEFAULT CHARSET = latin1

comments table create query shold look like i typed it a few posts ago…

At least that’s my idea… Again, you should be better off with some books/tutorials.

AYB!
E.

A(Answer):

I just went to mysql tutorials and got points running great it now updates my points to.
you were right, update did the trick, i commented out nextid from the code and walla worked..

Thanks to every one who helped me. I am still learning so i will be back but thanks agian.
I got to start addng my games again…..long night.

A(Answer):

I just went to mysql tutorials and got points running great it now updates my points to.
you were right, update did the trick, i commented out nextid from the code and walla worked..

Thanks to every one who helped me. I am still learning so i will be back but thanks agian.

You’re welcome.

…..long night.

Tell me about it. Being a code monkey is one hell of a summer job.

A(Answer):

Heya, Breana.

Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime 🙂