help with inserting queries

Discussion in 'PHP' started by WhitneyM, Nov 25, 2009.

  1. #1
    I am making a horse game using php and its my first project, so I am really new to this, but basically i need some help with my script. Ok so everything works from the extract($row) and up, but after that it keeps telling me it can not execute query. Is it not possible to insert multiple queries on one page? So here is part of my script (I cut out the login stuff and php tags, but the login is working fine and if I try to echo $playerid after the extract($row) it works):

    $query="INSERT INTO member (loginname, password, createdate, lastname, firstname, email, ranchname) VALUES ('$UserName', '$Newpassword', '$today', '$LastName', '$FirstName', '$Email', '$RanchName')";
    $result=mysqli_query($cxn, $query) or die ("Couldn't execute query 1.");
    $sql="SELECT playerid FROM member WHERE loginname='$_POST[username]'";
    $result=mysqli_query ($cxn, $sql) or die ("couldn't execute query for member.");
    $row=mysqli_fetch_assoc($result);
    extract($row);
    $query="INSERT INTO bank (playerid, balance) VALUES ('$playerid', '20000')";
    $result=mysqli_query($cxn, $query) or die ("Couldn't execute query 3.");
    $query="INSERT INTO upgrades (playerid, upgradetype) VALUES ('$playerid', 'free')";
    $result=mysqli_query($cxn, $query) or die ("Couldn't execute query4.");



    Thanks!
     
    WhitneyM, Nov 25, 2009 IP
  2. japanninja

    japanninja Active Member

    Messages:
    54
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    50
    #2
    Can you post a detailed error message that is displayed in your page.
     
    japanninja, Nov 26, 2009 IP
  3. WhitneyM

    WhitneyM Guest

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    So here is the full start of the code (a lot of it is still in the making so all of the links, etc aren't there yet):

    <html>
    <head>
    <title></title>
    </head>
    <body bgcolor=“#CCFF99”>
    <body>
    <table>
    <table border=“0”>
    <tr>
    <td>
    Add season and date/time information here
    </td>
    <td>
    <div align="center">
    <img src="logo1.jpg" alt="Logo" width="400" height="200" alt="logo">
    </div>
    </td>
    </tr>
    <tr>
    <td>
    Add left hand menu links here
    </td>
    <td>
    <?php
    $host="######";
    $user="#####";
    $passwd="####";
    $dbname="####";
    $cxn=mysqli_connect ($host, $user, $passwd, $dbname) or die ("Couldn't connect to server");
    foreach ($_POST as $field=>$value)
    {
    echo"<p>$field=$value</p>";
    }
    $sql="SELECT LoginName FROM Member WHERE LoginName='$UserName'";
    $result=mysqli_query($cxn, $sql) or die("Couldn't execute select query.");
    $num=mysqli_num_rows($result);
    if($num>0)
    {
    echo"Your username has already been used, please select another user name.";
    exit();
    }
    elseif($num=="0")
    {
    $Newpassword=(md5($Password));
    $today = date ("Y-m-d h:i:s");
    $query="INSERT INTO member (loginname, password, createdate, lastname, firstname, email, ranchname) VALUES ('$UserName', '$Newpassword', '$today', '$LastName', '$FirstName', '$Email', '$RanchName')";
    $result=mysqli_query($cxn, $query) or die ("Couldn't execute query 1.");
    $sql="SELECT playerid FROM member WHERE loginname='$_POST[username]'";
    $result=mysqli_query ($cxn, $sql) or die ("couldn't execute query for member.");
    $row=mysqli_fetch_assoc($result);
    extract($row);
    $query="INSERT INTO bank (playerid, balance) VALUES ('$playerid', '20000')";
    $result=mysqli_query($cxn, $query) or die ("Couldn't execute query 3.");
    $query="INSERT INTO upgrades (playerid, upgradetype) VALUES ('$playerid', 'free')";
    $result=mysqli_query($cxn, $query) or die ("Couldn't execute query4.");

    So thats the start of the code. Everything works through the extract($row) and after that no matter what insert query I put there it will not work. I know that the connection is working, the insert into member works because i can log in, the extract for the playerid works because i can echo the playerid afterwards. Here is what i get when i enter "w" in all of the parts of the form:

    UserName=w

    RanchName=w

    Password=w

    LastName=w

    FirstName=w

    Email=w

    Couldn't execute query 2.

    That's it, and I checked all the tables in the database so nothing is wrong there.

    Thanks
     
    WhitneyM, Nov 26, 2009 IP
  4. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Change your code to this
    $result=mysqli_query($cxn, $query) or die ("Error in query 1<br />$query<br />".mysql_error);
    $sql="SELECT playerid FROM member WHERE loginname='$_POST[username]'";
    $result=mysqli_query ($cxn, $sql) or die ("Error in query 2<br />$sql<br />".mysql_error);
    $row=mysqli_fetch_assoc($result);
    extract($row);
    $query="INSERT INTO bank (playerid, balance) VALUES ('$playerid', '20000')";
    $result=mysqli_query($cxn, $query) or die ("Error in query 3<br />$query<br />".mysql_error);
    $query="INSERT INTO upgrades (playerid, upgradetype) VALUES ('$playerid', 'free')";
    $result=mysqli_query($cxn, $query) or die ("Error in query 4<br />$query<br />".mysql_error);
    PHP:
    And give us the output from the error
     
    JAY6390, Nov 26, 2009 IP
  5. WhitneyM

    WhitneyM Guest

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Ok, so I tried it with z this time for everything and this is what i got:

    UserName=z

    RanchName=z

    Password=z

    LastName=z

    FirstName=z

    Email=z

    Error in query 3
    INSERT INTO bank (playerid, balance) VALUES ('1', '20000')
    mysql_error
     
    WhitneyM, Nov 26, 2009 IP
  6. WhitneyM

    WhitneyM Guest

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    O, and I just checked the database again and there is a table in the databse called bank with columns playerid and balance.
     
    WhitneyM, Nov 26, 2009 IP
  7. WhitneyM

    WhitneyM Guest

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    So I just figured out what was wrong when I started playing with the script, but I can't figure out why its happening. Whenever I try to register the playerid is set to 1, the column is serial in the member table, but not in the other tables. When I pull up the table it says it is auto increment. This question most likely belongs in the mysql section, but since I already have a question here I figured I would ask it here since it relates and all. Thanks for everyones help so far.
     
    WhitneyM, Nov 26, 2009 IP
  8. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #8
    oops sorry, I meant
    $result=mysqli_query($cxn, $query) or die ("Error in query 1<br />$query<br />".mysql_error());
    $sql="SELECT playerid FROM member WHERE loginname='$_POST[username]'";
    $result=mysqli_query ($cxn, $sql) or die ("Error in query 2<br />$sql<br />".mysql_error());
    $row=mysqli_fetch_assoc($result);
    extract($row);
    $query="INSERT INTO bank (playerid, balance) VALUES ('$playerid', '20000')";
    $result=mysqli_query($cxn, $query) or die ("Error in query 3<br />$query<br />".mysql_error());
    $query="INSERT INTO upgrades (playerid, upgradetype) VALUES ('$playerid', 'free')";
    $result=mysqli_query($cxn, $query) or die ("Error in query 4<br />$query<br />".mysql_error());
    PHP:
     
    JAY6390, Nov 26, 2009 IP
  9. WhitneyM

    WhitneyM Guest

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Ok, so I know this is like my fourth post, sorry for posting so much I just can't figure out how to delete the older ones, but I figured it out. For some reason it was thinking that $_POST['username'] in the select query was blank so it kept turning up an account I had accidently made earlier while figuring out the registration page that had a blank username and a playerid of 1, so everything in the tables already existed. I figured it out though. Thanks so much for everyones help!
     
    WhitneyM, Nov 26, 2009 IP
  10. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Ah I see. Cool :cool:
     
    JAY6390, Nov 26, 2009 IP
  11. WhitneyM

    WhitneyM Guest

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Thanks again for your help!
     
    WhitneyM, Nov 26, 2009 IP