Code not adding to database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
robynprivette
Forum Commoner
Posts: 46
Joined: Sun May 02, 2010 6:22 pm

Code not adding to database

Post by robynprivette »

I run a fansite and I am trying to add users dragon codes to the database (kinda like an adoptable site). Here is the form to add ur dragon's.... I can pull up the user's scroll and it appears that they add but when i look in the db there is nothing, nor on my view page.

Code: Select all

<center><form name="input" action="ScrollRead.php" method="post">
Scroll Name:
<input type="text" name="user" maxlength="50" size="15"/>
<input type="submit" value="Read Scroll" />
</form></center>

<?php
mysql_connect("", "", "") or die(mysql_error());
mysql_select_db("a9634375_dragons") or die(mysql_error());


// Retrieve all the data from the "example" table
$result = mysql_query("SELECT * FROM egg ORDER by rand() LIMIT 100")
or die(mysql_error());  

while($row = mysql_fetch_array( $result )) {
// Print out the contents of the entry 
echo "<a href=\"http://dragcave.net/view/".$row['code'] . " \" target=\"view\"><img src=\"http://dragcave.net/image/".$row['code'] . " .gif\"></a>";
} 
?>
Here is ScrollRead.php

Code: Select all

<script type='text/javascript'>
checked=false;
function checkAll () {
var aa= document.getElementById('lomp');
if (checked == false)
{
checked = true
}
else
{
checked = false
}
for (var i =0; i < aa.elements.length; i++)
{
aa.elements[i].checked = checked;
}
}
</script>

<form name="form1" id="lomp" method="get" action="ScrollReadGet.php"> 

<?php
ini_set("display_errors",true);
$fdd = $_POST['user'];
$user = str_replace(" ","%20","$fdd");
echo ''.$fdd.'';
echo '<input type=hidden value="'.$user.'" name=user>';
$young = unserialize(file_get_contents('http://www.dragcave.net/api/samantha8/serialize/user_young/'.$user.''));

foreach($young['errors'] as $error) {
 if($error[0] == 3) {echo '<center>Sorry, we were unable to locate that scroll.</center>'; return;}
}

foreach($young['errors'] as $error) {
 if($error[0] == 4) {echo '<center>Sorry, this user has no dragons.</center>'; return;}
}


 echo '<table border="0"><tr><td></td><td>Dragon</td><td>Code</td><td>Clicks</td></tr>';
foreach ($young['dragons'] as $key => $value) {
$data = unserialize(file_get_contents('http://www.dragcave.net/api/samantha8/serialize/view/'.$key));
 echo '<tr><td><center><br><input type="checkbox" name="checkboxes[]" value="'.$key.'" border="0"></a></center></td>';
 echo '<td><center><a href="http://www.dragcave.net/view/'.$key.'"><img src="http://www.dragcave.net/image/'.$key.gif.'" border="0"></a></center></td>';
 echo '<td><center>'.$key.'</center></td>';

foreach ($data['clicks'] as $help => $me) {
 echo '<td><center>'.$help.'</center></td></tr>';
}
}


?>
</table>
<table border="0"><tr><td><input type='checkbox' name='checkz' onClick='checkAll();'>Check all/Uncheck all</td></tr></table>
<br><br><input type="submit" name="Submit" value="Add Dragons">
And here is ScrollReadGet.php which adds the dragon codes to the db

Code: Select all

<?php
ini_set("display_errors",true);
$link = mysql_connect('', '', '', true); if (!$link) {      die('Could not connect: ' . mysql_error()); }
mysql_select_db("a9634375_dragons") or die(mysql_error());
$ids=$_GET['checkboxes'];

$user = $_GET['user'];
$young = unserialize(file_get_contents('http://www.dragcave.net/api/samantha8/serialize/user/'.$user.''));

foreach ($young['dragons'] as $key => $value) {
$querya = "DELETE FROM hatch WHERE code='$key'" or die(mysql_error());
$queryb = "DELETE FROM er WHERE code='$key'" or die(mysql_error());
$queryc = "DELETE FROM egg WHERE code='$key'" or die(mysql_error());
mysql_query($querya);
mysql_query($queryb);
mysql_query($queryc);
}
echo '<br>Dragons updated.<br>';
 echo 'Dragons now entered:<br>';
foreach ($ids as $hey) {
echo '<br><a href="http://www.dragcave.net/view/'.$hey.'" target="frame1"><img src="http://www.dragcave.net/image/'.$hey.'.gif" border="0"></a>&nbsp;';

$data = unserialize(file_get_contents('http://www.dragcave.net/api/samantha8/serialize/'.$hey.''));
$hrsleft = $data['dragons'][$hey]['hoursleft'];
$hatch = $data['dragons'][$hey]['hatch'];

if ($hrsleft < 96) {
$query3 = "INSERT INTO er(code) VALUES('$hey')" or die(mysql_error());
mysql_query($query3);
echo 'Dragon has been entered into the ER.<br>';
}
else
{
if  (! $hatch) {
$query4 = "INSERT INTO egg(code) VALUES('$hey')" or die(mysql_error());
mysql_query($query4);
echo 'Egg has been entered into the Nest.<br>';
}
else
{
$query2 = "INSERT INTO hatch(code) VALUES('$hey')" or die(mysql_error());
mysql_query($query2);
echo 'Hatchling has been entered into the Nursery.<br>';
}
}
}

?>
Any help is appreciated.
Thanks :cry:
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Code not adding to database

Post by Jonah Bron »

Put this:

Code: Select all

error_reporting(E_ALL|E_STRICT);
right after

Code: Select all

ini_set("display_errors",true);
in ScrollReadGet.php. Then, put

Code: Select all

or die('Error on line ' . __LINE__ . ":\n" . mysql_error());
after the end of every mysql_query() so it looks like this:

Code: Select all

mysql_query($query) or die('Error on line ' . __LINE__ . ":\n" . mysql_error());
This will help with debugging. Try it again and post the output here.
User avatar
robynprivette
Forum Commoner
Posts: 46
Joined: Sun May 02, 2010 6:22 pm

Re: Code not adding to database

Post by robynprivette »

After entering my scroll name into the form I get this:

Code: Select all

silverDragonTears
Dragon	Code	Clicks



aA7V
PHP Error Message

Warning: Invalid argument supplied for foreach() in /home/a9634375/public_html/ScrollRead.php on line 46

Free Web Hosting



hjkP

PHP Error Message

Warning: Invalid argument supplied for foreach() in /home/a9634375/public_html/ScrollRead.php on line 46

Free Web Hosting



B2Z7

PHP Error Message

Warning: Invalid argument supplied for foreach() in /home/a9634375/public_html/ScrollRead.php on line 46

Free Web Hosting



2gPZ

PHP Error Message

Warning: Invalid argument supplied for foreach() in /home/a9634375/public_html/ScrollRead.php on line 46

Free Web Hosting



G57h

PHP Error Message

Warning: Invalid argument supplied for foreach() in /home/a9634375/public_html/ScrollRead.php on line 46

Free Web Hosting



GsW6

PHP Error Message

Warning: Invalid argument supplied for foreach() in /home/a9634375/public_html/ScrollRead.php on line 46

Free Web Hosting
Check all/Uncheck all
So it lists all the dragon codes on my scroll with a check box to add them to the db, but under each code it has the warning. Then I click all checkboxes to add the codes to the db and it says Error on line 30: Query execution was interrupted

I did it again and it said this
Notice: Undefined index: dragons in /home/a9634375/public_html/ScrollReadGet.php on line 38
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Code not adding to database

Post by Jonah Bron »

Put this right before $ids = $_GET['checkboxes']; :

Code: Select all

if (!isset($_GET['checkboxes'])) {
    echo 'Ghhhaaaaa!  It\'s not there!';
}
User avatar
robynprivette
Forum Commoner
Posts: 46
Joined: Sun May 02, 2010 6:22 pm

Re: Code not adding to database

Post by robynprivette »

nothing still... not even an error message
User avatar
robynprivette
Forum Commoner
Posts: 46
Joined: Sun May 02, 2010 6:22 pm

Re: Code not adding to database

Post by robynprivette »

scratch that.... it still says

Notice: Undefined index: dragons in /home/a9634375/public_html/ScrollReadGet.php on line 41
which is this line

Code: Select all

$hatch = $data['dragons'][$hey]['hatch'];
under the first code entered and the rest say they were entered into the db. but they aren't.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Code not adding to database

Post by Jonah Bron »

Does it say "Ghhhaaa! It's not there!"? If it doesn't, try this:

Code: Select all

if (!is_array($_GET['checkboxes'])) {
    echo 'Ghhhaaaaa!  It\'s not an array!';
}
User avatar
robynprivette
Forum Commoner
Posts: 46
Joined: Sun May 02, 2010 6:22 pm

Re: Code not adding to database

Post by robynprivette »

nope didn't say anything but this
Notice: Undefined index: dragons in /home/a9634375/public_html/ScrollReadGet.php on line 41
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Code not adding to database

Post by Jonah Bron »

Put

Code: Select all

print_r($data);
Right before that line.
User avatar
robynprivette
Forum Commoner
Posts: 46
Joined: Sun May 02, 2010 6:22 pm

Re: Code not adding to database

Post by robynprivette »

so i removed the numbers from this part...

Code: Select all

$query4="INSERT INTO er (code) VALUES ('$hey')" or die(mysql_error());
mysql_query($query4);
echo 'Dragon has been entered into the ER.<br>';
i removed all the numbers and now all the codes are being submitted to the ER table.... it must be something with the query.... does that help?
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Code not adding to database

Post by mikosiko »

Jonah suggested you to make this change in his first post

Code: Select all

mysql_query($query4) or die('Error on line ' . __LINE__ . ":\n" . mysql_error());
you still having your old lines

Code: Select all

$query4="INSERT INTO er (code) VALUES ('$hey')" or die(mysql_error());
mysql_query($query4);
echo 'Dragon has been entered into the ER.<br>';
make the suggested changes and look if errors are show after that... assuming that you implemented the others Jonah's suggested changes
Post Reply