iJoseph Posted July 28, 2010 Share Posted July 28, 2010 I have this code: <?php $con = mysql_connect("localhost","hhh","hhh"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("hhh", $con); // -------------------- // Avatar insert check // -------------------- session_start(); $name = $_POST[name]; $group = $_POST[group]; $age = $_POST[age]; $usernameid = $_SESSION[id]; $result = mysql_query("SELECT * FROM avatars WHERE name='$_POST[name]'"); $num = mysql_numrows($result); if ($num == 0) { mysql_query("INSERT INTO avatars (id, usernameid, name, group, age, xp) VALUES ('', '$usernameid', '$name', '$group', '$age', '0')"); header( 'Location: me/' ) ; } else echo 'Sorry, please pick a new name'; ?> And it does everything but put the data into the datebase. If I add a session befor and after '$request' they both run, but the sql doesn't. No error returns, if just redirects to the other page. Any help? Quote Link to comment https://forums.phpfreaks.com/topic/209087-php-mysql-insert-into-help/ Share on other sites More sharing options...
trq Posted July 28, 2010 Share Posted July 28, 2010 Where are $username, $password', $email, $dob & $realname defined? Quote Link to comment https://forums.phpfreaks.com/topic/209087-php-mysql-insert-into-help/#findComment-1092027 Share on other sites More sharing options...
iJoseph Posted July 28, 2010 Author Share Posted July 28, 2010 Ah, I used the wrong code. This is the code for registering the actual user, not the database I want to insert into, but for some reason I put the other SQL query. :S sorry. Quote Link to comment https://forums.phpfreaks.com/topic/209087-php-mysql-insert-into-help/#findComment-1092030 Share on other sites More sharing options...
iJoseph Posted July 28, 2010 Author Share Posted July 28, 2010 There - I put the correct sql query in. Still the same problem. Quote Link to comment https://forums.phpfreaks.com/topic/209087-php-mysql-insert-into-help/#findComment-1092032 Share on other sites More sharing options...
trq Posted July 28, 2010 Share Posted July 28, 2010 if ($num == 0) { $sql = "INSERT INTO avatars (id, usernameid, name, group, age, xp) VALUES ('', '$usernameid', '$name', '$group', '$age', '0')"; if (mysql_query($sql) { header( 'Location: me/' ); } else { trigger_error(mysql_error() . "<br />$sql"); } } else { echo 'Sorry, please pick a new name'; } Any output? Quote Link to comment https://forums.phpfreaks.com/topic/209087-php-mysql-insert-into-help/#findComment-1092034 Share on other sites More sharing options...
iJoseph Posted July 28, 2010 Author Share Posted July 28, 2010 Yes, I get an error, but I don't see why. Parse error: syntax error, unexpected '{' There is no '{' on the line the erorr is saying (28). Quote Link to comment https://forums.phpfreaks.com/topic/209087-php-mysql-insert-into-help/#findComment-1092036 Share on other sites More sharing options...
Alex Posted July 28, 2010 Share Posted July 28, 2010 There's a missing ), try: if ($num == 0) { $sql = "INSERT INTO avatars (id, usernameid, name, group, age, xp) VALUES ('', '$usernameid', '$name', '$group', '$age', '0')"; if (mysql_query($sql)) { header( 'Location: me/' ); } else { trigger_error(mysql_error() . "<br />$sql"); } } else { echo 'Sorry, please pick a new name'; } Quote Link to comment https://forums.phpfreaks.com/topic/209087-php-mysql-insert-into-help/#findComment-1092037 Share on other sites More sharing options...
iJoseph Posted July 28, 2010 Author Share Posted July 28, 2010 Yeah. This looks like the correct error: Notice: 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 'group, age, xp) VALUES ('', '9', '', '', '', '0')' at line 1 INSERT INTO avatars (id, usernameid, name, group, age, xp) VALUES ('', '9', '', '', '', '0') in _________ on line 31 Still have no idea though. Quote Link to comment https://forums.phpfreaks.com/topic/209087-php-mysql-insert-into-help/#findComment-1092038 Share on other sites More sharing options...
iJoseph Posted July 28, 2010 Author Share Posted July 28, 2010 It looks like the variables are not being seen by the code, so they are filling the insert area with 'null' Quote Link to comment https://forums.phpfreaks.com/topic/209087-php-mysql-insert-into-help/#findComment-1092039 Share on other sites More sharing options...
trq Posted July 28, 2010 Share Posted July 28, 2010 Well, firstly. Associative array indexes are strings. Strings need quotes in php. $name = $_POST['name']; $group = $_POST['group']; $age = $_POST['age']; $usernameid = $_SESSION['id']; You should then wrap all your code in an if statement that checks that the form submission was successful. if (isset($_POST['submit'])) { // rest of code } else { echo "No form data sent"; } Make sure you have a hidden field within your form named "submit" Quote Link to comment https://forums.phpfreaks.com/topic/209087-php-mysql-insert-into-help/#findComment-1092049 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.