Запис в няколко таблици

Dizasterr

Registered
Здравейте! Написах си този код:
Код:
<?php
   require_once('../connection.php');
   mysql_query("SET NAMES cp1251");
   
   if (isset($_POST['Submit'])) {
	
	$user=strip_tags($_POST['potrebitel']);
    $user=addslashes($user);
     
    $pass= md5($_POST['pass']);
	$pass2= md5($_POST['pass2']);
	  
    $email=strip_tags($_POST['email']);
    $email=addslashes($email);
	
	$race = $_POST['race'];
	
	$gold = '10000';
	$gas = '10000';
	$metal = '10000';
	$hangar = '20';
	$hangarlvl = '1';
	
	if(empty($user) || empty($pass) || empty($pass2) || empty($email) || empty($race)) {
	die ("<strong style='color: #FF0000;'>Не сте попълнили всички полета</strong>"); 
	}
	
	$check=mysql_query("SELECT * FROM `users` WHERE `user` = '$user'");
	$user_exist=mysql_num_rows($check);
	if($user_exist > 0) {
		die("Потребителското име е заето!");
		}else{
	}
	
	$check_email=mysql_query("SELECT * FROM `users` WHERE `email` = '$email'");
	$email_exist=mysql_num_rows($check_email);
	if($user_exist > 0) {
		die("Тази поща е вече използвана!");
		}else{
	}
	
	if($pass!=$pass2) {	
	die ("Не сте въвели правилно втората парола!");
	}
	
	if(!ereg("@",$email) ) {
	die("<strong style='color: #FF0000;'>Невалиден e - mail</strong>");
	}else{
	}
	
	$insert_sql = "INSERT INTO `users`(user, password, email, race)
                  VALUES ('$user', '$pass', '$email', '$race')" or die(mysql_error());
			  
      $insert_result = mysql_query($insert_sql);
	  if ($insert_result) {
	$select=mysql_query("SELECT * FROM `users`") or die(mysql_error());
	$r=mysql_fetch_assoc($select);
	$id=$r['id'];
	$insert=mysql_query("INSERT INTO `resources` (user_id,gold,gas,metal) VALUES ('$id','$gold','$gas','$metal')") or die(mysql_error());
	$buildings=mysql_query("INSERT INTO `buildings` (hangar,hangarlvl) VALUES ('$hangar','$hangarlvl')") or die(mysql_error());
			echo "<strong style='color: #FFF;'>Потребителя е регистриран успешно!</strong>";
		}else{
			echo "<strong style='color: #FF0000;'>Грешка при регистрация!</strong>";
			}
	  }
	
?>
<form method="post" action="<?php $_SERVER ['PHP_SELF'] ?>">
<table align="center">
<tr align="center">
<b style="color: #FFF;"><center>Ако нямате регистрация можете да си направите безплатна от тук:</center></b><br/>
<td style="color: #FFF;">Потребителско име*</td>
<td><input id="user" name="potrebitel" type="text" maxlength="20" onblur="checkUsername(this.value)" style="opacity:0.6;filter:alpha(opacity=60)" onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100"onmouseout="this.style.opacity=0.6;this.filters.alpha.opacity=60" /></td>
<td width="100px"><div style="color: #FFF;" class="message" id="message"></div></td>
</tr>
<tr>
<td style="color: #FFF;">Парола*</td>
<td><input id="pass" name="pass" type="password" maxlength="15" style="opacity:0.6;filter:alpha(opacity=60)" onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100"onmouseout="this.style.opacity=0.6;this.filters.alpha.opacity=60" ></td>
</tr>
<tr>
<td style="color: #FFF;">Повторете паролата*</td>
<td><input id="pass" name="pass2" type="password" maxlength="15" style="opacity:0.6;filter:alpha(opacity=60)" onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100"onmouseout="this.style.opacity=0.6;this.filters.alpha.opacity=60" ></td>
</tr>
<tr>
<td style="color: #FFF;">E - Mail*</td>
<td><input id="user" name="email" type="text" maxlength="20" style="opacity:0.6;filter:alpha(opacity=60)" onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100"onmouseout="this.style.opacity=0.6;this.filters.alpha.opacity=60" ></td>
</tr>
<tr>
<tr>
<td style="color: #FFF;">Код*</td>
<td><input id="user" name="captcha" type="text" maxlength="20" style="opacity:0.6;filter:alpha(opacity=60)" onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100"onmouseout="this.style.opacity=0.6;this.filters.alpha.opacity=60" ></td>
<td><?php echo $captcha; ?></td>
</tr>
<tr>
<td style="color: #FFF;">Раса*</td>
<td>
<select id="user" name="race">
<option id="select">Земляни</option>
<option id="select">Марсианци</option>
</select></td>

</tr>
<tr>
            <tr>
              <td> </td>
              <td valign="top"><input type="submit" name="Submit" id="login" value="Регистрация" style="opacity:0.8;filter:alpha(opacity=80)" onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100" onmouseout="this.style.opacity=0.8;this.filters.alpha.opacity=80"/></td>
            </tr>
</table>
</form>
Искам да записва в няколко таблици, ид - то на потребителя и съответните стойности, но записва само в едната, но не и в другата. Къде е грешката ?
 
Код:
<?php
   require_once('../connection.php');
   mysql_query("SET NAMES cp1251");
   
   if (isset($_POST['Submit'])) {
   
   $user=strip_tags($_POST['potrebitel']);
    $user=addslashes($user);
     
    $pass= md5($_POST['pass']);
   $pass2= md5($_POST['pass2']);
    
    $email=strip_tags($_POST['email']);
    $email=addslashes($email);
   
   $race = $_POST['race'];
   
   $gold = '10000';
   $gas = '10000';
   $metal = '10000';
   $hangar = '20';
   $hangarlvl = '1';
   
   if(empty($user) || empty($pass) || empty($pass2) || empty($email) || empty($race)) {
   die ("<strong style='color: #FF0000;'>Не сте попълнили всички полета</strong>");
   }
   
   $check=mysql_query("SELECT * FROM `users` WHERE `user` = '$user'");
   $user_exist=mysql_num_rows($check);
   if($user_exist > 0) {
      die("Потребителското име е заето!");
      }else{
   }
   
   $check_email=mysql_query("SELECT * FROM `users` WHERE `email` = '$email'");
   $email_exist=mysql_num_rows($check_email);
   if($user_exist > 0) {
      die("Тази поща е вече използвана!");
      }else{
   }
   
   if($pass!=$pass2) {   
   die ("Не сте въвели правилно втората парола!");
   }
   
   if(!ereg("@",$email) ) {
   die("<strong style='color: #FF0000;'>Невалиден e - mail</strong>");
   }else{
   }
   
   $insert_sql = mysql_query("INSERT INTO `users`(user, password, email, race)
                  VALUES ('$user', '$pass', '$email', '$race')") or die(mysql_error());
       
	    $select=mysql_query("SELECT * FROM `users`") or die(mysql_error());
   $r=mysql_fetch_assoc($select);
   $id=$r['id'];
	   
   $insert=mysql_query("INSERT INTO `resources` (`user_id`, `gold`, `gas`, `metal`) VALUES ('$id','$gold','$gas','$metal')") or die(mysql_error());
   $buildings=mysql_query("INSERT INTO `buildings` (`hangar`, `hangarlvl`) VALUES ('$hangar','$hangarlvl')") or die(mysql_error());	   

     if ($insert_sql==TRUE && $insert==TRUE && $buildings==TRUE) {

         echo "<strong style='color: #FFF;'>Потребителя е регистриран успешно!</strong>";
      }else{
         echo "<strong style='color: #FF0000;'>Грешка при регистрация!</strong>";
         }
     }
   
?>
<form method="post" action="<?php $_SERVER ['PHP_SELF'] ?>">
<table align="center">
<tr align="center">
<b style="color: #FFF;"><center>Ако нямате регистрация можете да си направите безплатна от тук:</center></b><br/>
<td style="color: #FFF;">Потребителско име*</td>
<td><input id="user" name="potrebitel" type="text" maxlength="20" onblur="checkUsername(this.value)" style="opacity:0.6;filter:alpha(opacity=60)" onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100"onmouseout="this.style.opacity=0.6;this.filters.alpha.opacity=60" /></td>
<td width="100px"><div style="color: #FFF;" class="message" id="message"></div></td>
</tr>
<tr>
<td style="color: #FFF;">Парола*</td>
<td><input id="pass" name="pass" type="password" maxlength="15" style="opacity:0.6;filter:alpha(opacity=60)" onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100"onmouseout="this.style.opacity=0.6;this.filters.alpha.opacity=60" ></td>
</tr>
<tr>
<td style="color: #FFF;">Повторете паролата*</td>
<td><input id="pass" name="pass2" type="password" maxlength="15" style="opacity:0.6;filter:alpha(opacity=60)" onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100"onmouseout="this.style.opacity=0.6;this.filters.alpha.opacity=60" ></td>
</tr>
<tr>
<td style="color: #FFF;">E - Mail*</td>
<td><input id="user" name="email" type="text" maxlength="20" style="opacity:0.6;filter:alpha(opacity=60)" onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100"onmouseout="this.style.opacity=0.6;this.filters.alpha.opacity=60" ></td>
</tr>
<tr>
<tr>
<td style="color: #FFF;">Код*</td>
<td><input id="user" name="captcha" type="text" maxlength="20" style="opacity:0.6;filter:alpha(opacity=60)" onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100"onmouseout="this.style.opacity=0.6;this.filters.alpha.opacity=60" ></td>
<td><?php echo $captcha; ?></td>
</tr>
<tr>
<td style="color: #FFF;">Раса*</td>
<td>
<select id="user" name="race">
<option id="select">Земляни</option>
<option id="select">Марсианци</option>
</select></td>

</tr>
<tr>
            <tr>
              <td> </td>
              <td valign="top"><input type="submit" name="Submit" id="login" value="Регистрация" style="opacity:0.8;filter:alpha(opacity=80)" onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100" onmouseout="this.style.opacity=0.8;this.filters.alpha.opacity=80"/></td>
            </tr>
</table>
</form>
 
Когато регистрирам един е добре, но като регистрирам втори и ми дублира id=1. Защо става така ?
 
Код:
<?php
   require_once('../connection.php');
   mysql_query("SET NAMES cp1251");
   
   if (isset($_POST['Submit'])) {
   
   $user=strip_tags($_POST['potrebitel']);
    $user=addslashes($user);
     
    $pass= md5($_POST['pass']);
   $pass2= md5($_POST['pass2']);
   
    $email=strip_tags($_POST['email']);
    $email=addslashes($email);
   
   $race = $_POST['race'];
   
   $gold = '10000';
   $gas = '10000';
   $metal = '10000';
   $hangar = '20';
   $hangarlvl = '1';
   
   if(empty($user) || empty($pass) || empty($pass2) || empty($email) || empty($race)) {
   die ("<strong style='color: #FF0000;'>Не сте попълнили всички полета</strong>");
   }
   
   $check=mysql_query("SELECT * FROM `users` WHERE `user` = '$user'");
   $user_exist=mysql_num_rows($check);
   if($user_exist > 0) {
      die("Потребителското име е заето!");
      }else{
   }
   
   $check_email=mysql_query("SELECT * FROM `users` WHERE `email` = '$email'");
   $email_exist=mysql_num_rows($check_email);
   if($user_exist > 0) {
      die("Тази поща е вече използвана!");
      }else{
   }
   
   if($pass!=$pass2) {   
   die ("Не сте въвели правилно втората парола!");
   }
   
   if(!ereg("@",$email) ) {
   die("<strong style='color: #FF0000;'>Невалиден e - mail</strong>");
   }else{
   }
   
   $insert_sql = mysql_query("INSERT INTO `users`(user, password, email, race)
                  VALUES ('$user', '$pass', '$email', '$race')") or die(mysql_error());
       
      
   $insert=mysql_query("INSERT INTO `resources` (`user_id`, `gold`, `gas`, `metal`) VALUES ('','$gold','$gas','$metal')") or die(mysql_error());
   $buildings=mysql_query("INSERT INTO `buildings` (`hangar`, `hangarlvl`) VALUES ('$hangar','$hangarlvl')") or die(mysql_error());      

     if ($insert_sql==TRUE && $insert==TRUE && $buildings==TRUE) {

         echo "<strong style='color: #FFF;'>Потребителя е регистриран успешно!</strong>";
      }else{
         echo "<strong style='color: #FF0000;'>Грешка при регистрация!</strong>";
         }
     }
   
?>
<form method="post" action="<?php $_SERVER ['PHP_SELF'] ?>">
<table align="center">
<tr align="center">
<b style="color: #FFF;"><center>Ако нямате регистрация можете да си направите безплатна от тук:</center></b><br/>
<td style="color: #FFF;">Потребителско име*</td>
<td><input id="user" name="potrebitel" type="text" maxlength="20" onblur="checkUsername(this.value)" style="opacity:0.6;filter:alpha(opacity=60)" onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100"onmouseout="this.style.opacity=0.6;this.filters.alpha.opacity=60" /></td>
<td width="100px"><div style="color: #FFF;" class="message" id="message"></div></td>
</tr>
<tr>
<td style="color: #FFF;">Парола*</td>
<td><input id="pass" name="pass" type="password" maxlength="15" style="opacity:0.6;filter:alpha(opacity=60)" onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100"onmouseout="this.style.opacity=0.6;this.filters.alpha.opacity=60" ></td>
</tr>
<tr>
<td style="color: #FFF;">Повторете паролата*</td>
<td><input id="pass" name="pass2" type="password" maxlength="15" style="opacity:0.6;filter:alpha(opacity=60)" onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100"onmouseout="this.style.opacity=0.6;this.filters.alpha.opacity=60" ></td>
</tr>
<tr>
<td style="color: #FFF;">E - Mail*</td>
<td><input id="user" name="email" type="text" maxlength="20" style="opacity:0.6;filter:alpha(opacity=60)" onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100"onmouseout="this.style.opacity=0.6;this.filters.alpha.opacity=60" ></td>
</tr>
<tr>
<tr>
<td style="color: #FFF;">Код*</td>
<td><input id="user" name="captcha" type="text" maxlength="20" style="opacity:0.6;filter:alpha(opacity=60)" onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100"onmouseout="this.style.opacity=0.6;this.filters.alpha.opacity=60" ></td>
<td><?php echo $captcha; ?></td>
</tr>
<tr>
<td style="color: #FFF;">Раса*</td>
<td>
<select id="user" name="race">
<option id="select">Земляни</option>
<option id="select">Марсианци</option>
</select></td>

</tr>
<tr>
            <tr>
              <td> </td>
              <td valign="top"><input type="submit" name="Submit" id="login" value="Регистрация" style="opacity:0.8;filter:alpha(opacity=80)" onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100" onmouseout="this.style.opacity=0.8;this.filters.alpha.opacity=80"/></td>
            </tr>
</table>
</form>
 
Ще разгледам кода по хубавичко и ще си едитна мнението, но сигорен ли си че полето id ти е int ?
ЕДИТ: ако не ти е било инт го направи, ако е инт и пак нестава пробвай така :



Код:
<?php
   require_once('../connection.php');
   mysql_query("SET NAMES cp1251");
   
   if (isset($_POST['Submit'])) {
   
   $user=strip_tags($_POST['potrebitel']);
    $user=addslashes($user);
     
    $pass= md5($_POST['pass']);
   $pass2= md5($_POST['pass2']);
   
    $email=strip_tags($_POST['email']);
    $email=addslashes($email);
   
   $race = $_POST['race'];
   
   $gold = '10000';
   $gas = '10000';
   $metal = '10000';
   $hangar = '20';
   $hangarlvl = '1';
   
   if(empty($user) || empty($pass) || empty($pass2) || empty($email) || empty($race)) {
   die ("<strong style='color: #FF0000;'>Не сте попълнили всички полета</strong>");
   }
   
   $check=mysql_query("SELECT * FROM `users` WHERE `user` = '$user'");
   $user_exist=mysql_num_rows($check);
   if($user_exist > 0) {
      die("Потребителското име е заето!");
      }else{
   }
   
   $check_email=mysql_query("SELECT * FROM `users` WHERE `email` = '$email'");
   $email_exist=mysql_num_rows($check_email);
   if($user_exist > 0) {
      die("Тази поща е вече използвана!");
      }else{
   }
   
   if($pass!=$pass2) {   
   die ("Не сте въвели правилно втората парола!");
   }
   
   if(!ereg("@",$email) ) {
   die("<strong style='color: #FF0000;'>Невалиден e - mail</strong>");
   }else{
   }
   
   $insert_sql = mysql_query("INSERT INTO `users`(user, password, email, race)
                  VALUES ('$user', '$pass', '$email', '$race')") or die(mysql_error());
       
       $select=mysql_query("SELECT * FROM `users` ORDER BY `id` DESC") or die(mysql_error());
   $r=mysql_fetch_assoc($select);
   $id=$r['id'];
   $nid=$id+1;
      
   $insert=mysql_query("INSERT INTO `resources` (`user_id`, `gold`, `gas`, `metal`) VALUES ('$nid','$gold','$gas','$metal')") or die(mysql_error());
   $buildings=mysql_query("INSERT INTO `buildings` (`hangar`, `hangarlvl`) VALUES ('$hangar','$hangarlvl')") or die(mysql_error());      

     if ($insert_sql==TRUE && $insert==TRUE && $buildings==TRUE) {

         echo "<strong style='color: #FFF;'>Потребителя е регистриран успешно!</strong>";
      }else{
         echo "<strong style='color: #FF0000;'>Грешка при регистрация!</strong>";
         }
     }
   
?>
<form method="post" action="<?php $_SERVER ['PHP_SELF'] ?>">
<table align="center">
<tr align="center">
<b style="color: #FFF;"><center>Ако нямате регистрация можете да си направите безплатна от тук:</center></b><br/>
<td style="color: #FFF;">Потребителско име*</td>
<td><input id="user" name="potrebitel" type="text" maxlength="20" onblur="checkUsername(this.value)" style="opacity:0.6;filter:alpha(opacity=60)" onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100"onmouseout="this.style.opacity=0.6;this.filters.alpha.opacity=60" /></td>
<td width="100px"><div style="color: #FFF;" class="message" id="message"></div></td>
</tr>
<tr>
<td style="color: #FFF;">Парола*</td>
<td><input id="pass" name="pass" type="password" maxlength="15" style="opacity:0.6;filter:alpha(opacity=60)" onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100"onmouseout="this.style.opacity=0.6;this.filters.alpha.opacity=60" ></td>
</tr>
<tr>
<td style="color: #FFF;">Повторете паролата*</td>
<td><input id="pass" name="pass2" type="password" maxlength="15" style="opacity:0.6;filter:alpha(opacity=60)" onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100"onmouseout="this.style.opacity=0.6;this.filters.alpha.opacity=60" ></td>
</tr>
<tr>
<td style="color: #FFF;">E - Mail*</td>
<td><input id="user" name="email" type="text" maxlength="20" style="opacity:0.6;filter:alpha(opacity=60)" onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100"onmouseout="this.style.opacity=0.6;this.filters.alpha.opacity=60" ></td>
</tr>
<tr>
<tr>
<td style="color: #FFF;">Код*</td>
<td><input id="user" name="captcha" type="text" maxlength="20" style="opacity:0.6;filter:alpha(opacity=60)" onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100"onmouseout="this.style.opacity=0.6;this.filters.alpha.opacity=60" ></td>
<td><?php echo $captcha; ?></td>
</tr>
<tr>
<td style="color: #FFF;">Раса*</td>
<td>
<select id="user" name="race">
<option id="select">Земляни</option>
<option id="select">Марсианци</option>
</select></td>

</tr>
<tr>
            <tr>
              <td> </td>
              <td valign="top"><input type="submit" name="Submit" id="login" value="Регистрация" style="opacity:0.8;filter:alpha(opacity=80)" onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100" onmouseout="this.style.opacity=0.8;this.filters.alpha.opacity=80"/></td>
            </tr>
</table>
</form>
 
Ако имаш на някое от останалите полета UNIQUE го махни и пробвай пак с по-горните кодове.
 
Не никое поле не е уникално. Ами аз пробвах да направя така: Във всяка таблицаат да записва и при всеки нов потребител във всяка таблица ид - то се овеличава с едно, но така не е сигурно. В краен случей ще го направя пак така.
 
последния код който ти дадох, в едита е точно такав, увеличава с 1, от последното ид, добавено в дб-то..
 
Ако полето е AUTO_INCREMENT му задаваш стойност 'NULL' , всякакви други стойности са освен безмислени ,и грешни.
П.П. Извинявам се ,не съм разглеждал предните кодове ,може и аз да не съм разбрал.
 

Back
Горе