TeamPhotoshop
Reviews, updates and in depth guides to your favourite mobile games - AppGamer.com
Forum Home Latest Posts Search Help Subscribe

Everything looks ok... Whats wrong with my code? (php)

Page: 1 Reply
Mar 22nd 2004#145951 Report
Member since: Mar 22nd 2004
Posts: 1
Hello,

MsSQL is sent to all privelages, and connects fine. My problem is when i try to add or update a record-its just not happening.

To me the code looks fine.... can you tell me what am i doing wrong?
[PHP]
error_reporting(E_ALL);
include('settings.inc.php');
if (isset($_GET["action"]))
{
if ($_GET["action"] == "submit")
{
$sql = "INSERT INTO categories
(
name,
description,
id
) VALUES (
'$name',
'$description',
'$id'
)";
$result = mysql_query($sql) or die (mysql_error());
print ("..... Added to the database.

");
include('categories.inc.php');
print ("
add category


");
}
}
else if (isset($_GET["action"]))
{
if ($_GET["action"] == "update")
{
$sql_update = "UPDATE categories SET
name='$name',
description='$description'
WHERE
id='$id'";
$result = mysql_query($sql_update) or die (mysql_error());
print ("..... Updated database.

");
include('categories.inc.php');
}
}
else if (isset($_GET["action"]))
{
if ($_GET["action"] == "id")
{
$result = mysql_query("SELECT * FROM categories WHERE id=$id",$db);
$info = mysql_fetch_array($result);
?>




Name:

Notes:







}
}

else
{
?>










Name:



Notes:





 


}
?>

[/PHP]

What am i doing wrong?
Reply with Quote Reply
Mar 22nd 2004#145961 Report
Member since: May 22nd 2003
Posts: 315
Try this:
[PHP]

$sql = "INSERT INTO `categories`
(
`name`,
`description`,
`id`
) VALUES (
'$name',
'$description',
'$id'
)";


[/PHP]
Reply with Quote Reply
Mar 24th 2004#146227 Report
Member since: Mar 29th 2003
Posts: 1326
/me dies.

PLEASE dont use the b tag, font tag, center tag, or unquoted attribute values. They're all either depreciated or frowned upon.
Reply with Quote Reply
Mar 25th 2004#146305 Report
Member since: May 22nd 2003
Posts: 315
[QUOTE=trhaynes]/me dies.

PLEASE dont use the b tag, font tag, center tag, or unquoted attribute values. They're all either depreciated or frowned upon.[/QUOTE]


Are you making fun of me? Huh?!
Reply with Quote Reply
Apr 30th 2004#149330 Report
Member since: Apr 25th 2004
Posts: 74
That all depends on what DOCTYPE you specify. I have found that not specifying a doctype is best for compatibility. I personally think that w3c standards are overrated. However, I always quote my values :P
btw did you get the problem solved? If not, I'll look through your code


Endat
Reply with Quote Reply
Apr 30th 2004#149336 Report
Member since: Nov 26th 2001
Posts: 2586
[QUOTE=boom4x]Hello,

MsSQL is sent to all privelages, and connects fine. My problem is when i try to add or update a record-its just not happening.

To me the code looks fine.... can you tell me what am i doing wrong?
[PHP]
error_reporting(E_ALL);
include('settings.inc.php');
if (isset($_GET["action"]))
{
if ($_GET["action"] == "submit")
{
$sql = "INSERT INTO categories
(
name,
description,
id
) VALUES (
'$name',
'$description',
'$id'
)";
$result = mysql_query($sql) or die (mysql_error());
print ("..... Added to the database.

");
include('categories.inc.php');
print ("
add category


");
}
}
else if (isset($_GET["action"]))
{
if ($_GET["action"] == "update")
{
$sql_update = "UPDATE categories SET
name='$name',
description='$description'
WHERE
id='$id'";
$result = mysql_query($sql_update) or die (mysql_error());
print ("..... Updated database.

");
include('categories.inc.php');
}
}
else if (isset($_GET["action"]))
{
if ($_GET["action"] == "id")
{
$result = mysql_query("SELECT * FROM categories WHERE id=$id",$db);
$info = mysql_fetch_array($result);
?>




Name:

Notes:







}
}

else
{
?>










Name:



Notes:





 


}
?>

[/PHP]

What am i doing wrong?[/QUOTE]

A frew things popped out right away (that I would change):

1. [php] Don't use
2. $PHP_SELF is wrong. $_SERVER['PHP_SELF'] is what you want. and remember whenever you have [php][/php] --> end it with a semi-colon.

3. When using sql statements and using variables in php as values, put them between '$someVar' -- quotes. so you have this statement:

SELECT * FROM categories WHERE id=$id --> put $id between single quotes.

4. You are using an ID for your sql. Set those to AUTO_INCREMENT and reflect that in your insert statements by using emtpy quotes. ie:

INSERT INTO categories (name, description, id) VALUES ('$name','$description','')

Note that your DB table has to have an auto_incrementing coumn that reflects that. If that table only has 3 columns you don't need to declare the column names. You could also just use:

INSERT INTO categories VALUES ('$name','$description','')



To make life easier you don't need to use ( ) around echo or print or includes.

Your script would be better suited as a 'case' rather than a bunch of if / elseif /else statements.

Example:

[quote]
if (isset($_GET["action"])) {

switch ($_GET["action"]) {
case 'submit':
... put the code you want to execute here...
break;
case 'update':
... put the code you want to execute here...
break;
case 'id':
... put the code you want to execute here...
break;
}

}
[/quote]


and your form is also messed up. You have an input tag before the form tag (last form). Also - this might be from the forum post - but your indenting is all over the place.

* rant...
Regarding w3c standards. I wouldn't say they are overrated. They should be stuck with strictly (by the browser programmers). Case in point. Yesterday having a width set to %100 and the body has a 23 px margin on all sides. Now some browsers will give you 100% minus the margin (as they are supposed to) while IE adds the margin to the width and you get a page that extends too far on the left (scroll bars). So you either have to make a hack or set your percentage slightly less so it looks like you want in IE and not the way you want in lesser used browsers.

The standards are created for a reason. There is no reason developers should have to create sites for browsers when the rules clearly state that the markup or css should behave like it is stated.

Using an old doctype will support depracated tags as will no doctype at all. But later on down the road when browsers just stop supporting font tags, eg, do you really want to spend time to fix all that? Plus CSS is the way to properly format text so if you really want to be a web developer, get used to it.

I can accept older browsers not displaying properly, but the latest version of IE6 still tweaks CSS how M$ decides and not the W3C and that is poor development on M$'s side, if you ask me.

Our lives are ruled by standards on so many levels that are taken for granted you don't even realise it. If they weren't you would have all sorts of issues. "Well I went to get a quart of OIL for my car, but all they had were Mozilla 10G4352S brand and I know that if I buy that oil I have to go to the dump and find a dead cat, grind it's claws into meal and add it to the oil to get it to work properly in my car...." K that was a dumb example, but my point is standards are created so companies that create a product that will be useable amongst other products. Browsers are one of those and should abide by the standards very strictly.

* end rant...
Reply with Quote Reply
Page: 1 Back to top
Please login or register above to post in this forum