need php/mySQL help again :(

thenewprogrammer

New Member
Messages
45
Reaction score
0
Points
0
Trying to make pet registration form. When i choose color/gender/name than click register it sends the pet information to the table. But for the second part i cant figure out why it comes up with error fetch_aray is a invalid argument.

confirm.php has my connecting information and its all correct.
The table the userpet information gets sent to is "pets."
Than im trying to take the base stats of the pet and send it to the users table "pets" when clicking register. The base stats of the pet are located in the table "pettable".

Code:
<?php
include('confirm.php');

//test to see if username is alphanumeric
$test=$_POST[pet_name];

if(!eregi(("[^A-Za-z0-9]"),$test)){

	//test for duplicate names
	$query="SELECT * FROM pets WHERE pet_name ='$_POST[pet_name]'";
	$result=mysql_query($query);
	$num=mysql_num_rows($result);
	
	
	if ($num == 0){
			
				//get rid of all html from hackers
				$name=strip_tags($_POST['pet_name']);
				$color=$_POST['pet_color'];
				$gender=$_POST['pet_gender'];
				$species=$_POST['pet_species'];
				
			
				//insert data into database
				$sql="INSERT INTO pets SET pet_name='$name',pet_color='$color',pet_gender='$gender', species_id='$species'";
				$result=mysql_query($sql);
				
				}
				
				//select stats from pet table
				$query2 = "SELECT * FROM pettable petatk ='$pet_atk',petdef='$pet_def' WHERE petname='$species' ";
				$result2 = mysql_query($query2);
				$row = mysql_fetch_array($result2);
				

				
				//if pet stats are in table update players pet table when registering
				if($row == 1){
				$sql3="UPDATE * FROM pets SET pet_atk='$pet_atk',petdef='$pet_def' WHERE petname='$species'";
				$result3=mysql_query($sql3);
				
				//take them to page for having succesfully created pet
				header("Location:newpet.php");
					
				}else{
				echo "Not found in our database";
				}
				
				
				}else{
				header("Location:usernametaken.html");
	
}
?>
 
Last edited:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
But for the second part i cant figure out how to get the variable for pet id so it can search table for the right pet when registered and not taking them all.

int mysql_insert_id( )

Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
This function returns 0 if the previous operation does not generate an AUTO_INCREMENT ID, or FALSE on MySQL connection failure.
Be sure to call mysql_insert_id() immediately after a query to get the correct value.

You can also use:

mysql_result (mysql_query ('SELECT LAST_INSERT_ID()'));
 

thenewprogrammer

New Member
Messages
45
Reaction score
0
Points
0
currently have fetch_array is a invalid arugment on line 32 if anyone knows how to fix this
 
Last edited:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
currently have fetch_array is a invalid arugment on line 32 if anyone knows how to fix this


Code:
$query2 = "SELECT * FROM pettable 
      [B]petatk ='$pet_atk',petdef='$pet_def' [/B]WHERE pet_id='$petid' ";

I have no idea what the bolded part is doing there. You cannot set php variables from within a SELECT statement like that.
Instead of SELECT * which returns all the fields, ask for the two fields you are interested in by name:

Code:
$query2 = "[B]SELECT petatk,  petdef[/B] FROM pettable  WHERE pet_id='$petid' ";
$result2 = mysql_query($query2);
$row = mysql_fetch_array($result2);

Now $row should be an array with the two fields you requested from the row you just inserted.

$row[0] will contain the petatk value and $row[1] will contain the petdef value.
So you can set

Code:
$pet_atk = $row[0];
$pet_def = $row[1];
 
Last edited:
Top