Jump to content

Failed to insert info into databse


chilllax

Recommended Posts

What I want to do is collect a gamers username and their email address send that into my database and then display all of the gamers who have signed up on another page using the database. I made the html form that was fairly simple. My problem is getting that information into the database. When I run the scrip that is supposed to put the information into the databse there is never an error but the information never gets put into the databse. Heres the code of the script (thanks for any help):

[code]
<html>
<head>
<title>Your Entry</title>
</head>
<table align="center">
<tr>
  <td>
<a href="http://www.officalhalo.com">Home</a> |

<a href="/tournament/FFA.htm">FFA</a> |

<a href="/tournament/signup.htm">Sign Up</a> |

<a href="/tournament/results.htm">Results</a>
</td>
</tr>
</table>
<body>
<h3 align="center">Your Entry</h3>
<?php
  $gamertag=$_POST['gamertag'];
  $eaddress=$_POST['eaddress'];
  
  if (!$gamertag || !$eaddress)
  {
      echo 'You have not entered all of the required information.<br />'
           .'Please go back and try again.';
      exit;
  }

  @ $db = new mysqli('localhost', 'chilllax_chillla', 'red123', 'chilllax_tournament');
  
if (mysqli_connect_errno())
{
    echo 'Error: Could not connect to database. Please try again later.';
    exit;
}

$query = "INSERT INTO " . chilllax_tournament ." ffa (gamertag, eaddress)
              VALUES ("' $gamertag . '", "' . $eaddress . '")";

$result = $db->query($query);
if ($result)
    echo $db->affected_rows.' Entry submitted into database.';
        
$db->close();

?>
</body>
</html>
[/code]
Link to comment
Share on other sites

You're not displaying the error. Display the error so you get an idea of where the problem lies.

[code]
if ($result)
    echo $db->affected_rows.' Entry submitted into database.';
else {
    echo 'Could not insert. Error: ', mysqli_error();
    exit;  //
}
[/code]

You'll probably find that you have an SQL syntax error around " ffa". You have a space before the "ffa" which won't work. I'm assuming that "chilllax_tournament" is a constant defined with the first part of your table name. Once you fix that, you have another problem with quotes in the $query. It should look something like this:

[code]
define ('chilllax_tournament', 'MyTableNamePrefix_');
.
.
.
$query = 'INSERT INTO ' . chilllax_tournament . "ffa (gamertag, eaddress) VALUES ('$gamertag', '$eaddress')";
[/code]

That will make the SQL look something like this:

[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']INSERT[/span] [color=green]INTO[/color] [color=orange]MyTableNamePrefix_ffa[/color] (gamertag, eaddress) VALUES ([color=red]'some_value'[/color], [color=red]'some_value'[/color])
[!--sql2--][/div][!--sql3--]

Link to comment
Share on other sites

I added the error in, that was a smart thing to do I didnt think of that thank you. Unfortunatly there is still no error only blank page when I run the script. So I am guessing my error lies in the bit of code that you told me to replace with this

[code]define ('chilllax_tournament', 'MyTableNamePrefix_');
.
.
.
$query = 'INSERT INTO ' . chilllax_tournament . "ffa (gamertag, eaddress) VALUES ('$gamertag', '$eaddress')";
[/code]

Just to be sure, my table name is ffa and chilllax_tournament is the name of my databse. I copied your code verbatum but I dont think the periods seperating the first line with the second should be there correct? I tried to run the script with them and Parse error: parse error, unexpected '.' in /home/chilllax/public_html/tournament/yourentry.php on line 42 that error came up. Withouth the periods there was only a blank page. Thanks for all of your help.
Link to comment
Share on other sites

If that's your database name, then it's optional whether you specify it in the query (since you already selected the database during the connection).

If you use the database, there must be a period before the table name. Example:

$query = "INSERT INTO chilllax_tournament.ffa (gamertag, eaddress) VALUES ('$gamertag', '$eaddress')";

or simply this way will work too:

$query = "INSERT INTO ffa (gamertag, eaddress) VALUES ('$gamertag', '$eaddress')";


See manual:
[a href=\"http://dev.mysql.com/doc/refman/4.1/en/insert.html\" target=\"_blank\"]http://dev.mysql.com/doc/refman/4.1/en/insert.html[/a]


While debugging, add this at the top of your script:

// Add to make sure you see PHP errors/warnings/notices
ini_set('display_errors', '1');
error_reporting(E_ALL);

Link to comment
Share on other sites

Here is the code after the changes you told me to make. Unfortunatly it still isnt submitting the information into the database. Could there be something wrong with the settings of my columns that wouldnt allow information to be placed in it? If you need to know anything else just ask.
[code]<html>
<head>
<title>Your Entry</title>
</head>
<table align="center">
<tr>
  <td>
<a href="http://www.officalhalo.com">Home</a> |

<a href="/tournament/FFA.htm">FFA</a> |

<a href="/tournament/signup.htm">Sign Up</a> |

<a href="/tournament/results.htm">Results</a> |

<a href="/tournament/participants.php">Participants</a>
</td>
</tr>
</table>
<body>
<h3 align="center">Your Entry</h3>
<?php
  // Add to make sure you see PHP errors/warnings/notices
ini_set('display_errors', '1');
error_reporting(E_ALL);
  
  $gamertag=$_POST['gamertag'];
  $eaddress=$_POST['eaddress'];
  
  if (!$gamertag || !$eaddress)
  {
      echo 'You have not entered all of the required information.<br />'
           .'Please go back and try again.';
      exit;
  }

  @ $db = new mysqli('localhost', 'chilllax_chillla', 'red123', 'chilllax_tournament');
  
if (mysqli_connect_errno())
{
    echo 'Error: Could not connect to database. Please try again later.';
    exit;
}

$query = "INSERT INTO ffa (gamertag, eaddress) VALUES ('$gamertag', '$eaddress')";

$result = $db->query($query);
if ($result)
    echo $db->affected_rows.' Entry submitted into database.';
else {
    echo 'Could not insert. Error: ', mysqli_error();
    exit;  //
}
?>
</body>
</html> [/code]
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.