www.webdeveloper.com
Results 1 to 15 of 15

Thread: php code help?? urgent - eror in result

  1. #1
    Join Date
    Jan 2014
    Location
    Pakistan
    Posts
    45

    php code help?? urgent - eror in result

    hello friends i want to show record on firstname condition..i create a html form to get firstname input from user and a submit button...and the result.phph file including code given below...but it give me error on highlighted line..please tell me what i do..please correct this code....
    <?php
    $con=mysqli_connect("abc.com","college","College","college");
    // Check connection
    if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $result = mysqli_query($con,"SELECT * FROM student WHERE FirstName=$_POST["firstname"]);

    echo "<table border='1'>
    <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Roll_Num</th>
    <th>Marks</th>
    <th>Attendance</th>
    </tr>";

    while($row = mysqli_fetch_array($result))
    {
    echo "<tr>";
    echo "<td>" . $row['FirstName'] . "</td>";
    echo "<td>" . $row['LastName'] . "</td>";
    echo "<td>" . $row['Roll_Num'] . "</td>";
    echo "<td>" . $row['Marks'] . "</td>";
    echo "<td>" . $row['Attendance'] . "</td>";
    echo "</tr>";
    }
    echo "</table>";

    mysqli_close($con);
    ?>
    PR 2 Do-f0llow links

    like my sites i'll like your choice

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,175
    PHP Code:
    $result mysqli_query($con,"SELECT * FROM student WHERE FirstName='".mysqli_real_escape_string($con$_POST["firstname"])."'"); 
    (Though I would used prepared statements and bound parameters instead, but I figure if it's truly "urgent", this is probably all you want.)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jan 2014
    Location
    Pakistan
    Posts
    45
    Quote Originally Posted by NogDog View Post
    PHP Code:
    $result mysqli_query($con,"SELECT * FROM student WHERE FirstName='".mysqli_real_escape_string($con$_POST["firstname"])."'"); 
    (Though I would used prepared statements and bound parameters instead, but I figure if it's truly "urgent", this is probably all you want.)
    thanks dear i'll try it now
    PR 2 Do-f0llow links

    like my sites i'll like your choice

  4. #4
    Join Date
    Jan 2014
    Location
    Pakistan
    Posts
    45
    Quote Originally Posted by tanvirzafar View Post
    thanks dear i'll try it now
    i try this code....i did not give me any error but also it doesn't show any record against firstname..
    PR 2 Do-f0llow links

    like my sites i'll like your choice

  5. #5
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    536
    Show the code again

  6. #6
    Join Date
    Jan 2014
    Location
    Pakistan
    Posts
    45
    Quote Originally Posted by ginerjm View Post
    Show the code again
    <?php
    $con=mysqli_connect("abc.com","college","College","college");
    // Check connection
    if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $result = mysqli_query($con,"SELECT * FROM student WHERE FirstName=$_POST["firstname"]);

    echo "<table border='1'>
    <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Roll_Num</th>
    <th>Marks</th>
    <th>Attendance</th>
    </tr>";

    while($row = mysqli_fetch_array($result))
    {
    echo "<tr>";
    echo "<td>" . $row['FirstName'] . "</td>";
    echo "<td>" . $row['LastName'] . "</td>";
    echo "<td>" . $row['Roll_Num'] . "</td>";
    echo "<td>" . $row['Marks'] . "</td>";
    echo "<td>" . $row['Attendance'] . "</td>";
    echo "</tr>";
    }
    echo "</table>";

    mysqli_close($con);
    ?>
    PR 2 Do-f0llow links

    like my sites i'll like your choice

  7. #7
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    536
    Are you serious? You showed the same old code again.

  8. #8
    Join Date
    Jan 2014
    Location
    Pakistan
    Posts
    45
    Quote Originally Posted by ginerjm View Post
    Are you serious? You showed the same old code again.
    no no...there is nothing to show in output....only columns names are show ..no record
    PR 2 Do-f0llow links

    like my sites i'll like your choice

  9. #9
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    536
    Never mind - I asked to see your corrected code from the previous suggestions and you aren't showing it.

    good by.

  10. #10
    Join Date
    Jan 2014
    Location
    Pakistan
    Posts
    45
    PHP Code:
    <?php
    $con
    =mysqli_connect("abc.com","college","College","college");
    // Check connection
    if (mysqli_connect_errno())
    {
    echo 
    "Failed to connect to MySQL: " mysqli_connect_error();
    }

    $result mysqli_query($con,"SELECT * FROM student WHERE FirstName='".mysqli_real_escape_string($con$_POST["firstname"])."'"); 

    echo 
    "<table border='1'>
    <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Roll_Num</th>
    <th>Marks</th>
    <th>Attendance</th>
    </tr>"
    ;

    while(
    $row mysqli_fetch_array($result))
    {
    echo 
    "<tr>";
    echo 
    "<td>" $row['FirstName'] . "</td>";
    echo 
    "<td>" $row['LastName'] . "</td>";
    echo 
    "<td>" $row['Roll_Num'] . "</td>";
    echo 
    "<td>" $row['Marks'] . "</td>";
    echo 
    "<td>" $row['Attendance'] . "</td>";
    echo 
    "</tr>";
    }
    echo 
    "</table>";

    mysqli_close($con);
    ?>
    Last edited by NogDog; 02-25-2014 at 03:16 PM. Reason: added [php] tags around code
    PR 2 Do-f0llow links

    like my sites i'll like your choice

  11. #11
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,175
    Try something like this in place of the line where you do the query:
    PHP Code:
    $sql "SELECT * FROM student WHERE FirstName='".mysqli_real_escape_string($con$_POST["firstname"])."'";
    $result mysqli_query($con$sql); 
    if(
    $result == false) {
        throw new 
    Exception("Query failed: ".mysqli_error($con).PHP_EOL.$sql);
    }
    // rest of code... 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  12. #12
    Join Date
    Jan 2014
    Location
    Pakistan
    Posts
    45
    not work
    it does't get any record....only columns labels are show.
    PR 2 Do-f0llow links

    like my sites i'll like your choice

  13. #13
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,175
    If that's the case, then my best guess is that either:
    [*]There is no exact match in the database for the first name you are supplying (remembering it is case-sensitive and white-space matters), or[*]There is some flaw elsewhere that is corrupting the value in $_POST['firstname'] (which maybe you could var_dump() right before you use it, to verify what is in it).

    Simplest way to debug might be to just echo out $sql after you define it, and then copy and paste it into a MySQL command line session or phpMyAdmin SQL window and see what it returns.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  14. #14
    Join Date
    Jan 2014
    Location
    Pakistan
    Posts
    45
    Quote Originally Posted by NogDog View Post
    If that's the case, then my best guess is that either:
    [*]There is no exact match in the database for the first name you are supplying (remembering it is case-sensitive and white-space matters), or[*]There is some flaw elsewhere that is corrupting the value in $_POST['firstname'] (which maybe you could var_dump() right before you use it, to verify what is in it).

    Simplest way to debug might be to just echo out $sql after you define it, and then copy and paste it into a MySQL command line session or phpMyAdmin SQL window and see what it returns.
    thanks bro..i got it and fixed the error.....it is case sensitive.....i have another question....i want to show text massage if there is no matched record found in the database....give me code for that bro...also tell me in each .php page in which i enter queries..i have to connect database???
    PR 2 Do-f0llow links

    like my sites i'll like your choice

  15. #15
    Join Date
    Feb 2014
    Location
    Canada
    Posts
    155
    Quote Originally Posted by tanvirzafar View Post
    thanks bro..i got it and fixed the error.....it is case sensitive.....i have another question....i want to show text massage if there is no matched record found in the database....give me code for that bro...also tell me in each .php page in which i enter queries..i have to connect database???
    For your first part, you can simply print it to the screen with an echo or be fancier and have an alert created through JavaScript or jQuery. For example:

    PHP Code:
    #if there is no match 
    echo("Found no match for $record");

    #putting the result in a HTML element to control its position:
    echo("<h4 id = 'noMatch' >Found no match for $record</h4>");

    # or being fancier and use JS (bit sloppy in this example):
    echo("<script type = 'text/javascript'>");
    echo(
    "alert('No match was found for $record');");
    echo(
    "</script>"); 
    For your second question, it depends whether you use a global variable to store your database connection, have a singleton class, etc... . You can certainly connect to your database in each file if you don't have any of those in place.

Thread Information

Users Browsing this Thread

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

Posting Permissions

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



Recent Articles