OK I have a table in the database (boyem) called "amx_regusers".
In that table there are 4 fields fileds are as fallows:

nick, pass, flags, aflags

Well I have created a HTML page here is the html page.

regusers.html

 <html>
<head>
  <title>Nick Registration</title>
  <link href="csss.css" rel="stylesheet" type="text/css">
</head>

<body>
  <h1>Registering Names and Clans</h1>

  <form action="insert_player.php" method="post">
    <table border="0">
      <tr>
        <td width="103"><font color="#000000" face="Geneva, Arial, Helvetica, sans-serif"><strong>Nick</strong></font></td>
         <td width="271"><input type="text" name="nick" maxlength="30" size="13">
         <br /></td>
      </tr>
      <tr>
        <td><font face="Geneva, Arial, Helvetica, sans-serif"><strong>Pass</strong></font></td>
        <td> <input type="text" name="pass" maxlength="10" size="10">
        <br /></td>
      </tr>
      <tr>
        <td><font face="Geneva, Verdana, Georgia"><strong>Flag</strong></font></td>
        <td> <input type="text" name="flags" maxlength="20" size="20">
        <br></td>
      </tr>
      <tr>
        <td height="75"><font size="3" face="Geneva, Verdana, Georgia"><strong>Access Flags</strong></font></td>
        <td><input type="text" name="aflags" maxlength="50" size="50">
        <br /></td>
      </tr>
      <tr>
        <td colspan="2"><input type="submit" value="Register"></td>
      </tr>
    </table>
  </form>
</body>
</html>

and I also created insert_player.php that will make the database connection

<html>
<head>
  <title>Player Registration</title>
</head>
<body>
<h1>Player Registration Results</h1>
<?php
  // create short variable names
  $nick=$HTTP_POST_VARS['nick'];
  $pass=$HTTP_POST_VARS['pass'];
  $flags=$HTTP_POST_VARS['flags'];
  $aflags=$HTTP_POST_VARS['aflags'];

  if (!$nick || !$pass || !$flags || !$aflags)
  {
     echo 'You have not entered all the required details.<br />'
          .'Please go back and try again.';
     exit;
  }

  $nick = addslashes($nick);
  $pass = addslashes($pass);
  $flags = addslashes($flags);
  $aflags = addslashes($aflags);

  @ $db = mysql_pconnect('localhost', 'boyem', 'password', 'boyem_com');

  if (!$db)
  {
     echo 'Error: Could not connect to database.  Please try again later.';
     exit;
  }

  mysql_select_db('boyem_com');
  $query = "insert into amx_regusers values 
            ('".$nick."', '".$pass."', '".$flags."', '".$aflags."')"; 
  $result = mysql_query($query);
  if ($result)
      echo  mysql_affected_rows().' Player inserted into database.'; 
?>

</body>
</html>

first of all in the web page after I click on Register it doesn't tell me the next page "Player inserted into database" it just gives me a emty page with the Player Registration header...

also when I look at my database if it inserted correctly I see nothing inserted.....

what is wrong with my code???

PS in my amx_regusers table there are other fields here is the total fields...

id nick pass flags aflags lastus ipaddr

but id, lastus, and ipaddr are entered atutomatically do you think that is the problem or I have seomthing missing in the code...

thanks in advance

    I think it's your query that is the problem, try this:

    // When you're only inserting some fields, you must specify which ones, like so
    	$query = "INSERT INTO  amx_regusers (nick, pass, flags, aflags) VALUES ('".$nick."', '".$pass."', '".$flags."', '".$aflags."')"; 
    
    $result = mysql_query($query);
    
    /* We use the else so that if the insert failed, it will display the error message that
    MySQL returned. This comes in VERY handy for debugging */
    if ($result) {
    
      		echo  mysql_affected_rows().' Player inserted into database.'; 
      	} else {
    
      		echo "Query Failed: ".mysql_error();
      		echo "<br />".$query;
      	}
    

    Also, unless you're entering their ip address and whatever that other field is somewhere else, it wont be automagically inserted, you'll have to specify that in your query as well.

    Hope that helps,
    Matt

      thanks a lot it worked the only thing that i need is as you said one field didnt insterted which is lastus (whic I didn't want to insert.. I will update the code but how can I enter the data which is carries the current time in this format

      2003-05-14 21:03:27

      ?

        If you're going to update the record later on, then you'll need to use the UPDATE structure in mysql. It's done like this:

        UPDATE amx_regusers SET lastus=NOW() WHERE id='whatever'

        The NOW() function will get the current date and time in the format you wanted.

        If you want to insert all the data at the same time (which is what I would recommend, it's always better to use less queries), you would create your query like this:

        $query = "INSERT INTO  amx_regusers (nick, pass, flags, aflags, lastus, ipaddr) VALUES ('$nick', '$pass', '$flags', '$aflags', NOW(), '".$_SERVER['REMOTE_ADDR']."')";
        

        Also, just noticed then, because you're using double quotes to encapsulate your string when you're setting the $query variable, you don't need to close them when you're just referring to a normal variable.

        This:

        $query = "This is your query, with the value, $value in it";

        will work, rather that doing it like this:

        $query = "This is your query, with the value, ".$value." in it";

        Not really important, but IMO it just makes it neater. If you were using single quotes, it wouldn't because that would make php interpret it as a literal string. 🙂

        Hope that helps,
        Matt

          A good way to find problems with your MySQL queries is to run them on the MySQL server itself. Telnet or phpMyAdmin, or tell your script to output the error.

          MySQL has always helped me find the problem.

            Ok if I want to automatically give a user a ID how would I put that in the code?
            Thanks 😉

              Create a field ID, and give it the properties of 'auto_increment' then do an insert like this...

              INSERT INTO table VALUES ('', 'field1', 'field2');

              You see the first insert part where there are only two quotes? The MySQL database will automatically create an 'ID' for it! and a unique one too!

                Write a Reply...