+ Reply to Thread
Results 1 to 5 of 5

Thread: need php/mySQL help again :(

  1. #1
    thenewprogrammer is offline x10Hosting Member thenewprogrammer is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    45

    need php/mySQL help again :(

    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 by thenewprogrammer; 07-31-2009 at 07:03 PM.

  2. #2
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: need php/mySQL help again :(

    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()'));

  3. #3
    thenewprogrammer is offline x10Hosting Member thenewprogrammer is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    45

    Re: need php/mySQL help again :(

    currently have fetch_array is a invalid arugment on line 32 if anyone knows how to fix this
    Last edited by thenewprogrammer; 07-31-2009 at 05:16 PM.

  4. #4
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: need php/mySQL help again :(

    Quote Originally Posted by thenewprogrammer View Post
    currently have fetch_array is a invalid arugment on line 32 if anyone knows how to fix this

    Code:
    $query2 = "SELECT * FROM pettable 
          petatk ='$pet_atk',petdef='$pet_def' 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 = "SELECT petatk,  petdef 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 by descalzo; 07-31-2009 at 07:05 PM.

  5. #5
    thenewprogrammer is offline x10Hosting Member thenewprogrammer is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    45

    Re: need php/mySQL help again :(

    thnx got my problem solved now

+ Reply to Thread

Similar Threads

  1. Tutorial: PHP/MySQL Membership System
    By Jesse in forum Tutorials
    Replies: 31
    Last Post: 03-20-2012, 03:48 PM
  2. Need PHP/mySQL Coders
    By kesne in forum Ads & Offers
    Replies: 0
    Last Post: 12-16-2008, 02:27 PM
  3. Any good PHP/MySQL tutorials out there?
    By shiveringtim in forum Off Topic
    Replies: 4
    Last Post: 05-13-2008, 10:34 PM
  4. PHP/mySQL noob question
    By alexxz4 in forum Programming Help
    Replies: 10
    Last Post: 04-05-2008, 01:56 PM
  5. I'll do HTML, PHP/MySQL work for credits
    By lupus_42 in forum Earning Money
    Replies: 9
    Last Post: 02-23-2008, 04:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers