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.
Results 1 to 2 of 2

Thread: mysql php help!

  1. #1
    New to the CF scene
    Join Date
    May 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    mysql php help!

    Hi,
    I'm new on mySQL. I want to validate user input from html form with mySQL row.

    ----------------------
    html>
    <head></head>
    <body>
    <form name="login" method="post" action="if.php">
    Lucky Number <input type="text" name="guess" />
    <input type="submit" value="Submit" />
    </form>
    </body>
    </html>



    and if.php is here

    --------------------------------
    <?php
    $dbhost = "localhost";
    $dbname = "gm";
    $dbuser = "root";
    $dbpass = "";

    //Connect to database

    mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
    mysql_select_db($dbname) or die(mysql_error());

    //table rows
    $guess = $_POST['guess'];
    $result = mysql_query("SELECT guessnum FROM secnum");
    $row = mysql_fetch_array( $result);

    if($_POST['guess']==$row) {
    echo "<p>Good Job!</p>";
    } else {
    echo "<p>Sorry!</p>";
    }
    ?>


    ---------------------------
    and my mysql database is like this
    Datebase name = gm
    table=secnum
    column=guessnum
    row(or column value)=1

  • #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
    if($_POST['guess']==$row) {
    That won't work because $row is an array. But more to the point, you should be able to use a WHERE clause on your query to determine if your row exists, rather than selecting _every row_ in your table and then using PHP to look for the row you're after.

    Look in the PHP manual at php.net for examples of how to use mysql_fetch_array(). Also look for some basic SQL tutorials on the internet.


  •  

    Posting Permissions

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