INSERT INTO mysql Tables and stuff I desperately need help

Discussion in 'PHP' started by Sirricharic, Jan 28, 2011.

  1. #1
    $db_table = "jos_bl_teams"; // leave this as is 
    $db_table2 = "jos_bl_season_teams";
    
    
    # STOP HERE 
    ####################################################################
    # THIS CODE IS USED TO CONNECT TO THE MYSQL DATABASE 
    $db = mysql_connect($hostname, $db_user, $db_password); 
    mysql_select_db($database,$db); 
    ?> 
    <html> 
    <head> 
    <title>Team Registration</title> 
    </head> 
    <body> 
    
    <?php 
    if (isset($_REQUEST['Submit'])) { 
    # THIS CODE TELL MYSQL TO INSERT THE DATA FROM THE FORM INTO YOUR MYSQL TABLE 
    //$sql = "INSERT INTO $db_table2 (season_id,team_id) values (1,(SELECT id FROM $db_table WHERE t_name = '{$_REQUEST['t_name']}')"; 
    $sql = "INSERT INTO $db_table(t_name,t_city) values ('".mysql_real_escape_string(stripslashes($_REQUEST['t_name']))."','".mysql_real_escape_string(stripslashes($_REQUEST['t_city']))."') INSERT INTO $db_table2 (season_id,team_id) values ('1','$thing')"; 
    $thing = "SELECT id FROM $db_table WHERE t_name = '{$_REQUEST['t_name']}'";                   
    if($result = mysql_query($sql ,$db)) { 
    echo '<h1>Thank you</h1>For Registering<br><br><img src="header.jpg"'; 
    } else { 
    echo "ERROR: ".mysql_error();
    }  
    $to ="teams@golsoccer.net";
    $name1= $_REQUEST['first_name'] ;
    $name2= $_REQUEST['last_name'] ;
    $tname= $_REQUEST['t_name'] ;
    $tcity= $_REQUEST['t_city'] ;
    $email= $_REQUEST['user_email'] ;
    $subject ="New Team Registered";
    $message =" YAY! $name1 $name2 has made a new team named $tname in $tcity using email $email";
    $from ="site@golsoccer.net";
    $header ="From $from";
    mail($to,$subject,$message,$header); 
    echo "Admin Notified";
    }
    ?>
    <h1>Team Registration</h1> By <a href="http://www.golsoccer.net">Golsoccer.net</a><hr> 
    <form method="post" action=""> 
    Name (First):<br> 
    <input type="text" name="first_name"> 
    <br>
    Name (Last):<br> 
    <input type="text" name="last_name">
    <br>
    Email:<br> 
    <input type="text" name="user_email"> 
    <br>
    Team Name:<br>
    <input type="text" name="t_name">
    <br>
    Team Location:<br>
    <input type="text" name="t_city">
    <br><br> 
    <input type="submit" name="Submit" value="Submit"> 
    </form>
    <?php  
    ?> 
    </body> 
    </html> 
    
    PHP:
    Ok I'm just about to give up I got it to insert the t_name and the t_city data just fine.
    Now I am trying to get it to read the id from jos_bl_teams where the team equals the team name entered in the field THEN add that id into jos_bl_season_teams under season_id 1!!!!

    This isn't working for me can you help me?
     
    Sirricharic, Jan 28, 2011 IP
  2. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #2
    Try this :
    
    <?php
    $thing = 0;
    if (isset($_REQUEST['Submit'])) { 
    $result = mysql_query("SELECT id FROM $db_table WHERE t_name = '".$_POST['t_name']."' LIMIT 1");
    if(mysql_num_rows($result) == 1)
    {
    $row = mysql_fetch_array($result);
    $thing = $row['id'];
    }
    $sql = "INSERT INTO $db_table(t_name,t_city) values ('".mysql_real_escape_string(stripslashes($_POST['t_name']))."','".mysql_real_escape_string(stripslashes($_POST['t_city']))."');INSERT INTO $db_table2 (season_id,team_id) values ('1','".$thing."')"; 
    $result = mysql_query($sql ,$db)      
    if(mysql_affected_rows($db) == 1) { 
    echo '<h1>Thank you</h1>For Registering<br><br><img src="header.jpg"';
    
    $to ="teams@golsoccer.net";
    $name1= $_POST['first_name'] ;
    $name2= $_POST['last_name'] ;
    $tname= $_POST['t_name'] ;
    $tcity= $_POST['t_city'] ;
    $email= $_POST['user_email'] ;
    $subject ="New Team Registered";
    $message =" YAY! $name1 $name2 has made a new team named $tname in $tcity using email $email";
    $from ="site@golsoccer.net";
    $header ="From $from";
    mail($to,$subject,$message,$header); 
    echo "Admin Notified"; 
    } else { 
    echo "ERROR: ".mysql_error();
    }  
    }
    ?>
    
    PHP:
     
    tvoodoo, Jan 28, 2011 IP
  3. Sirricharic

    Sirricharic Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I get
    Parse error: syntax error, unexpected T_IF in /home3/golsocce/public_html/teamapp2.php on line 39
    PHP:
    Where it says:
    if(mysql_affected_rows($db) == 1) {
    PHP:
     
    Sirricharic, Jan 29, 2011 IP
  4. Sirricharic

    Sirricharic Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I fixed that by putting a ; on the line above that but now I get

    ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO jos_bl_season_teams (season_id,team_id) values ('1','0')' at line 1

    When I submit the form
     
    Sirricharic, Jan 29, 2011 IP