SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot mcahill's Avatar
    Join Date
    May 2002
    Location
    Manchaug, MA, USA
    Posts
    180
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unable to pass variables into sql statement

    Here's the problem. This insert statement works, but it does not insert values for region id, author_id. I'm stumped. I can echo the variable I want to pass in, but it does not make it to my db. As always, any help appreciated


    PHP Code:
    //rec'ing my data from previous page
    $user $_POST['user'];
    $region $_POST['region'];
    echo 
    $user;
    echo 
    $region;    

    //now I need to get the actual userid and region id

    $q1 mysql_query("SELECT useruid FROM rtcms_user WHERE name = '$user'") or die("Error");
    $q2 mysql_query("SELECT reguid FROM rtcms_regions WHERE reg_name='$region'") or die("Error");

    //dump the data into objects

    $r1 mysql_fetch_object($q1);
    $r2 mysql_fetch_object($q2);

    //dump the objects into variables (I tried dumping the object info, but couldn't make it work)

    $useruid $r1->useruid;
    $reguid  $r2->reguid;      
    echo 
    $useruid;
    echo 
    $reguid;    

    //this code ought to look a little familiar...

    if ($submitjoke == "SUBMIT") {      
    $sql3 "INSERT INTO rtcms_fishwire SET 
    region_id='
    $useruid', 
    Author_uid='
    $reguid', 
    date=CURDATE()"
    ;
    if (@
    mysql_query($sql3)) {        
    echo(
    "<p>Your Report has been created.</p>");      
    }  else {        
    echo(
    "<p>Error adding creating Report: " .             
    mysql_error() . "</p>");      }


  2. #2
    Shiver me timbers!! anthony_irl's Avatar
    Join Date
    Aug 1999
    Location
    Dublin, Ireland
    Posts
    495
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll assume you've submitted the variables via a form.

    Hard to see a problem with it. What appears in the database fields?
    Anthony - How's tings?

    24 hours in a day, 24 beers in a case. Coincidence? I think not.
    Contact me by: PM ¦ Email ¦ NEW! Carrier Pigeon

  3. #3
    SitePoint Zealot mcahill's Avatar
    Join Date
    May 2002
    Location
    Manchaug, MA, USA
    Posts
    180
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Only the date...

    The variables are passed in from a form on the previous page. The sql is kicked off by clicking submit on the existing page.

    Thanks

    Mark

  4. #4
    Shiver me timbers!! anthony_irl's Avatar
    Join Date
    Aug 1999
    Location
    Dublin, Ireland
    Posts
    495
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could it be something to do with the case. Do you have 2 columns in the rtcms_fishwire table called: region_id and Author_uid?
    Anthony - How's tings?

    24 hours in a day, 24 beers in a case. Coincidence? I think not.
    Contact me by: PM ¦ Email ¦ NEW! Carrier Pigeon

  5. #5
    SitePoint Zealot mcahill's Avatar
    Join Date
    May 2002
    Location
    Manchaug, MA, USA
    Posts
    180
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Checked that...

    Since I'm knew at php, I was warned by someone here about case sensitive stuff. The case on the DB fields is correct (although in retrospect, I don't know why I mixed case).

    I have also tried the sql statement in phpmyadmin, plugging in values for the variables and it runs fine.

    My suspicion is that somehow the variables for userid and regionid are getting not being recognized when the user clicks the submit button.

  6. #6
    Shiver me timbers!! anthony_irl's Avatar
    Join Date
    Aug 1999
    Location
    Dublin, Ireland
    Posts
    495
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can u post the full code for the second page in the sequence (the page that does the insert)?
    Anthony - How's tings?

    24 hours in a day, 24 beers in a case. Coincidence? I think not.
    Contact me by: PM ¦ Email ¦ NEW! Carrier Pigeon

  7. #7
    SitePoint Zealot mcahill's Avatar
    Join Date
    May 2002
    Location
    Manchaug, MA, USA
    Posts
    180
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Here it is...

    I added a line to the insert statement that sets the status on the report to "unsubmitted" - it works. Still no variables.




    PHP Code:
    <?php
     
    //connect string works
    $dbcnx = @mysql_connect("host""user""pass");
    if (!
    $dbcnx) { 
     echo( 
    "<p>Unable to connect to the " .      
    "database server at this time.</p>" );  
    exit();
    }

    mysql_select_db("db"$dbcnx);

    if (! @
    mysql_select_db("db") ) { 
     echo( 
    "<p>Unable to locate the RT" .        
        
    "database at this time.</p>" ); 
         exit();
    }
    $user $_POST['user'];
    $region $_POST['region'];
    echo 
    $user;
    echo 
    $region;    

    $q1 mysql_query("SELECT useruid FROM rtcms_user WHERE name = '$user'") or die("Error");
    $q2 mysql_query("SELECT reguid FROM rtcms_regions WHERE reg_name='$region'") or die("Error");

    $r1 mysql_fetch_object($q1);
    $r2 mysql_fetch_object($q2);

    $useruid $r1->useruid;
    $reguid  $r2->reguid;      
    echo 
    $useruid;
    echo 
    $reguid;    

    if (
    $submitjoke == "SUBMIT") {      
    $sql3 "INSERT INTO rtcms_fishwire SET 
    status='unsubmitted',
    region_id='
    $useruid', 
    Author_uid='
    $reguid', 
    date=CURDATE()"
    ;
    if (@
    mysql_query($sql3)) {        
    echo(
    "<p>Your Report has been created.</p>");      
    }  else {        
    echo(
    "<p>Error adding creating Report: " .             
    mysql_error() . "</p>");      }
    }
            
    ?>
    <html>
    <head>
    <title>Untitled</title>
    </head>
    <body>
    <p>Click here to create a new report:
    <form action="<?=$PHP_SELF?>" method="post">
    <input type="submit" name="submitjoke" value="SUBMIT" /></p></form>


    </body>
    </html>

  8. #8
    gingham dress, army boots... silver trophy redux's Avatar
    Join Date
    Apr 2002
    Location
    Salford / Manchester / UK
    Posts
    4,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i think you might want to review the correct syntax for an INSERT statement there

    it's INSERT INTO tablename (columns) VALUES (values), and not INSERT INTO tablename SET column=value (that's an UPDATE, once you already have a record in there you do UPDATE tablename SET column=value WHERE condition)

    anyhows...give this a whirl: change
    PHP Code:
    $sql3 "INSERT INTO rtcms_fishwire SET 
    status='unsubmitted',
    region_id='
    $useruid', 
    Author_uid='
    $reguid', 
    date=CURDATE()"

    to
    PHP Code:
    $sql3 "INSERT INTO rtcms_fishwire (status,region_id,Author_uid,date) VALUES ('unsubmitted','$userid','$reguid',CURDATE())"
    also...if region_id and Author_uid are numbers, you can drop the quotes around them...
    PHP Code:
    $sql3 "INSERT INTO rtcms_fishwire (status,region_id,Author_uid,date) VALUES ('unsubmitted',$userid,$reguid,CURDATE())"
    re·dux (adj.): brought back; returned. used postpositively
    [latin : re-, re- + dux, leader; see duke.]
    WaSP Accessibility Task Force Member
    splintered.co.uk | photographia.co.uk | redux.deviantart.com

  9. #9
    Shiver me timbers!! anthony_irl's Avatar
    Join Date
    Aug 1999
    Location
    Dublin, Ireland
    Posts
    495
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    AHA!! Got it. Change it to this:
    PHP Code:
    <?php
     
    // we include the database stuff first as both eventualities will need a db connection

    $dbcnx = @mysql_connect("host""user""pass");
    if (!
    $dbcnx) { 
     echo( 
    "<p>Unable to connect to the " .      
    "database server at this time.</p>" );  
    exit();
    }

    if (! @
    mysql_select_db("db") ) { 
     echo( 
    "<p>Unable to locate the RT" .        
        
    "database at this time.</p>" ); 
         exit();
    }

    if (
    $submitjoke == "SUBMIT") {      
    $sql3 "INSERT INTO rtcms_fishwire SET 
    status='unsubmitted',
    region_id='
    $useruid', 
    Author_uid='
    $reguid', 
    date=CURDATE()"
    ;
    if (@
    mysql_query($sql3)) {        
    echo(
    "<p>Your Report has been created.</p>");      
    }  else {        
    echo(
    "<p>Error adding creating Report: " .             
    mysql_error() . "</p>");      }
    }
    else 
    //If the page has yet to be submitted 
    {

    $user $_POST['user'];
    $region $_POST['region'];
    echo 
    $user;
    echo 
    $region;    

    $q1 mysql_query("SELECT useruid FROM rtcms_user WHERE name = '$user'") or die("Error");
    $q2 mysql_query("SELECT reguid FROM rtcms_regions WHERE reg_name='$region'") or die("Error");

    $r1 mysql_fetch_object($q1);
    $r2 mysql_fetch_object($q2);

    $useruid $r1->useruid;
    $reguid  $r2->reguid;      
    ?>
    <html>
    <head>
    <title>Untitled</title>
    </head>
    <body>
    <p>Click here to create a new report:
    <form action="<?=$PHP_SELF?>" method="post">
    <!-- The values need to be stored and sent across as the page is reloading and will not hold the values for the insert otherwise -->
    <input type="hidden" name="useruid" value="<?=$useruid?>">
    <input type="hidden" name="reguid" value="<?=$reguid?>">
    <input type="submit" name="submitjoke" value="SUBMIT" /></p></form>


    </body>
    </html>
    <?php //The end brace for the else ?>
    Last edited by anthony_irl; May 22, 2002 at 08:02.
    Anthony - How's tings?

    24 hours in a day, 24 beers in a case. Coincidence? I think not.
    Contact me by: PM ¦ Email ¦ NEW! Carrier Pigeon

  10. #10
    SitePoint Zealot mcahill's Avatar
    Join Date
    May 2002
    Location
    Manchaug, MA, USA
    Posts
    180
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    That did it!

    A virtual cold frosty mug for you!

    Thanks, I see where I went wrong now. As always, the help is greatly appreciated.

    Mark


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
  •