SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Resident Boozehound simontheak's Avatar
    Join Date
    Jun 2003
    Location
    _root.europe.uk.london
    Posts
    965
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Newbie Question regarding forms and databases

    I've just started mixing PHP with MySQL. I have written the script below which works fine. It access a particular database and prints the records out in a table:
    PHP Code:
    <?php
    print $_POST['name'];
    $link=@mysql_connect;
    if (!
    $link){
    print 
    "Couldn't connect to database";
    exit;
    }
    if(!@
    mysql_select_db("phpforflash")){
    print 
    "Couldn't find database";
    exit;
    }
    $query "SELECT * FROM news ORDER BY title ASC";
    $result = @mysql_query($query);

    while (
    $row mysql_fetch_array($result)){
    print 
    "<table border=1><tr>";
    print 
    "<td colspan=2><b>Record No.".$row['newsID'] . "</b><br></td>\n";
    print 
    "<tr><td>Title:</td><td>".$row['title'] . "<br>\n";
    print 
    "<tr><td>Author:</td><td>".$row['author'] . "<br>\n";
    print 
    "<tr><td>Plot:</td><td>".$row['body'] . "<br>\n";
    print 
    "</table>";
    print 
    "<p>";
    }

    ?>
    The next step (as I saw it) was to get PHP to search through the database for a particular variable. So I designed a simple form with it's action attribute pointing to my database.php script (above). Here's the HTML for the form:
    PHP Code:
    <html>
    <
    form name="form1" method="get" action="database.php">
      <
    table width="80%" border="0" cellspacing="0" cellpadding="0">
        <
    tr>
          <
    td><input name="name" type="text" id="name"></td>
        </
    tr>
        <
    tr>
          <
    td><input type="submit" name="Submit" value="Submit"></td>
        </
    tr>
      </
    table>
    </
    form
    The problem is though that when I click on the "Submit" button, all I get is an error message saying that PHP couldn't find the database. So my question is, why does my first script find the database when I run it directly through a browser, but not when I try to run it after a form submission?

    Can someone put me right?

    Thanks very much for your help!

  2. #2
    SitePoint Enthusiast falfool's Avatar
    Join Date
    Jun 2003
    Location
    Amman
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why dont u put some parameters for mysql_connect? and try to get the "@" off some functions...

  3. #3
    Resident Boozehound simontheak's Avatar
    Join Date
    Jun 2003
    Location
    _root.europe.uk.london
    Posts
    965
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by falfool
    try to get the "@" off some functions...
    I'm sorry - I'm not quite sure I understand what you mean by that

  4. #4
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $link=@mysql_connect
    Preceding a funtion call by "@" negates any error messages usually sent by it. If you remove the "@", you will see that PHP cannot connect to the mysql server, the reason being that you have not provided any paramters.

    See mysql_connect for more details

    Cheers!
    My name is Steve, and I'm a super-villian.

  5. #5
    Resident Boozehound simontheak's Avatar
    Join Date
    Jun 2003
    Location
    _root.europe.uk.london
    Posts
    965
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers - thanks alot!

  6. #6
    Resident Boozehound simontheak's Avatar
    Join Date
    Jun 2003
    Location
    _root.europe.uk.london
    Posts
    965
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry to be a pain.

    I've now got the form submission sorted out, but can someone tell me what is wrong with this line of code:
    PHP Code:
    $query "SELECT * FROM news WHERE author='$choice' ORDER BY title ASC"
    I've defined $choice at the beginning of the script, and I've tried the above line without quotation marks, but the browser can't seem to parse it.

  7. #7
    Resident Boozehound simontheak's Avatar
    Join Date
    Jun 2003
    Location
    _root.europe.uk.london
    Posts
    965
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's alright - got it!!


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
  •