Zoofu Posted September 8, 2009 Share Posted September 8, 2009 if(!$_POST['buy']){ if($row['tokens'] < $price){ $sql3 = "INSERT INTO `inventory` (`id`,`uid`,`price`,`name`,`image`) VALUES('".$id."','".$_SESSION['uid']."','".$price."','".$name."','".$imageurl."')"; $res3 = mysql_query($sql3) or die(mysql_error()); }else { echo "Insufficient Funds!"; } } I'm trying to get this function to work, except it's not inserting ANYTHING into inventory. Been trying to get it working for what? an hour or so? Quote Link to comment https://forums.phpfreaks.com/topic/173563-insert-into-problem/ Share on other sites More sharing options...
Maq Posted September 8, 2009 Share Posted September 8, 2009 Echo $sql3 and post the output. Quote Link to comment https://forums.phpfreaks.com/topic/173563-insert-into-problem/#findComment-914870 Share on other sites More sharing options...
SilveR316 Posted September 8, 2009 Share Posted September 8, 2009 Generally the id on insert queries should be NULL so its autoincremented. Why are you setting it and what are you setting it to? Quote Link to comment https://forums.phpfreaks.com/topic/173563-insert-into-problem/#findComment-914871 Share on other sites More sharing options...
Zoofu Posted September 8, 2009 Author Share Posted September 8, 2009 echo "<center><table border=\"1\" cellspacing=\"3\" cellpadding=\"3\">\n"; echo "<form method=\"post\" action=\"./process.php\">\n"; echo "<tr><td colspan=\"2\" align=\"Center\"><input type=\"submit\" name=\"buy\" value=\"Purchase\"></td></tr>\n"; echo "</form></table></center>\n"; if(!$_POST['buy']){ if($row['tokens'] < $price){ $sql3 = "INSERT INTO `inventory` (`id`,`uid`,`price`,`name`,`image`) VALUES('".$id."','".$_SESSION['uid']."','".$price."','".$name."','".$imageurl."')"; $res3 = mysql_query($sql3) or die(mysql_error()); }else { echo "Insufficient Funds!"; } } I'm trying to get it so when you click purchase and have enough 'tokens' it will insert the values already defined into the fields. <?php session_start(); include "./globals.php"; $action = $_GET['act']; $actions_array = array("itemcomment","nitemcomment"); ?> <html> <head> <title>Zoofu | View Item</title> <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" /> <link rel="icon" href="/favicon.ico" type="image/x-icon" /> <link rel="stylesheet" type="text/css" href="./style.css"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <center> <div id="holder"> <div id="userInfo"> <?php if($_SESSION['uid']){ $sql = "SELECT * FROM `users` WHERE `id`='".$_SESSION['uid']."'"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) == 0){ session_destroy(); echo "<div align=\"right\"><a href=\"./login.php\">Login</a> | <a href=\"./register.php\">Register</a></div>\n"; }else { $row = mysql_fetch_assoc($res); echo "<div align=\"right\">Logged in as: <a href=\"./index.php?act=profile&id=".$row['id']."\">".$row['username']."</a> | <a href=\"./inventory.php\">My Inventory</a> | <a href=\"./logout.php\">Logout?</a></div>\n"; echo "<div align=\"right\">Lv. ".$row['level']." | EXP ( ".$row['exp']." / 1000 ) | <img src=\"./token.png\"> Tokens ( ".$row['tokens']." )</div>\n"; if($row['admin'] == '1'){ echo "<a href=\"./admin.php\"></a>\n"; } } }else { echo "<div align=\"right\"><a href=\"./login.php\">Login</a> | <a href=\"./register.php\">Register</a></div>\n"; } $admin_user_level = $row['admin']; echo "<tr><td><center><img src=\"./logo.png\"></center></td></tr>\n"; echo "<center><table border=\"0\" cellspacing=\"3\" cellpadding=\"3\" width=\"85%\">\n"; echo "<tr><td><center><b><a href=\"./games.php\">| Games |</a></b></td></center><td><center><b><a href=\"./catalog.php\">| Catalog |</a></b></td></center><td><center><b><a href=\"./browse.php\">| Browse |</a></b></center></td><td><center><b><a href=\"./index.php\">| Forum |</a></b></center></td><td><center><b><a href=\"./help.php\">| Help |</a></b></center></td></tr>\n"; echo "</table></center>\n"; ?> <div id ="content"> <?php $id = $_GET['id']; $query = mysql_query("SELECT * FROM items WHERE id = '$id'"); $fetch_array = mysql_fetch_array($query); $name = $fetch_array['name']; $id = $fetch_array['id']; $price = $fetch_array['price']; $comments = $fetch_array['comment']; $imageurl = $fetch_array['image']; $desc = $fetch_array['desc']; $type = $fetch_array['type']; $sel=mysql_query("SELECT * FROM `items` WHERE `id`=".$id."") OR die(mysql_error()); $row=mysql_fetch_array($sel); echo "<center><table border=\"0\" cellspacing=\"3\" cellpadding=\"3\" width=\"80%\">\n"; echo "<tr><td class=\"forum_header\"><center><b>".$name."</b><br>ItemID: ".$id."</center></td></tr>\n"; echo "<br>\n"; echo "</table></center>\n"; echo "<center><table border=\"0\" cellspacing=\"3\" cellpadding=\"3\" width=\"30%\">\n"; echo "<tr><td class=\"forum_header\"><center><b><img src=".$imageurl."></center></td></tr>\n"; echo "</table></center>\n"; echo "<center><table border=\"0\" cellspacing=\"3\" cellpadding=\"3\" width=\"50%\">\n"; echo "<tr><td class=\"forum_header\"><center><b>Description</center></b><br>".$desc."</td>\n"; echo "</tr></table></center>\n"; echo "<center><table border=\"0\" cellspacing=\"3\" cellpadding=\"3\" width=\"50%\">\n"; echo "<tr><td class=\"forum_header\"><center><img src=\"./token.png\"> Tokens: $price</center><br><center><b>Type: $type</b></center></td>\n"; echo "</tr></table></center>\n"; echo "<center><table border=\"1\" cellspacing=\"3\" cellpadding=\"3\">\n"; echo "<form method=\"post\" action=\"./process.php\">\n"; echo "<tr><td colspan=\"2\" align=\"Center\"><input type=\"submit\" name=\"buy\" value=\"Purchase\"></td></tr>\n"; echo "</form></table></center>\n"; if(!$_POST['buy']){ if($row['tokens'] < $price){ $sql3 = "INSERT INTO `inventory` (`id`,`uid`,`price`,`name`,`image`) VALUES('".$id."','".$_SESSION['uid']."','".$price."','".$name."','".$imageurl."')"; $res3 = mysql_query($sql3) or die(mysql_error()); }else { echo "Insufficient Funds!"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/173563-insert-into-problem/#findComment-914873 Share on other sites More sharing options...
TeNDoLLA Posted September 8, 2009 Share Posted September 8, 2009 As Maq said, echo out the query and post it here for starters. Quote Link to comment https://forums.phpfreaks.com/topic/173563-insert-into-problem/#findComment-914881 Share on other sites More sharing options...
Zoofu Posted September 9, 2009 Author Share Posted September 9, 2009 It shows "Duplicate entry '1' for key 1" And then when you click purchase, it says "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 1" Quote Link to comment https://forums.phpfreaks.com/topic/173563-insert-into-problem/#findComment-915554 Share on other sites More sharing options...
Zoofu Posted September 9, 2009 Author Share Posted September 9, 2009 And echoing $sql3 didn't show anything. Quote Link to comment https://forums.phpfreaks.com/topic/173563-insert-into-problem/#findComment-915560 Share on other sites More sharing options...
Zoofu Posted September 9, 2009 Author Share Posted September 9, 2009 Oh I see, it is inserting it into inventory. But only allowing 1 of each item now. Quote Link to comment https://forums.phpfreaks.com/topic/173563-insert-into-problem/#findComment-915563 Share on other sites More sharing options...
TeNDoLLA Posted September 9, 2009 Share Posted September 9, 2009 You probably have the id field as PRIMARY KEY, so it can't be duplicate. And it SHOULD be auto_increment also so you don't even need to insert the ID field never ever. The second error appears because your SQL query has errors in it as the error message says. So echo out the query that fails and post here so we can see what is wrong. If you have some queries before $sql3 echo them and see what is the result. If $sql3 was empty it probably never goes inside that if -statement at all. Quote Link to comment https://forums.phpfreaks.com/topic/173563-insert-into-problem/#findComment-915568 Share on other sites More sharing options...
Zoofu Posted September 9, 2009 Author Share Posted September 9, 2009 Yeah, that's what I just realised. Thanks. But the if statements isn't working... the if($row['tokens'] < $price){ part. Dunno why. Quote Link to comment https://forums.phpfreaks.com/topic/173563-insert-into-problem/#findComment-915576 Share on other sites More sharing options...
TeNDoLLA Posted September 9, 2009 Share Posted September 9, 2009 Before the if do two line code test: var_dump($row['tokens']); and var_dump($price); and post both output here please. Quote Link to comment https://forums.phpfreaks.com/topic/173563-insert-into-problem/#findComment-915577 Share on other sites More sharing options...
Zoofu Posted September 9, 2009 Author Share Posted September 9, 2009 NULL string(1) "3" Is what appeared. So I take it that it recognises $price, and not tokens? Quote Link to comment https://forums.phpfreaks.com/topic/173563-insert-into-problem/#findComment-915578 Share on other sites More sharing options...
mattal999 Posted September 9, 2009 Share Posted September 9, 2009 Yeah, that's what I just realised. Thanks. But the if statements isn't working... the if($row['tokens'] < $price){ part. Dunno why. Just saw the values, and don't know what the problem is there, but this statement is checking if your amount of 'tokens' is less than the item 'token value'. Shouldn't that be the other way around? eg. If the player tokens are > than the item token value... Quote Link to comment https://forums.phpfreaks.com/topic/173563-insert-into-problem/#findComment-915581 Share on other sites More sharing options...
TeNDoLLA Posted September 9, 2009 Share Posted September 9, 2009 Yes it means $row['tokens'] has a NULL value and $price = "3". So that is why the if does not evaluate as TRUE. Because NULL is not same as 3. This is basic debugging and good to remember things like this. The next step would be to find out why is tokens NULL. <?php var_dump((NULL < '3')); Actually that gives out a TRUE value so... a bit weird why it's not going anyway inside the if. But now i gotta go.. will be back later! Quote Link to comment https://forums.phpfreaks.com/topic/173563-insert-into-problem/#findComment-915582 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.