Jump to content

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


ataria

Recommended Posts

the code is..
[code]<?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" />
[/code]

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

help!!!
*global connects to the DB.
Link to comment
Share on other sites

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

it should be something like:
$chari = @mysql_num_rows(mysql_query([b]"SELECT * FROM users WHERE charid[i]='SOMETHING'[/i]"[/b]));
Link to comment
Share on other sites

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.

[code]SELECT * FROM `users` WHERE `charid`[/code]

will do the exact same as

[code]SELECT * FROM `users`[/code]

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:

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

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:

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

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?
Link to comment
Share on other sites

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

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

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.
Link to comment
Share on other sites

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...
[code]$time = time();
$day = date("z", $time) -252;
[/code]

and, i'm gonna make it insert the day...
alright.
it just inserted the same day. (31)
Link to comment
Share on other sites

Here is the code as it stands right now..


[code]
<?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" />
[/code]
Link to comment
Share on other sites

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:

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

the problem is likely just something simple that i've missed, but i guess we'll do it the long way.
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

alright, try this:

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

we'll tackle your check to see if the charid is there or not after we've sussed this multiple insertion out.
Link to comment
Share on other sites

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:

[code]echo 'rows inserted so far: '.mysql_affected_rows().'<br>';[/code]

see if you get multiple lines saying "rows inserted so far".
Link to comment
Share on other sites

if it is still inserting two rows, but only echoes "rows inserted so far: 1" once, you are running the query somewhere else on the page.  let's see the entire page.

to get rid of it saying "rows inserted so far: 1", just remove the echo line beneath the INSERT statement.
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.