Jump to content


Photo

Failed to insert info into databse


  • Please log in to reply
4 replies to this topic

#1 chilllax

chilllax
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 15 April 2006 - 11:08 PM

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):

<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>


#2 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 16 April 2006 - 12:15 AM

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

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

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:

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

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] INTO MyTableNamePrefix_ffa (gamertag, eaddress) VALUES ('some_value', 'some_value')
[!--sql2--][/div][!--sql3--]



#3 chilllax

chilllax
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 16 April 2006 - 02:55 PM

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

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

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.

#4 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 16 April 2006 - 03:08 PM

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);



#5 chilllax

chilllax
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 16 April 2006 - 03:43 PM

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.
<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>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users