Jump to content

PHP MYSQL - INSERT INTO Help


iJoseph

Recommended Posts

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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';
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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"

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.