Jump to content


Photo

Making it only make 1 row in the database...


  • Please log in to reply
20 replies to this topic

#1 ataria

ataria
  • Members
  • PipPipPip
  • Advanced Member
  • 179 posts

Posted 11 October 2006 - 10:20 PM

the code is..
<?php
include ("global.php");

$chari = @mysql_num_rows(mysql_query("SELECT * FROM `users` WHERE `charid`"));
if ($chari == 1) { 
echo "TEST.";
}
else {
$sql = @mysql_query("INSERT INTO `characters` ( `id`, `username` ) VALUES ('', '{$_COOKIE['uid']}')" );
echo "<center>
<table cellspacing=3 cellpadding=3 width=300>
<tr>
<td class=bar colspan=2><center>
<font class=white><b>Uh-Oh</b></font></center>
</td>
</tr>
<tr>
<td>
You don't have a character! In order to play the site, you need one... so, we gave you one! <br><br>
<img src=>
<br>
<br>
This is your <b>one</b> and <b>only</b> character. You will be able to make it go to school and become smarter, get it a job, change it's appearance, and, much more! Remember; treat it as you treat yourself, hence, lay off the drugs...
<br><br><br><br><br><br><br><br>
</td>
</tr>
</table>
</center>

";


}

?>
<link rel="stylesheet" type="text/css"
href="mystyle.css" />

it inserts 2 rows..
When i limit it to '1', it doesn't insert a row...

help!!!
*global connects to the DB.


#2 Jocka

Jocka
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationDallas, Texas

Posted 11 October 2006 - 10:23 PM

well for one you didn't finish your SQL.
You have:
$chari = @mysql_num_rows(mysql_query("SELECT * FROM `users` WHERE `charid`"));

it should be something like:
$chari = @mysql_num_rows(mysql_query("SELECT * FROM users WHERE charid='SOMETHING'"));

#3 ataria

ataria
  • Members
  • PipPipPip
  • Advanced Member
  • 179 posts

Posted 11 October 2006 - 10:25 PM

yeah.
but, i just want to retrieve /that/ information.
I'm not trying to make it equal anything.


#4 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 11 October 2006 - 10:33 PM

i have no idea what you mean about just wanting to retrieve that information.  when placing an equal sign in a WHERE clause, you're not assigning charid to a value, you're comparing it to one.

SELECT * FROM `users` WHERE `charid`

will do the exact same as

SELECT * FROM `users`

because the WHERE clause always evaluates to true.

next, get out of the habit of placing @ in front of all of your mysql_ function calls.  it suppresses errors that might arise, and errors are key in debugging your scripts.  try:

$sql = mysql_query("INSERT INTO `characters` ( `id`, `username` ) VALUES ('', '{$_COOKIE['uid']}')" ) or die(mysql_error());

finally, if your `id` column is an auto_increment primary key, you don't need to specify it in INSERT commands since MySQL will define it itself:

INSERT INTO `characters` ( `username` ) VALUES ('{$_COOKIE['uid']}')

as for why it's inserting two rows exactly, i don't know.  i have a feeling you may be running the page twice somehow, or are mistaken as to it inserting two rows on this page.  are you running this query elsewhere?

#5 ataria

ataria
  • Members
  • PipPipPip
  • Advanced Member
  • 179 posts

Posted 11 October 2006 - 10:37 PM

About the 'where' thing..
the charid, is only 0 or 1..
so, if it equals 1 it shows test..
if it equals 0, it shows the else..
it works...
(yes, i jsut did a test)

--

the query is not run elsewhere... =/

#6 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 11 October 2006 - 10:46 PM

ok, well that explains why the WHERE clause is working then.

have you actually attempted my suggestions on the INSERT command (die clause and removing id)?

#7 ataria

ataria
  • Members
  • PipPipPip
  • Advanced Member
  • 179 posts

Posted 11 October 2006 - 10:52 PM

yeah. I tried it..
it still doubles.

#8 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 11 October 2006 - 10:57 PM

in that case, perhaps try getting more information on each insert that is happening by inserting runtime information (if your data type will allow it):

"INSERT INTO `characters` ( `username` ) VALUES ('{$_SERVER['PHP_SELF']}".time()."')"

look at the rows and check whether the path and the timestamp show up for both, and if so, whether either of the values differ.

#9 ataria

ataria
  • Members
  • PipPipPip
  • Advanced Member
  • 179 posts

Posted 11 October 2006 - 11:00 PM

they don't show up..
and, i added ');' to the end to make it work...

#10 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 11 October 2006 - 11:02 PM

what is your PHP code for the INSERT command as it stands right now?

#11 ataria

ataria
  • Members
  • PipPipPip
  • Advanced Member
  • 179 posts

Posted 11 October 2006 - 11:04 PM

Well... they show a '0'. (your insert)


I am gonna try something..
here it what i am gonna do.
(the site is measured in days...)
in the global, i have the time thing which is...
$time = time();
$day = date("z", $time) -252;

and, i'm gonna make it insert the day...
alright.
it just inserted the same day. (31)

#12 ataria

ataria
  • Members
  • PipPipPip
  • Advanced Member
  • 179 posts

Posted 11 October 2006 - 11:05 PM

Wtf. Now it's creating 4.... ugh.

#13 ataria

ataria
  • Members
  • PipPipPip
  • Advanced Member
  • 179 posts

Posted 11 October 2006 - 11:07 PM

Here is the code as it stands right now..


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

$chari = @mysql_num_rows(mysql_query("SELECT * FROM `users` WHERE `charid`"));
if ($chari == 1) { 
echo "TEST.";
}
else {
$sql = mysql_query("INSERT INTO `characters` ( `username` ) VALUES ('{$_SERVER['PHP_SELF']}".time()."')");
echo "<center>
<table cellspacing=3 cellpadding=3 width=300>
<tr>
<td class=bar colspan=2><center>
<font class=white><b>Uh-Oh</b></font></center>
</td>
</tr>
<tr>
<td>
You don't have a character! In order to play the site, you need one... so, we gave you one! <br><br>
<img src=>
<br>
<br>
This is your <b>one</b> and <b>only</b> character. You will be able to make it go to school and become smarter, get it a job, change it's appearance, and, much more! Remember; treat it as you treat yourself, hence, lay off the drugs...
<br><br><br><br><br><br><br><br>
</td>
</tr>
</table>
</center>

";


}

?>
<link rel="stylesheet" type="text/css"
href="mystyle.css" />


#14 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 11 October 2006 - 11:11 PM

first of all, are you getting the "we created one for you" message?  second, how are you using this page?  does it post to itself, do you run it manually, or what?  third, try changing your insert line to the following two lines:

$value = 'something@'.time();
$sql = mysql_query("INSERT INTO `characters` ( `username` ) VALUES ('$value')") or die(mysql_error());

the problem is likely just something simple that i've missed, but i guess we'll do it the long way.

#15 ataria

ataria
  • Members
  • PipPipPip
  • Advanced Member
  • 179 posts

Posted 11 October 2006 - 11:15 PM

Alright.
the page is supposed to be..

'If user has a character (charid=1), display the stats. (didn't get that far),
else, automatically create one, then change the 'charid to 1', (didn't get that far, b//c it would be annoying to have to keep changing the charid to '0' to do tests)..
and, since the character won't create properly, it's messing me up... big time.

----

I changed it to the code you just posted, and, it didn't even create a row.



#16 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 11 October 2006 - 11:29 PM

alright, try this:

$sql = mysql_query("INSERT INTO `characters` ( `username` ) VALUES ('{$_COOKIE['uid']}')") or die(mysql_error());
exit(mysql_affected_rows());

we'll tackle your check to see if the charid is there or not after we've sussed this multiple insertion out.

#17 ataria

ataria
  • Members
  • PipPipPip
  • Advanced Member
  • 179 posts

Posted 11 October 2006 - 11:31 PM

WOW. It only did one row.. but, it made the page go white :(

#18 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 11 October 2006 - 11:34 PM

that's what it's meant to do - it is supposed to exit immediately after the query is run, echoing how many rows were inserted.  did you see a '1' in the browser (probably the only thing on the page)?

now try changing the exit(mysql_affected_rows()) to:

echo 'rows inserted so far: '.mysql_affected_rows().'<br>';

see if you get multiple lines saying "rows inserted so far".

#19 ataria

ataria
  • Members
  • PipPipPip
  • Advanced Member
  • 179 posts

Posted 11 October 2006 - 11:36 PM

It shows...
"rows inserted so far: 1"
then my crap on the page.
any way to hide that?

#20 ataria

ataria
  • Members
  • PipPipPip
  • Advanced Member
  • 179 posts

Posted 11 October 2006 - 11:38 PM

ugh. It is still inserting 2 rows....




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users