Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 132,694 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,310 people online right now. Registration is fast and FREE... Join Now!




Writing 2 records to table

 
Reply to this topicStart new topic

Writing 2 records to table

rjolitz
post 26 May, 2008 - 01:29 PM
Post #1


D.I.C Head

Group Icon
Joined: 17 May, 2008
Posts: 86



Thanked 1 times

Dream Kudos: 75
My Contributions


Hello.

For some reason I can't seem to fathom, once this code checks that no one else is using the same user name, vessel name, and email address, it writes the record to the table.

However, it is writing the record TWICE. I have to be honest, I have absolutely no clue why. I have stared at this most of last night and a good chunk of today.

Anyone see anything that might explain this?


I've left out the multitude of variables and the setting of their values. Some are from $_POST and others are set directly.

CODE

if ($password != $confirm)
  {  
     print("<span style='color:red'>ERROR: Passwords do not match! </span>");
  }

   $mysqli = new mysqli($host, $dbuser, $pw, $db);
  
    if ( $mysqli->errno)
   {
      printf("Unable to connect to the database: <br />  %s", $mysqli->error);
      exit();
    }

$query="SELECT *  FROM `users` WHERE `user_name` = CONVERT(_utf8 '$user' USING latin1) COLLATE latin1_swedish_ci";

$mysqli->query($query);
$result=$mysqli->query($query);

if (!$result) {
  printf("Invalid query: %s", $mysqli->error);
exit();
}

if ($result->num_rows > 0)
{
    printf("Sorry but character name %s is already taken.", $user);
    exit();
    }

;

$query=" SELECT * FROM users WHERE `vessel_name` = '$vessel_name'";

$mysqli->query($query);
$result=$mysqli->query($query, MYSQLI_STORE_RESULT);

if (!$result) {
  printf("Invalid query: %s", $mysqli->error);
exit();
}

if ($result->num_rows > 0)
{
    printf("Sorry but vessel name %s is already taken.", $vessel_name);
    exit();
    }


$query=" SELECT * FROM users WHERE `email` = '$email'";

$mysqli->query($query);
$result=$mysqli->query($query, MYSQLI_STORE_RESULT);

if (!$result) {
  printf("Invalid query: %s", $mysqli->error);
exit();
}

if ($result->num_rows > 0)
{
    printf("Sorry but a player is already using %s as an email address.", $email);
    exit();
    }
    
$query="INSERT INTO users (`user_name`, `password`, `rank`, `affiliation`, `admin`, `subscription`, `beta_test`, `email`, `country`, `points`, `wins`, `losses`, `vessel_name`, `pos_x`, `pos_y`, `class`, `point_value`, `shield`, `max_shield`, `heavy_weapon`, `max_heavy_weapon`, `main_phaser`, `max_main_phaser`, `defensive_phaser`, `max_defensive_phaser`, `hull`, `max_hull`, `lab`, `max_lab`, `trans`, `max_trans`, `bridge`, `max_bridge`, `sub_light_engines`, `max_sub_light_engines`, `auxillary_power`, `max_auxillary_power`, `battery_power`, `max_battery_power`, `small_craft`, `max_small_craft`, `extra_damage`, `max_extra_damage`, `light_engines`, `max_light_engines`, `NOT_USED`, `NOT_USED1`, `NOT_USED2`, `NOT_USED3`, `NOT_USED4`, `NOT_USED5`, `NOT_USED6`, `NOT_USED7`, `NOT_USED8`, `NOT_USED9`, `NOT_USED10`, `NOT_USED11`)  VALUES ('$user',(md5('$password')),'$rank', '$faction','$admin','$subscription','$beta_test','$email','$country','$points','$wins','$losses','$vessel_name', '$pos_x','$pos_y','$class','$point_value','$shield','$max_shield','$heavy_weapon','$max_heavy_weapon','$main_phaser','$max_main_phaser','$defensive_phaser','$max_defensive_phaser','$hull','$max_hull','$lab','$max_lab','$trans','$max_trans','$bridge','$max_bridge','$sub_light_engines','$max_sub_light_engines','$auxillary_power','$max_auxillary_power','$battery_power','$max_battery_power','$small_craft','$max_small_craft','$extra_damage','$max_extra_damage','$light_engines','$max_light_engines','$NOT_USED','$NOT_USED1','$NOT_USED2','$NOT_USED3','$NOT_USED4','$NOT_USED5','$NOT_USED6','$NOT_USED7','$NOT_USED8','$NOT_USED9','$NOT_USED10','$NOT_USED11');";

$mysqli->query($query);

$result=$mysqli->query($query);

if (!$result) {
  printf("Invalid query: %s", $mysqli->error);
exit();
}



Code after this point sends a confirmation email and then prints out a fun YOU ARE GIVEN COMMAND message to the screen.

Thanks for any help.
User is offlineProfile CardPM

Go to the top of the page

girasquid
post 26 May, 2008 - 01:40 PM
Post #2


Barbarbar

Group Icon
Joined: 3 Oct, 2006
Posts: 1,256



Thanked 14 times

Dream Kudos: 650
My Contributions


It's because you call $mysql->query() twice at the end:
CODE

$mysqli->query($query);

$result=$mysqli->query($query);


I think the reason that you aren't noticing this is because that's the only query that does something to the database - all of the others are just SELECT statements, so running them twice is no big deal(although kind of pointless).

This post has been edited by girasquid: 26 May, 2008 - 01:41 PM
User is offlineProfile CardPM

Go to the top of the page

rjolitz
post 26 May, 2008 - 02:22 PM
Post #3


D.I.C Head

Group Icon
Joined: 17 May, 2008
Posts: 86



Thanked 1 times

Dream Kudos: 75
My Contributions


QUOTE(girasquid @ 26 May, 2008 - 02:40 PM) *

It's because you call $mysql->query() twice at the end:
CODE

$mysqli->query($query);

$result=$mysqli->query($query);


I think the reason that you aren't noticing this is because that's the only query that does something to the database - all of the others are just SELECT statements, so running them twice is no big deal(although kind of pointless).


so just running $result=$mysqli->query($query); will get the job done.

that makes sense, and a stupid newbie mistake to boot.

Thanks!

User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 07:44AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month