Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

Beginner: use WHERE in PHP-form with PHP_SELF

Codes here !

Moderators: macek, egami, gesf

Beginner: use WHERE in PHP-form with PHP_SELF

Postby mcgutschy » Wed Apr 09, 2014 2:01 pm

Hello to all,

greetings from Germany and ad first I have to apologize my bad English but hope it works anyway . A part of my problem is that I use PHP and MySQL only a few months in years. So I know in theory not just a little, but in practice and in detail rather few.

What I try is to made a form for a address-query, you put the postal code on and get all addresses with it.
The MySQL connection is in function and a simple SQL-SELECT instruction works too, but not much more. I don't now how to send from the form to the MySQL query.

In the code I have made a few comments which should explain my goal, look for line 18 and 24.
Code: Select all
<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="description" content="Adressen Datenbank">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Testside</title>
        <link rel="stylesheet" href="css/style.css">
        <link rel="author" href="humans.txt">
    </head>
    <body>
    <h1>Testside</h1>
    <form action="<? echo $PHP_SELF?>" method= "POST">
       <textarea cols="10" rows="1" name="suche" wrap=virtual></textarea>
       <input type="submit" value="Suchen">
    </form>
<?php
#$suche = $_POST["suche"];
$dp = mysql_connect("localhost", "pizzauser", "sommer");
mysql_select_db("pizzadb2", $dp);
$sql = "SELECT laden_name, vorwahl, telenr1, plz, ort,
linkname1, forum_link
FROM pizzeria_table";
#FROM pizzeria_table WHERE  plz == '$suche' ORDER BY laden_name ";
$result = mysql_query($sql);
$felder = mysql_num_fields($result);
$datensaetze = mysql_num_rows($result);
echo "<p>Anzahl der Datensätze: <b>$datensaetze</b></p>\n";
echo "<table border='1' cellspacing='0'>\n"; // Tabelle beginnen
echo "<tr>"; // Zeile erzeugen
for ($i = 0;$i < $felder;$i++) {
    $feldname = mysql_field_name($result, $i);
    echo "<th>$feldname</th>";
}
echo "</tr>\n"; // Zeile schließen

while ($row = mysql_fetch_assoc($result)) {
    echo "<tr>"; // Zeile erzeugen
    foreach ($row as $key => $value) {
        echo "<td>$value&nbsp;</td>";
    }
    echo "</tr>\n"; // Zeile schließen
}
echo "</table>\n"; // Tabelle schließen
mysql_close($dp);
?>
       
    </body>
</html>


Hope my code in not total rubbish.
:?

greetings,

gutschy
mcgutschy
New php-forum User
New php-forum User
 
Posts: 6
Joined: Wed Apr 09, 2014 12:21 pm

Re: Beginner: use WHERE in PHP-form with PHP_SELF

Postby seandisanti » Wed Apr 09, 2014 11:14 pm

mysql_ functions are insecure and deprecated, mysqli_ functions are on the way out also. Check out PDO for the best way to interact with MySql database. http://php.net/pdo It looks like a lot of info, but you can handle it, I promise.

Code: Select all
<?php
try {
    
$dbh = new PDO('mysql:host=localhost;dbname=test'$user$pass);
    foreach(
$dbh->query('SELECT * from FOO') as $row) {
        
print_r($row);
    }
    
$dbh null;
} catch (
PDOException $e) {
    print 
"Error!: " $e->getMessage() . "<br/>";
    die();
}
$sql "SELECT field1, field2, field3 FROM table WHERE field1 = " $dbh->quote($user_entered_string);
$result $dbh->query($sql,PDO::FETCH_ASSOC);

if (!
$result || $result->rowCount()==0){ //empty result or mysql error occurred
// code to gracefully fail goes here
}

while (
$row $result->fetch()){
     foreach(
$row as $field=>$value){ // this is just for example, you do not need this loop at all
     
echo 'Field: ' $field ' = ' $value;
     }
}
 
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Beginner: use WHERE in PHP-form with PHP_SELF

Postby mcgutschy » Thu Apr 10, 2014 11:43 am

Hi seandisanti,

thanks a lot for this much pretty looking code, I haven't tested yet but I just started with the PDO tutorial. I'm sure to thank you again when I'm done with the tutorial and tested the code.

THX, :D :D :D

gutschy
mcgutschy
New php-forum User
New php-forum User
 
Posts: 6
Joined: Wed Apr 09, 2014 12:21 pm

Re: Beginner: use WHERE in PHP-form with PHP_SELF

Postby seandisanti » Thu Apr 10, 2014 4:32 pm

No problem, I'm glad i could help
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Beginner: use WHERE in PHP-form with PHP_SELF

Postby mcgutschy » Fri Apr 11, 2014 11:09 am

Hi seandisanti,

I'm just come in the near of understanding what does PDO mean. But I am far from understanding the syntax.

This is what I've tested so far.
Error-message of the beginning:
Notice: Undefined index: suche in /var/www/tabelle/eingabe2/index3.php on line 20
But the first line of the database is printed.

If I press the send button the following error-message appears:
The requested URL /tabelle/eingabe2/<br /><b>Notice</b>: Undefined variable: PHP_SELF in <b>/var/www/tabelle/eingabe2/index3.php</b> on line <b>13</b><br /> was not found on this server.

At first it would be nice to bring query on, and of course I have no Idea how to bring the layout on later.

Here my try of the code.
Code: Select all
<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="description" content="Adressen Datenbank">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Testside</title>
        <link rel="stylesheet" href="css/style.css">
        <link rel="author" href="humans.txt">
    </head>
    <body>
    <h1>Testside</h1>
    <form action="<? echo $PHP_SELF?>" method= "POST">
       <textarea cols="10" rows="1" name="suche" wrap=virtual></textarea>
       <input type="submit" value="Suchen">
    </form>
<?php
try {
    $dbh = new PDO('mysql:host=localhost;dbname=dbname2', 'user', 'passwort');
    $suche = ($_POST['suche']);
    $sql = "SELECT laden_name, vorwahl, telenr1, plz, ort,
    linkname1, forum_link FROM pizzeria_table WHERE plz = " .$dbh->quote($suche);
    foreach($dbh->query($sql) as $row) {
        print_r($row);


        // $felder = mysql_num_fields($result);
        // $datensaetze = mysql_num_rows($result);
        // echo "<p>Anzahl der Datensätze: <b>$datensaetze</b></p>\n";
        // echo "<table border='1' cellspacing='0'>\n"; // Tabelle beginnen
        // echo "<tr>"; // Zeile erzeugen
        // for ($i = 0;$i < $felder;$i++) {
        //     $feldname = mysql_field_name($result, $i);
        //     echo "<th>$feldname</th>";
        // }
        // echo "</tr>\n"; // Zeile schließen

        // while ($row = mysql_fetch_assoc($result)) {
        //     echo "<tr>"; // Zeile erzeugen
        //     foreach ($row as $key => $value) {
        //         echo "<td>$value&nbsp;</td>";
        //     }
        //     echo "</tr>\n"; // Zeile schließen
        // }
        // echo "</table>\n"; // Tabelle schließen


    }
    $dbh = null;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}
?>       
    </body>
</html>


Hope you can give a solution and have a good weekend.

greetings,

gutschy
mcgutschy
New php-forum User
New php-forum User
 
Posts: 6
Joined: Wed Apr 09, 2014 12:21 pm

Re: Beginner: use WHERE in PHP-form with PHP_SELF

Postby seandisanti » Fri Apr 11, 2014 6:02 pm

you are having it query the database even when no information is passed. Try this:

Code: Select all

    <!doctype html>
    <html>
        <head>
            <meta charset="utf-8">
            <meta name="description" content="Adressen Datenbank">
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <title>Testside</title>
            <link rel="stylesheet" href="css/style.css">
            <link rel="author" href="humans.txt">
        </head>
        <body>
        <h1>Testside</h1>
        <form action="<? echo $PHP_SELF?>" method= "POST">
           <textarea cols="10" rows="1" name="suche" wrap=virtual></textarea>
           <input type="submit" value="Suchen">
        </form>
    <?php if ($_POST){
    try {
        
$dbh = new PDO('mysql:host=localhost;dbname=dbname2''user''passwort');
        
$suche = ($_POST['suche']);
        
$sql "SELECT laden_name, vorwahl, telenr1, plz, ort,
        linkname1, forum_link FROM pizzeria_table WHERE plz = " 
.$dbh->quote($suche);
        foreach(
$dbh->query($sql) as $row) {
            
print_r($row);


            
// $felder = mysql_num_fields($result);
            // $datensaetze = mysql_num_rows($result);
            // echo "<p>Anzahl der Datensätze: <b>$datensaetze</b></p>\n";
            // echo "<table border='1' cellspacing='0'>\n"; // Tabelle beginnen
            // echo "<tr>"; // Zeile erzeugen
            // for ($i = 0;$i < $felder;$i++) {
            //     $feldname = mysql_field_name($result, $i);
            //     echo "<th>$feldname</th>";
            // }
            // echo "</tr>\n"; // Zeile schließen

            // while ($row = mysql_fetch_assoc($result)) {
            //     echo "<tr>"; // Zeile erzeugen
            //     foreach ($row as $key => $value) {
            //         echo "<td>$value&nbsp;</td>";
            //     }
            //     echo "</tr>\n"; // Zeile schließen
            // }
            // echo "</table>\n"; // Tabelle schließen


        
}
        
$dbh null;
    } catch (
PDOException $e) {
        print 
"Error!: " $e->getMessage() . "<br/>";
        die();
    }
}
    
?>       
        </body>
    </html>

All i did was wrap the form handler in a conditional block that only executes when $_POST is populated. That way it doesn't try to use form data if it hasn't been supplied.
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Beginner: use WHERE in PHP-form with PHP_SELF

Postby mcgutschy » Sat Apr 12, 2014 2:43 pm

Ahhh, very easy, thanks a lot! :)

But I've still a problem in understanding the syntax. I've tried this in line 24:
Code: Select all
while($zeile = ($dbh->query($sql)) {

and get this error-message:
Parse error: syntax error, unexpected '{' in /var/www/tabelle/eingabe2/index4.php on line 24

How can I create a table? And how can I get the syntax highlightning on? :?
Code: Select all
<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="description" content="Adressen Datenbank">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Testside</title>
        <link rel="stylesheet" href="css/style.css">
        <link rel="author" href="humans.txt">
    </head>
    <body>
    <h1>Testside</h1>
    <form action="index3.php" method= "POST">
       <textarea cols="10" rows="1" name="suche" wrap=virtual></textarea>
       <input type="submit" value="Suchen">
    </form>
<?php if ($_POST){
    try {
        $dbh = new PDO('mysql:host=localhost;dbname=pizzadb2', 'pizzauser', 'sommer');
        $suche = ($_POST['suche']);
        $sql = "SELECT laden_name, vorwahl, telenr1, plz, ort,
        linkname1, forum_link FROM pizzeria_table WHERE plz = " .$dbh->quote($suche);
        echo '<table border = "1">';
        while($zeile = ($dbh->query($sql)) {
            echo"<tr>";
            echo"<td>" $zeile['laden_name']. "<td>";
            echo"<td>" $zeile['vorwahl']. "<td>";
            echo"<td>" $zeile['telenr1']. "<td>";
            echo"<td>" $zeile['plz']. "<td>";
            echo"<td>" $zeile['ort']. "<td>";
            echo"<td>" $zeile['linkname1']. "<td>";
            echo"<td>" $zeile['forum_link']. "<td>";
            echo"</tr>";

        }
        echo "</table>";
        $dbh = null;
    } catch (PDOException $e) {
        print "Error!: " . $e->getMessage() . "<br/>";
        die();
    }
}
?>       
    </body>
</html>
mcgutschy
New php-forum User
New php-forum User
 
Posts: 6
Joined: Wed Apr 09, 2014 12:21 pm

Re: Beginner: use WHERE in PHP-form with PHP_SELF

Postby seandisanti » Sat Apr 12, 2014 8:06 pm

The unexpected { is because you have 3 opening parentheses and 2 closing. Creating a table in sql? There's more to that than could be easily explained. google 'mysql create table' and you should get plenty of examples of creating tables with code. Syntax highlighting depends on your editor. unless you're talking about in code blocks on the forum. those you do by using code tags, with code=php
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Beginner: use WHERE in PHP-form with PHP_SELF

Postby mcgutschy » Sun Apr 13, 2014 3:25 am

Sorry sorry, you are right. To create a table is no problem, but what does this mean?
Code: Select all
Fatal error: Call to undefined method PDO::fetch() in /var/www/tabelle/eingabe2/index4.php on line 24

Line 24:
Code: Select all
while($row = $dbh->fetch()) 

I've read about it many times in googl and I've tried some variations, but I really not leaned how to fix it. Would it be a problem for you to fix it for me. And I swear that I've just started an new PHP book, cause it's look like I have to learn a lot again.
Code: Select all

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="description" content="Adressen Datenbank">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Testside</title>
        <link rel="stylesheet" href="css/style.css">
        <link rel="author" href="humans.txt">
    </head>
    <body>
    <h1>Testside</h1>
    <form action="index4.php" method= "POST">
        <textarea cols="10" rows="1" name="suche" wrap=virtual></textarea>
        <input type="submit" value="Suchen">
    </form>
<?php if ($_POST){
    try {
        $dbh = new PDO('mysql:host=localhost;dbname=pizzadb2', 'pizzauser', 'sommer');
        $suche = ($_POST['suche']);
        $sql = "SELECT laden_name, vorwahl, telenr1, plz, ort, 
        linkname1, forum_link FROM pizzeria_table WHERE plz = "
 .$dbh->quote($suche);
        echo '<table border = "1">';
        while($row = $dbh->fetch()) {
            echo"<tr>";
            echo"<td>". $row['laden_name']. "<td>";
            echo"<td>". $row['vorwahl']. "<td>";
            echo"<td>". $row['telenr1']. "<td>";
            echo"<td>". $row['plz']. "<td>";
            echo"<td>". $row['ort']. "<td>";
            echo"<td>". $row['linkname1']. "<td>";
            echo"<td>". $row['forum_link']. "<td>";
            echo"</tr>";

        }
        echo "</table>";
        $dbh = null;
            }catch (PDOException $e) {
            print "Error!: " . $e->getMessage() . "<br/>";
            die();
    }
}
?>        
    </body>
</html>


(And this time I've tried "[code=php]", in one line it works, in another not?)
mcgutschy
New php-forum User
New php-forum User
 
Posts: 6
Joined: Wed Apr 09, 2014 12:21 pm

Re: Beginner: use WHERE in PHP-form with PHP_SELF

Postby seandisanti » Sun Apr 13, 2014 11:20 pm

fetch is a function of PDOStatement, not PDO. You're very close though. right after you put the query into $sql, add the line
Code: Select all

$result 
$dbh->query($sqlPDO::FETCH_ASSOC);
 

then change your while loop to be
Code: Select all
while ($row $result->fetch())
 
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Beginner: use WHERE in PHP-form with PHP_SELF

Postby mcgutschy » Tue Apr 15, 2014 11:31 am

Ahhh, thanks a lot. I'm very sorry that I don't tested yet but I'm to busy. But you really safed my week!!!
Hope for my next problem I will met one who could helped me in the same way. :)
mcgutschy
New php-forum User
New php-forum User
 
Posts: 6
Joined: Wed Apr 09, 2014 12:21 pm

Re: Beginner: use WHERE in PHP-form with PHP_SELF

Postby seandisanti » Wed Apr 16, 2014 1:01 am

No problem, i'm always happy to help.
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm


Return to mySQL & php coding

Who is online

Users browsing this forum: No registered users and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron