Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    New Coder
    Join Date
    Nov 2007
    Location
    Maine
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question PHP add script wont work

    so i have it customers can click a purchase link & it takes the fields from customer & vehicles table & adds it to an empty purchases table but its not working (not adding to purchase table) heres my code
    PHP Code:
    <? 
    session_start
    ();  
    if ((
    $_SESSION[auth]='yes') and ($_SESSION[level]==3)) { 

    include 
    'connect.inc.php'
     
    $id=$_GET[id]; 
     
    $query="SELECT * FROM vehicles,customers"
     
    $result=mysql_query($query); 
      

     
    $num=mysql_numrows($result); 
      
      
      
      
     
    $i=0

    while  (
    $i <$num) { 
    $vid=mysql_result($result,$i,"vid"); 
    $cid=mysql_result($result,$i,"cid"); 
    $year=mysql_result($result,$i,"year"); 
    $make=mysql_result($result,$i,"make"); 
    $model=mysql_result($result,$i,"model"); 
    $price=mysql_result($result,$i,"price"); 
    $fname=mysql_result($result,$i,"fname"); 
    $lname=mysql_result($result,$i,"lname"); 


    $i++; 


      
    $query="insert into purchases (vid,cid,year,make,model,price,fname,lname) values('$vid','$cid','$year','$make','$model','$price','$fname','$lname')"
    $result=mysql_query($query); 
    mysql_close(); 

    echo 
    "you have just bought $year $make $model for $price"

     else { 
    header('Location:index.php'); 



    ?>

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Check your queries for errors.

  • #3
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    dont the INSERT and INTO and VALUE in the sql query have to be capitals. Like you have done with SELECT and DONE for the other query
    You can not say you know how to do something, until you can teach it to someone else.

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    no those clauses are all case-INsensitive.

  • #5
    New Coder
    Join Date
    Nov 2007
    Location
    Maine
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fumigator View Post
    Check your queries for errors.
    how so? do you mean run it in the command line? does anyone have quick fix for this or see whats wrong?

  • #6
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    Have you checked all the spellings of the fields are the same in the database as they are in the code
    You can not say you know how to do something, until you can teach it to someone else.

  • #7
    New Coder
    Join Date
    May 2008
    Location
    Michigan
    Posts
    66
    Thanks
    7
    Thanked 0 Times in 0 Posts
    At the end of your INSERT INTO query, include an or die(mysql_error()) line. The end result will look like:

    Code:
    $query="insert into purchases (vid,cid,year,make,model,price,fname,lname) values('$vid','$cid','$year','$make','$model','$price','$fname','$lname')" or die(mysql_error());
    This way, if there is an error happening at this query, you will be notified of it.

  • #8
    New Coder
    Join Date
    Nov 2007
    Location
    Maine
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by dharvell View Post
    At the end of your INSERT INTO query, include an or die(mysql_error()) line. The end result will look like:

    Code:
    $query="insert into purchases (vid,cid,year,make,model,price,fname,lname) values('$vid','$cid','$year','$make','$model','$price','$fname','$lname')" or die(mysql_error());
    This way, if there is an error happening at this query, you will be notified of it.
    where would the error (if any) display?

  • #9
    New Coder
    Join Date
    Nov 2007
    Location
    Maine
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by timgolding View Post
    Have you checked all the spellings of the fields are the same in the database as they are in the code
    ya...im setting the code (queries & everything) up correctly rnt i?

  • #10
    New Coder
    Join Date
    May 2008
    Location
    Michigan
    Posts
    66
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by jmoney3457 View Post
    where would the error (if any) display?
    Typically, the error would be displayed right in the browser. Depending on how your page is set up, it could be anywhere. Most of the time I have done this, my errors are the only thing showing up on an otherwise white screen. However, that might just be how I set up my pages.

    More correctly, the error will show up where the query is executing, if that makes any sense...?

  • #11
    New Coder
    Join Date
    Nov 2007
    Location
    Maine
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by dharvell View Post
    Typically, the error would be displayed right in the browser. Depending on how your page is set up, it could be anywhere. Most of the time I have done this, my errors are the only thing showing up on an otherwise white screen. However, that might just be how I set up my pages.

    More correctly, the error will show up where the query is executing, if that makes any sense...?
    somewhat to me lol..but can anyone see mistakes I made like did i not set up my queries correctly or have typo etc? ..

  • #12
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Change this
    PHP Code:
    $result=mysql_query($query); 
    mysql_close(); 
    to this
    PHP Code:
    $result=mysql_query($query) or die(mysql_error().'<br>SQL:' $query); 
    mysql_close(); 
    You'll get an error if there is one. I think it might be a scope issue or your variables aren't being stet properly. I suggest you echo out your variables to see what their values are.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #13
    New Coder
    Join Date
    Nov 2007
    Location
    Maine
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by _Aerospace_Eng_ View Post
    Change this
    PHP Code:
    $result=mysql_query($query); 
    mysql_close(); 
    to this
    PHP Code:
    $result=mysql_query($query) or die(mysql_error().'<br>SQL:' $query); 
    mysql_close(); 
    You'll get an error if there is one. I think it might be a scope issue or your variables aren't being stet properly. I suggest you echo out your variables to see what their values are.
    its displaying the variables fine when i echo them its just not adding it to the empty purchase table..

  • #14
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    So you made the changes I told you to make and you got NO errors?
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #15
    New Coder
    Join Date
    Nov 2007
    Location
    Maine
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts
    correct, however i fixed it thx


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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