SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 27
  1. #1
    Gone!
    Join Date
    Aug 2001
    Location
    Witty Location Parody
    Posts
    3,889
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Adding additional SQL statement throws errors after it.

    Hey!

    Building the script below but ever since I added the second SQL statement (the SELECT) it is throwing up the following error on something separate from the new SQL:

    Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\Program Files\PHP Home Edition 2\www\portal\test.php on line 19
    It worked fine until I added the SQL statement but now is throwing up and error in the foreach bit? Started at it long enough but cant see what is blatantly staring me in the face.

    Here is the php with line 19 annotated:

    PHP Code:
    <?php
    include ('dbconnect.php');
    if(
    $_POST['Submit'] == 'Submit') {

        
    $sqlquery "INSERT INTO admin (userid, password, firstname, surname, email, year) VALUES ('".$_POST['userid']."','".$_POST['password']."','".$_POST['firstname']."','".$_POST['surname']."','".$_POST['email']."','".$_POST['year']."')";
        
    $results mysql_query($sqlquery);

        
    $sqlquery "SELECT from admin 'student_id' WHERE userid = '$_POST[userid]';
        
    $result = mysql_query($sqlquery);
        echo 
    $result;

        
    $retcode = $_POST['retcode'];     <===== Line 19
         foreach (
    $retcode as $mod) {
             echo 
    $mod.'<br />';
        }

    }
    else  {
    // some stuff
    Anyone?

  2. #2
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $sqlquery "SELECT student_id FROM admin WHERE userid = $_POST[userid]"
    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  3. #3
    Gone!
    Join Date
    Aug 2001
    Location
    Witty Location Parody
    Posts
    3,889
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers Sean!

    Man I need to take a break! Either that or stop ignoring the 1 hour alarms I have set!


  4. #4
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Real programmers don't take breaks



    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  5. #5
    Gone!
    Join Date
    Aug 2001
    Location
    Witty Location Parody
    Posts
    3,889
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by seanf
    Real programmers don't take breaks



    Sean
    Ill never be a real programmer! Strange relationships with quotes and back to front things!

    Speaking of which it wont echo $result from that query. Column names and vars are correct. Wanna show me up again ?

  6. #6
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If there's only ever one student_id returned change:

    PHP Code:
    $result mysql_query($sqlquery); 
    to:

    PHP Code:
    $result mysql_result $sqlquery0); 
    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  7. #7
    Gone!
    Join Date
    Aug 2001
    Location
    Witty Location Parody
    Posts
    3,889
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Still no cigar!

    PHP Code:
        $sqlquery "SELECT student_id from admin WHERE userid = $_POST[userid]";
        
    $result mysql_result $sqlquery0);
        echo 
    $result
    At the risk of sounding like a dumbass (just to shock the crowd ) but at the benefit of learning whats the 0,0 all about?

  8. #8
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're not running the query:

    PHP Code:
    $sqlquery mysql_query "SELECT student_id from admin WHERE userid = $_POST[userid]);
        
    $result mysql_result $sqlquery0);
        echo 
    $result
    See: http://www.sitepointforums.com/showt...threadid=93447 for a good answer to your second question

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  9. #9
    Gone!
    Join Date
    Aug 2001
    Location
    Witty Location Parody
    Posts
    3,889
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Aha that would explain it. But the new code still isnt working!

  10. #10
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $sqlquery mysql_query "SELECT student_id from admin WHERE userid = $_POST[userid]) or die ( mysql_error ( ) ); 
    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  11. #11
    Gone!
    Join Date
    Aug 2001
    Location
    Witty Location Parody
    Posts
    3,889
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by seanf
    PHP Code:
    $sqlquery mysql_query "SELECT student_id from admin WHERE userid = $_POST[userid]) or die ( mysql_error ( ) ); 
    Sean
    It says:

    Unknown column 'jhgjhgj' in 'where clause' <-- Where 'jhgjhgj' is the userid.

    Which would suggest I have got my WHERE clause the wrong way round. But I havent. I even switched the 2 around to double check and oddly it output the same error?

    Starting to go mad now

  12. #12
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If userid is a string you'll need to put single quotes around it, I assumed it was an integer

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  13. #13
    Gone!
    Join Date
    Aug 2001
    Location
    Witty Location Parody
    Posts
    3,889
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm I didnt notice that at all! ( I take it you mean missing quotes inside the square brackets of $_POST[userid]

    But in the true tradition of things:

    Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\Program Files\PHP Home Edition 2\www\portal\test.php on line 15

    PHP Code:
    $sqlquery mysql_query("SELECT student_id from admin WHERE userid = $_POST['userid']") or die ( mysql_error ( ) ); 
    I have tried adding single uotes to the column names and anything I can think of in diff combinations but still a parse error!

  14. #14
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $sqlquery mysql_query "SELECT student_id from admin WHERE userid = '$_POST[userid]'" ) or die ( mysql_error ( ) ); 
    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  15. #15
    Gone!
    Join Date
    Aug 2001
    Location
    Witty Location Parody
    Posts
    3,889
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    :| Kinda!

    No parse error and no sql error, but no echoed userid!

    PHP Code:
    $sqlquery mysql_query("SELECT student_id from admin WHERE userid = '$_POST[userid]'") or die( mysql_error());
    $result mysql_result ($sqlquery);
    echo 
    $result
    Im just off for a little cry in the corner

  16. #16
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there something there for it to select?

    PHP Code:
    $sqlquery mysql_query("SELECT student_id from admin WHERE userid = '$_POST[userid]'") or die( mysql_error());

    if ( 
    mysql_num_rows $sqlquery ) == ) {

        exit ( 
    'No rows here - off for a pint' );

    }

    $result mysql_result ($sqlquery);

    echo 
    $result
    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  17. #17
    Gone!
    Join Date
    Aug 2001
    Location
    Witty Location Parody
    Posts
    3,889
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yep!

    The SQL just before my troublesome one inserts a row into the table which this staement is supposed to grab the student_id of.

    Using your script works fine, it doesnt exit and moves on to the foreach.


  18. #18
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's too late for this! Let's see your full code

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  19. #19
    Gone!
    Join Date
    Aug 2001
    Location
    Witty Location Parody
    Posts
    3,889
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by seanf
    It's too late for this! Let's see your full code

    Sean
    Tell me about it I really appreciate your patience!

    PHP Code:
    <?php echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?".">"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>TEST SCRIPT</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    </head>
    <?php
    include ('dbconnect.php');
    if(
    $_POST['Submit'] == 'Submit') {

        
    $sqlquery "INSERT INTO admin (userid, password, firstname, surname, email, year) VALUES ('".$_POST['userid']."','".$_POST['password']."','".$_POST['firstname']."','".$_POST['surname']."','".$_POST['email']."','".$_POST['year']."')";
        
    $results mysql_query($sqlquery);

        
    $sqlquery mysql_query("SELECT student_id from admin WHERE userid = '$_POST[userid]'") or die( mysql_error());
        if ( 
    mysql_num_rows $sqlquery ) == ) {
            exit ( 
    'No rows here - off for a pint' );
        }
        
    $result mysql_result ($sqlquery);
        echo 
    $result;

        
    $retcode $_POST['retcode']; 
         foreach (
    $retcode as $mod) {
             echo 
    $mod.'<br />';
        }

    }
    else  {

    /* Select all rows from the modules column */
    $sqlquery "SELECT * FROM modules";
    $result mysql_query($sqlquery);
    /* Pull amount of rows returned */
    $number mysql_num_rows($result);
    ?>
    <body><form action="<?=$_SERVER['PHP_SELF']?>" method="post" name="adduser">
      <table width="75%" border="0">
        <tr> 
          <td width="40%">First Name:</td>
          <td width="60%"><input type="text" name="firstname" width="150" /></td>
        </tr>
        <tr> 
          <td width="40%">Surname:</td>
          <td width="60%"><input type="text" name="surname" width="150" /></td>
        </tr>
        <tr> 
          <td width="40%">Username:</td>
          <td width="60%"><input type="text" name="userid" width="150" /></td>
        </tr>
        <tr> 
          <td width="40%">Password:</td>
          <td width="60%"><input type="text" name="password" width="150" /></td>
        </tr>
        <tr> 
          <td width="40%">Email:</td>
          <td width="60%"><input type="text" name="email" width="150" /></td>
        </tr>
        <tr> 
          <td width="40%">Confirm Email:</td>
          <td width="60%"><input type="text" name="email2" width="150" /></td>
        </tr>
        <tr> 
          <td width="40%">Year of Study:</td>
          <td width="60%"><input type="text" name="year" width="150" /></td>
        </tr>
        <tr> 
          <td width="40%">Modules to be taken this year (select all that apply):</td>
          <td width="60%"> 

    <?php
    /* Start a counter variable */
    $i 0;

    /* If number of rows returned is less than 1 there are no modules so output string */
    if ($number 1)
    {
    echo 
    "<center><p>There are no modules currently stored.</p></center>";
    }

    else
    {
    /* While counter variable is less than the number of rows run the following */
    while ($number $i
    {
    /* Pull out relevant field data from database results set and define as separate variables */
    $code mysql_result($result$i"code");
    /* Form HTML output string */
    echo("<input name=\"retcode[]\" type=\"checkbox\" value=\"$code\" />$code<br />");
    /* Increment counter script by 1 */
    $i++;
    }
    }

    ?>

          </td>
        </tr>
        <tr>
          <td><input type="submit" name="Submit" value="Submit" /></td>
          <td>&nbsp;</td>
        </tr>
      </table>
    </form>
    </body>
    </html>
    <?php
    }
    ?>
    # Table structure for table `admin`
    #

    CREATE TABLE admin (
    student_id int(5) NOT NULL auto_increment,
    userid varchar(15) NOT NULL default '',
    password varchar(15) NOT NULL default '',
    level int(5) NOT NULL default '0',
    firstname varchar(20) NOT NULL default '',
    surname varchar(20) NOT NULL default '',
    email text NOT NULL,
    year int(2) NOT NULL default '0',
    PRIMARY KEY (student_id)
    ) TYPE=MyISAM;

  20. #20
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In the manual/mysql_result() it says:
    Calls to mysql_result() should not be mixed with calls to other functions that deal with the result set.
    But when you call mysql_num_rows() that is getting it's answer from the same result set. But do mysql_num_rows() deal with the result set?

    I don't know , I just ask.

    You could try to use mysql_fetch_array() instead?

    -Helge

  21. #21
    Gone!
    Join Date
    Aug 2001
    Location
    Witty Location Parody
    Posts
    3,889
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Helge
    In the manual/mysql_result() it says:But when you call mysql_num_rows() that is getting it's answer from the same result set. But do mysql_num_rows() deal with the result set?

    I don't know , I just ask.

    You could try to use mysql_fetch_array() instead?

    -Helge
    Thanks Helge but it still wasnt echoing the $result before we added mysql_num_rows as another handler.

    Another weird thing which might help is this:

    I tried echoing $_POST_['userid']; directly after the first SQL operation, but it wont output anything. This is silly because I KNOW $_POST_['userid']; has a value as when the SQL query above is run it inserts a row fine.

    PHP Code:
        $sqlquery "INSERT INTO admin (userid, password, firstname, surname, email, year) VALUES ('".$_POST['userid']."','".$_POST['password']."','".$_POST['firstname']."','".$_POST['surname']."','".$_POST['email']."','".$_POST['year']."')";
        
    $results mysql_query($sqlquery);
        
        echo 
    $POST_['userid']; 
    Which brings the question: " If $_POST_['userid']; suddenly empties after the first SQL op, why doesnt the num_rows op further down return 0 and exit?


  22. #22
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't know if it's was a typo when you wrote the post, but your echo as is won't work. Try this:
    PHP Code:
    echo $_POST['userid']; 
    If it was just a typo and the $_POST['userid'] really get empty. That is very strange and should not happen.

    -Helge

    Edit: Added the last sentence
    Last edited by Helge; Feb 19, 2003 at 04:51.

  23. #23
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I changed a bit of your the code. Just to see if it makes any differnce. It doesn't stand clare to me what the reason is.

    PHP Code:
    $sqlquery mysql_query("SELECT student_id FROM admin WHERE userid = '$_POST[userid]'") or die( mysql_error());

    if(empty(
    $sqlquery) || $sqlquery == "") {
        exit ( 
    'The result set is empty' );
    }

    $result mysql_fetch_array($sqlquery);
    echo 
    $result['student_id']; 
    -Helge

  24. #24
    Gone!
    Join Date
    Aug 2001
    Location
    Witty Location Parody
    Posts
    3,889
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Helge
    I don't know if it's was a typo when you wrote the post, but your echo as is won't work. Try this:
    PHP Code:
    echo $_POST['userid']; 
    -Helge
    Ah yes a typo in my script. That does work

    But still having the same problem with the rest:

    PHP Code:
    $sqlquery "INSERT INTO admin (userid, password, firstname, surname, email, year) VALUES ('".$_POST['userid']."','".$_POST['password']."','".$_POST['firstname']."','".$_POST['surname']."','".$_POST['email']."','".$_POST['year']."')";
        
    $results mysql_query($sqlquery);
        
        echo 
    $_POST['userid'];

    $sqlquery mysql_query("SELECT student_id from admin WHERE userid = '$_POST[userid]'") or die( mysql_error());
        if ( 
    mysql_num_rows $sqlquery ) == ) {
            exit ( 
    'No rows here - off for a pint' );
        }
        
    $result mysql_result ($sqlquery);
        echo 
    $result;

        
    $retcode $_POST['retcode']; 
         foreach (
    $retcode as $mod) {
             echo 
    $mod.'<br />';
        } 

  25. #25
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry I don't reply systematically. I'm rereading your posts and then think of things that could cause the trouble.

    You said that your INSERT query runs well. I don't doubt that , but I've added som errorcheking just to make sure.
    Echo the INSERT sqlquery to make sure the $_POST variables aren't empty and add mysql_error() after the query.
    PHP Code:
    $sqlquery "INSERT INTO admin (userid, password, firstname, surname, email, year) VALUES ('".$_POST['userid']."','".$_POST['password']."','".$_POST['firstname']."','".$_POST['surname']."','".$_POST['email']."','".$_POST['year']."')";
    echo 
    "INSERT query: " $sqlquery;
    $results mysql_query($sqlquery) or die(mysql_error()); 
    Sorry if my suggestion seems stupid. That's the best I can think of.

    -Helge


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •