I'm not exactly sure what the problem is with my code, but hopefully you all can help me figure it out.

There are four important files involved. We're making a text RPG written in PHP, and at the moment, I'm working on the registration file.

We've got header.php, which includes everything that we need to log into the database, included in our files with the include function.

<?php

echo "<html><body>
<h4>Character Setup</h4>
<form action='characterupdate.php' method='post'> 
Name: <input name='name' type='text' />
<br>
Password: <input name='password' type='password' />
<br>
Class: <select name='class'> 
<option>N/A</option>
</select>
<br>
<input type='submit' />
</form>
</body></html>";
?>

This is our charactercreation.php. It's supposed to post its data to charaacterupdate.php, which then inserts all data into the database.

<?php
include("header.php");

$name = $_POST['name'];
$password = $_POST['password'];
$password = md5($password);
$class = 0;

mysql_put("character", "id,name,password,class", ",".$name.",".$password.",".$class);
include("footer.php");
?>

This is our characterupdate.php. We created a function that's supposed to make mysql_query(INSERT INTO) easier (which is that mysql_put function up there), but I don't know if that's the problem or not. We've also tried using INSERT INTO, but that didn't work, either. I think the problem may be with the method that I'm using the POST data, but I have absolutely no clue.

    1. You haven't told us what's wrong... is the data not being inserted into the DB, I'm guessing? What is happening visually... when you submit the form, what happens? Is error_reporting set to E_ALL and display_errors turned On? If so, is PHP giving any error messages? We'll need this basic set of information before we can begin diving further into the problem.

    2. Well, I'd love to diagnose your mysql_put() function and see if it's got any coding faults... but you know show us the function declaration for it! Show us this function's declaration code so we can see if it's inserting correctly or not.

      Ah, sorry. Yes, no data is being inserted into the DB. Absolutely nothing is shown, and I'm pretty sure error_reporting is set to E_ALL. And yes, display_errors is turned on. The only thing I see upon load of characterupdate.php is a blank page.

      function mysql_put($table,$fields,$data) // Put data into MySQL field.
      {
      	$query = "INSERT INTO `" . $table . "` (" . $fields . ") VALUES(" . $data . ")";
      	mysql_query($query);
      }

      Here's the code for mysql_put. I didn't write it, so I have no clue if it's right. A friend of mine wrote it, and he's the one that's working on this project with me.

        First thing I would say is since we're having issues, we need to start outputting debugging information. For example, let's get MySQL to spew its guts if your query has any problems with it. How? Change this:

        mysql_query($query);

        to something like:

        mysql_query($query) or die('MySQL error - ' . mysql_error() . '<hr/>Query: ' . $query);

        Now before we do that, let me point out three errors/suggestions in regards to your query:

        1. In your fields list when you use the mysql_put() function (back in charactercreation.php), you don't delimit the fields in backticks ( ). Now, normally this doesn't cause a problem as MySQL is smart enough to figure out you're trying to name off columns/tables/databases... EXCEPT when the name of what you're referencing is a special keyword. For safety's sake, a personal preference of mine is that I [b]always[/b] use backticks when applicable. Example:
          [code]INSERT INTO
          myTable(column1,column2`) VALUES ('value1', 'value2')
          instead of:
          INSERT INTO myTable (column1, column2) VALUES ('value1', 'value2')[/code]

        2. You don't have quotes around your data (the stuff inside the VALUES (...) set). All strings must be quoted with single quotes... otherwise SQL thinks you're trying to name off column names. The only time you wouldn't use a quote is if you're specifying a numeric value.

        3. You list the 'id' column in your field list, yet then you leave it's value blank in the VALUES () set. I'm guessing this is because you want SQL to generate an id for you. If so, there's no reason to list the id column (or a null value for it) in your query at all.

        EDIT: Oh yeah, by the way: When posting PHP code, please use the board's [PHP][/PHP] bbcode tags - they make the code much, much easier to read (as well as aiding the diagnosis of syntax errors). You should edit your post now and add these bbcode tags to help others in reading/analyzing your code.

          Okay, I did what you said, and I got the following error message back.

          MySQL error - Unknown column 'asdf' in 'field list'
          
          Query: INSERT INTO `character` (`name`,`password`,`class`) VALUES(asdf , 912ec803b2ce49e4a541068d495ab570 , 0)

          This was after testing the files and entering "asdf" into both Name and Password fields. I also changed the mysql_put() part of my PHP code in characterupdate.php to the following.

          mysql_put(character, "`name`,`password`,`class`",  $name . " , " .$password. " , " .$class);
          include("footer.php");

          It looks like it's taking the first part of the third argument as the second argument if it's putting "asdf" as a column name. I'm not sure, though. I still can't figure it out. 🙁

            As Brad told you, you need to put quotes around your string values.

              I would, if I knew what they were. >_>
              This is newbies section, right? This is pretty much one of the first codes I've ever worked on. D:

                mysql_put("character", "id,name,password,class", ",'".$name."','".$password."','".$class."'");
                
                  Roger Ramjet wrote:
                  mysql_put("character", "id,name,password,class", ",'".$name."','".$password."','".$class."'");[/QUOTE]
                  
                  That's almost right, but I had to remove ID from both the second and third arguments.
                  
                  It works now. Thanks for your help Roger and Brad. :D
                    Write a Reply...