Page 1 of 2 12 LastLast
Results 1 to 16 of 19

Thread: PHP/MySQL What have I done wrong?

  1. #1
    Senior Member crazyfool's Avatar
    Join Date
    Jan 2008
    Posts
    761
    Thanks
    77
    Thanked
    38 times in 38 posts
    • crazyfool's system
      • Motherboard:
      • Striker Extreme
      • CPU:
      • Q6600
      • Memory:
      • OCZ 6400 2 x 2GB
      • Storage:
      • Samsung Spinpoint 500GB
      • Graphics card(s):
      • BFG 8800 GT OC 512MB
      • PSU:
      • Coolermaster Real Power Pro 850w
      • Case:
      • Coolermaster CM-690
      • Operating System:
      • XP, Vista, Windows 7 & Ubuntu
      • Monitor(s):
      • Samsung 2232BW

    PHP/MySQL What have I done wrong?

    hey guys,
    This is my first venture into php/mysql but I have found a couple of good tutorials on the net which I have been using. I've got mysql setup as localhost and the username is user, the password is pass. DB name is test and the table I'm using is called testtable. Basically all I'm trying to do at the moment is create a form in html which will submit its contents to the table. This is what I have so far:
    PHP Script (test.php)
    <?
    $username="user";
    $password="pass";
    $database="test";

    $forename=$_POST['forename'];
    $surname=$_POST['surname'];
    $address=$_POST['address'];
    $postcode=$_POST['postcode'];

    mysql_connect(localhost,$username,$password) or die('Error connecting to mysql');

    @mysql_select_db($database) or die("Unable to select database");

    $query = "INSERT INTO testtable VALUES ('','$forename','$surname','$address','$postcode')";

    mysql_query($query);

    mysql_close();
    ?>
    HTML
    <form action="test.php" method="post">
    <input name="forename" type="text" /><br />
    <input name="surname" type="text" /><br />
    <input name="address" type="text" /><br />
    <input name="postcode" type="text" /><br />
    <input name="submit" type="submit" value="submit" /></form>
    All that happens is when I click submit a blank tab opens titled test.php and thats all that happens. It's probably something stupid but I read the tutorial real close and I can't see what I have done wrong. Any help would be appreciated. Thanks

  2. #2
    Member
    Join Date
    Aug 2009
    Posts
    182
    Thanks
    3
    Thanked
    23 times in 22 posts
    • barry2811's system
      • Motherboard:
      • Gigabyte GA-Z87-HD3
      • CPU:
      • Intel i5 4670K (cooled by Noctua NH-D14)
      • Memory:
      • 2 x 8GB Corsair
      • Storage:
      • Samsung Pro 128GB Boot Drive, 1 x 6TB Seagate, 1 x 3TB Seagate and 256GB Crucial SSD for games
      • Graphics card(s):
      • Asus GTX970 Strix
      • PSU:
      • EVGA SuperNova 650 G2
      • Case:
      • NZXT S340 Elite
      • Operating System:
      • Windows 10
      • Monitor(s):
      • Viewsonic VG2439M-LED
      • Internet:
      • Sky Fibre

    Re: PHP/MySQL What have I done wrong?

    It's probably a null exception or unescaped string. Try echoing out mysql_error() after the mysql_query();

    You could also try providing the field names to the query, ie, the full query syntax. eg,

    Code:
    $query = "INSERT INTO testtable (id, forename, surname, address, postcode) VALUES ('','$forename','$surname','$address','$postcode')";
    Better yet, if the id field is auto-incrementing let the database do it;

    Code:
    $query = "INSERT INTO testtable (forename, surname, address, postcode) VALUES ('$forename','$surname','$address','$postcode')";

  3. #3
    Not a good person scaryjim's Avatar
    Join Date
    Jan 2009
    Location
    Gateshead
    Posts
    15,196
    Thanks
    1,231
    Thanked
    2,291 times in 1,874 posts
    • scaryjim's system
      • Motherboard:
      • Dell Inspiron
      • CPU:
      • Core i5 8250U
      • Memory:
      • 2x 4GB DDR4 2666
      • Storage:
      • 128GB M.2 SSD + 1TB HDD
      • Graphics card(s):
      • Radeon R5 230
      • PSU:
      • Battery/Dell brick
      • Case:
      • Dell Inspiron 5570
      • Operating System:
      • Windows 10
      • Monitor(s):
      • 15" 1080p laptop panel

    Re: PHP/MySQL What have I done wrong?

    Two things: firstly your php script doesn't contain any code to output anything to a browser, so it will always show a blank page in a browser.

    Secondly, what web server are you using, and are you sure you've set it up correctly to work with php? It might be worth trying something simple like a php Hello World example to make sure that your php scripts are actually running, before you start trying to query databases!

  4. #4
    Senior Member crazyfool's Avatar
    Join Date
    Jan 2008
    Posts
    761
    Thanks
    77
    Thanked
    38 times in 38 posts
    • crazyfool's system
      • Motherboard:
      • Striker Extreme
      • CPU:
      • Q6600
      • Memory:
      • OCZ 6400 2 x 2GB
      • Storage:
      • Samsung Spinpoint 500GB
      • Graphics card(s):
      • BFG 8800 GT OC 512MB
      • PSU:
      • Coolermaster Real Power Pro 850w
      • Case:
      • Coolermaster CM-690
      • Operating System:
      • XP, Vista, Windows 7 & Ubuntu
      • Monitor(s):
      • Samsung 2232BW

    Re: PHP/MySQL What have I done wrong?

    ok i did the hello world and php is working correctly using xampp
    i did echo mysql_error() as well and I get a blank page again, no error message, nothing entered into the db, i'm not sure whats happening as the php script should output a message if it cant connect to the db or find the table
    EDIT: I just tried to run the script with the username, password and dbname empty and it did the same thing, no message like its supposed to... I dunno whats going on
    Last edited by crazyfool; 17-03-2010 at 11:41 PM.

  5. #5
    Senior Member
    Join Date
    Feb 2008
    Posts
    925
    Thanks
    4
    Thanked
    161 times in 148 posts
    • smargh's system
      • Motherboard:
      • Gigabyte GA-EP45-UD3P
      • CPU:
      • Xeon E5450 with 775-to-771 Mod
      • Memory:
      • 16GB Crucial
      • Storage:
      • Intel X25-M G2 80GB/Adaptec 3405 4x 2TB Ultrastar RAID1 / 1x 6TB Hitachi He6 / Dying 2TB Samsung
      • Graphics card(s):
      • GTX 750 Ti
      • PSU:
      • Seasonic X-560
      • Case:
      • Lian-Li PC-A71
      • Operating System:
      • Windows 7 Ultimate 64bit
      • Monitor(s):
      • BenQ G2400WD
      • Internet:
      • Really Crap ADSL2 <3Mbit

    Re: PHP/MySQL What have I done wrong?

    Two things need fixing.

    1. Your PHP appears to configured to only allow <?php - it won't work with just <? unless you enable set short_open_tag = On in your php.ini

    2. mysql_connect(localhost - that's not going to work. It'll time out. It should be either mysql_connect("127.0.0.1" or at least put "localhost" in quotes - "127.0.0.1" is safest though.

    Add print "affected rows: " . mysql_affected_rows(); under the query to quickly be told that it's working okay.

    I tend to put header("Content-type: text/plain"); at the top of the script during diagnostics - it stops the browser trying to interpret the results as HTML.
    Last edited by smargh; 18-03-2010 at 03:35 AM.

  6. #6
    HEXUS webmaster Steve's Avatar
    Join Date
    Nov 2003
    Posts
    14,283
    Thanks
    293
    Thanked
    841 times in 476 posts

    Re: PHP/MySQL What have I done wrong?

    Not wanting to throw more stuff at you, but as soon as you've got this bit working, spend some time reading about SQL injection attacks and why your code is dangerous right now
    PHP Code:
    $s = new signature();
    $s->sarcasm()->intellect()->font('Courier New')->display(); 

  7. #7
    Seething Cauldron of Hatred TheAnimus's Avatar
    Join Date
    Aug 2005
    Posts
    17,168
    Thanks
    803
    Thanked
    2,152 times in 1,408 posts

    Re: PHP/MySQL What have I done wrong?

    Quote Originally Posted by Steve View Post
    Not wanting to throw more stuff at you, but as soon as you've got this bit working, spend some time reading about SQL injection attacks and why your code is dangerous right now
    Magic Quotes Runtime.... Isn't that disabled by default now and probably will stop the script running?

    I don't know, because I would sooner write pearl than PHP with MySQL.
    throw new ArgumentException (String, String, Exception)

  8. #8
    HEXUS webmaster Steve's Avatar
    Join Date
    Nov 2003
    Posts
    14,283
    Thanks
    293
    Thanked
    841 times in 476 posts

    Re: PHP/MySQL What have I done wrong?

    I'd never trust the server configuration over my code, and yes I believe magic quotes tends to be disabled now. Prepared statements are better too.
    PHP Code:
    $s = new signature();
    $s->sarcasm()->intellect()->font('Courier New')->display(); 

  9. #9
    Senior Member crazyfool's Avatar
    Join Date
    Jan 2008
    Posts
    761
    Thanks
    77
    Thanked
    38 times in 38 posts
    • crazyfool's system
      • Motherboard:
      • Striker Extreme
      • CPU:
      • Q6600
      • Memory:
      • OCZ 6400 2 x 2GB
      • Storage:
      • Samsung Spinpoint 500GB
      • Graphics card(s):
      • BFG 8800 GT OC 512MB
      • PSU:
      • Coolermaster Real Power Pro 850w
      • Case:
      • Coolermaster CM-690
      • Operating System:
      • XP, Vista, Windows 7 & Ubuntu
      • Monitor(s):
      • Samsung 2232BW

    Re: PHP/MySQL What have I done wrong?

    well it turns out it was the php tags... I knew it would be something stupid like that
    As fan as the code being dangerous atm I'm just playing around with it, now that I'm getting the basics I can start looking into that... thanks for all the advice and help guys

  10. #10
    HEXUS webmaster Steve's Avatar
    Join Date
    Nov 2003
    Posts
    14,283
    Thanks
    293
    Thanked
    841 times in 476 posts

    Re: PHP/MySQL What have I done wrong?

    Good stuff. Validating input is an essential part of any program/script, so once you've got the basic syntax sorted, it should always be next on your list of things to get to grips with. It's too easy to miss things if you leave it too late.
    PHP Code:
    $s = new signature();
    $s->sarcasm()->intellect()->font('Courier New')->display(); 

  11. #11
    Seething Cauldron of Hatred TheAnimus's Avatar
    Join Date
    Aug 2005
    Posts
    17,168
    Thanks
    803
    Thanked
    2,152 times in 1,408 posts

    Re: PHP/MySQL What have I done wrong?

    throw new ArgumentException (String, String, Exception)

  12. #12
    HEXUS webmaster Steve's Avatar
    Join Date
    Nov 2003
    Posts
    14,283
    Thanks
    293
    Thanked
    841 times in 476 posts

    Re: PHP/MySQL What have I done wrong?

    One of my favourites
    PHP Code:
    $s = new signature();
    $s->sarcasm()->intellect()->font('Courier New')->display(); 

  13. #13
    Senior Member crazyfool's Avatar
    Join Date
    Jan 2008
    Posts
    761
    Thanks
    77
    Thanked
    38 times in 38 posts
    • crazyfool's system
      • Motherboard:
      • Striker Extreme
      • CPU:
      • Q6600
      • Memory:
      • OCZ 6400 2 x 2GB
      • Storage:
      • Samsung Spinpoint 500GB
      • Graphics card(s):
      • BFG 8800 GT OC 512MB
      • PSU:
      • Coolermaster Real Power Pro 850w
      • Case:
      • Coolermaster CM-690
      • Operating System:
      • XP, Vista, Windows 7 & Ubuntu
      • Monitor(s):
      • Samsung 2232BW

    Re: PHP/MySQL What have I done wrong?

    hey guys,
    Same project, question is a php/java one but didn't reallly want to open a new thread jus for it as it is a pretty simple one.
    Basically I have a text box called tb and I want to get the value it contains to use in a java method.
    So far I've got:
    var tbox = document.getElementById("tb").value;
    However the java code stops once it gets here without any error message so obviously this cant be right. Any ideas, or do you need all the html/java code?

  14. #14
    HEXUS webmaster Steve's Avatar
    Join Date
    Nov 2003
    Posts
    14,283
    Thanks
    293
    Thanked
    841 times in 476 posts

    Re: PHP/MySQL What have I done wrong?

    [pedant]It's javascript not java - very different[/pedant]

    If you can't find the element in question or it doesn't have a value property, it's likely to bomb out. My suggestion is get the Firebug plugin for Firefox - its error console should tell you precisely what's going wrong.
    PHP Code:
    $s = new signature();
    $s->sarcasm()->intellect()->font('Courier New')->display(); 

  15. #15
    Seething Cauldron of Hatred TheAnimus's Avatar
    Join Date
    Aug 2005
    Posts
    17,168
    Thanks
    803
    Thanked
    2,152 times in 1,408 posts

    Re: PHP/MySQL What have I done wrong?

    odds are, and this is just guess work (was years ago i did any jscript!) the document hasn't finished loading, so the element isn't there yet.
    throw new ArgumentException (String, String, Exception)

  16. #16
    PHP Geek Flash477's Avatar
    Join Date
    Dec 2008
    Location
    Devon
    Posts
    822
    Thanks
    51
    Thanked
    72 times in 65 posts

    Re: PHP/MySQL What have I done wrong?

    Make sure that the textbox has the id tb, not the name of it, as getElementById has a clue in the last two letters

    Or failing that, as already said it is running too soon, and the document is not fully loaded.

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Ordered the wrong item by mistake
    By Ironbuket in forum SCAN.care@HEXUS
    Replies: 6
    Last Post: 04-06-2009, 10:51 AM
  2. scan support number? wrong order
    By looney in forum SCAN.care@HEXUS
    Replies: 16
    Last Post: 09-05-2008, 01:09 AM
  3. Connecting a battery the wrong way round?
    By AD-15 in forum General Discussion
    Replies: 4
    Last Post: 21-01-2008, 11:31 PM
  4. wireless keyboard displaying wrong characters
    By colincliff in forum Help! Quick Relief From Tech Headaches
    Replies: 4
    Last Post: 16-10-2007, 04:23 PM
  5. Windows xp repair gone wrong
    By jkh13 in forum Help! Quick Relief From Tech Headaches
    Replies: 3
    Last Post: 18-08-2005, 09:22 AM

Posting Permissions

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