HI
I'm working with a php script and connecting via PDO to a database
My php file retrieves all data from datebase when I selected one radio button for example 2GB usb driver.

This is my html code

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>form php pdo</title>

</head>
<body>

<h3>Choose capacity for USB flash driver:</h3>
<form action="1php.php" method="post">
2GB
<input type="radio" name="programming[]" value="2GB"><br>
4GB
<input type="radio" name="programming[]" value="4GB"><br>
8GB
<input  type="radio" name="programming[]" value="8GB"><br>
16GB
<input  type="radio" name="programming[]" value="16GB"><br>
32GB
<input  type="radio" name="programming[]" value="32GB"><br>

<input type="submit" name="submit" value="Submit"><br>

</form>


</body>
</html>

And this is my php file

<?php
/*if(isset($_POST['programming'])){
echo "<h4>You Selected:</h4>";
echo "<ul>";
foreach ($_POST['programming'] as $value) {
echo "<li>$value</li>", " usb flash driver. ";

}
echo "</ul>";
}
echo "<br />";*/
echo "<html><body>";
$dsn = 'mysql:host=localhost;dbname=usb;charset=UTF8';
$dbuser = 'root';
$dbpass = '';
$DBH = null;
$usb_flash_driver= "usb_flash_driver";



try{
        # a DB Handler to manage the database connection
        $DBH = new PDO($dsn, $dbuser, $dbpass);
        # SQL query
        $sql = 'SELECT * FROM ' . $usb_flash_driver;
        # STH means "Statement Handle"
        $STH = $DBH->query($sql);
        # setting the fetch mode (array indexed by column name)
        $STH->setFetchMode(PDO::FETCH_ASSOC);
        //echo $STH->columnCount(), $STH->rowCount(), "<br />";
        while($row = $STH->fetch()) {
                echo "You are selected :<br />";
                echo "{$row['name']} {$row['capacity(GB)']} {$row['image']} {$row['description']} {$row['price']}<br />";
}
# close the connection
$DBH = null;
}
catch (PDOException $e){
echo '<b>PDOException: </b>', $e->getMessage();
die();
}

echo "</body></html>";
?>

I’m new to pdo I will be thankful for your help

What exactly is the problem?

Thanks pritaeas

The problem is when the user choose for example 2GB usb
http://puu.sh/5zF1i.png
it must shows(print out)only like this right.

You are selected :
lego 32 lego.jpg Lego brick building block USB 2.0 memory stick U disk 32GB flash drive thumb drives

But it shows(print out) hole usbGB in datebase not only one row (usbGB)that was chosen by user.
http://puu.sh/5zEZF.png

Ah. You need a WHERE clause in your query, so you limit your results. Something like this (may need some tweaking):

$value = $_POST['programming'];
$sql = "SELECT * FROM usb_flash_driver WHERE capacity(GB) = '$value'"

Thanks Sir
why I got error?
Parse error: syntax error, unexpected '$STH' (T_VARIABLE) in C:\xampp\htdocs\test\1Php.php on line 29

<?php
/*if(isset($_POST['programming'])){
echo "<h4>You Selected:</h4>";
echo "<ul>";
foreach ($_POST['programming'] as $value) {
echo "<li>$value</li>", " usb flash driver. ";

}
echo "</ul>";
}
echo "<br />";*/
echo "<html><body>";
$dsn = 'mysql:host=localhost;dbname=usb;charset=UTF8';
$dbuser = 'root';
$dbpass = '';
$DBH = null;
$usb_flash_driver= "usb_flash_driver";
$value = $_POST['programming'];



try{
        # a DB Handler to manage the database connection
        $DBH = new PDO($dsn, $dbuser, $dbpass, $value);
        # SQL query
        //$sql = 'SELECT * FROM ' . $usb_flash_driver;
        $sql = "SELECT * FROM usb_flash_driver WHERE capacity(GB) = '$value'"
        # STH means "Statement Handle"
        $STH = $DBH->query($sql);
        # setting the fetch mode (array indexed by column name)
        $STH->setFetchMode(PDO::FETCH_ASSOC);
        //echo $STH->columnCount(), $STH->rowCount(), "<br />";
        while($row = $STH->fetch()) {
                echo "You are selected :<br />";
                echo "{$row['name']} {$row['capacity(GB)']} {$row['image']} {$row['description']} {$row['price']}<br />";
}
/*
foreach ($STH as $row){
    echo "{$row['name']} {$row['capacity(GB)']} {$row['image']} {$row['description']} {$row['price']}<br />";
}*/

# close the connection
$DBH = null;
}
catch (PDOException $e){
echo '<b>PDOException: </b>', $e->getMessage();
die();
}

echo "</body></html>";
?>

Missing semi-colon.

Thanks
But I don't understand why I got error again?

Notice: Array to string conversion in C:\xampp\htdocs\test\1Php.php on line 27

Fatal error: Call to a member function setFetchMode() on a non-object in C:\xampp\htdocs\test\1Php.php on line 31

Notice: Array to string conversion in C:\xampp\htdocs\test\1Php.php on line 27

Oh, this may have to do with the fact you're using programming[], didn't notice this earlier. I think you can safely remove the square brackets.

Fatal error: Call to a member function setFetchMode() on a non-object in C:\xampp\htdocs\test\1Php.php on line 31

Means your query failed. Doesn't your error handling show you what's wrong? Why are you passing $value to the PDO constructor?

I removed but I got those error
Notice: Use of undefined constant programming - assumed 'programming' in C:\xampp\htdocs\test\1Php.php on line 18

Notice: Undefined index: programming in C:\xampp\htdocs\test\1Php.php on line 18

Fatal error: Call to a member function setFetchMode() on a non-object in C:\xampp\htdocs\test\1Php.php on line 31

Notice: Use of undefined constant programming

I think you removed the wrong thing. I meant in your form.

Did you remove $value from the constructor?

I got this error sir
Notice: Undefined variable: capacity in C:\xampp\htdocs\test\1Php.php on line 29

Fatal error: Call to a member function setFetchMode() on a non-object in C:\xampp\htdocs\test\1Php.php on line 33

<?php
/*if(isset($_POST['programming'])){
echo "<h4>You Selected:</h4>";
echo "<ul>";
foreach ($_POST['programming'] as $value) {
echo "<li>$value</li>", " usb flash driver. ";

}
echo "</ul>";
}
echo "<br />";*/
echo "<html><body>";
$dsn = 'mysql:host=localhost;dbname=usb;charset=UTF8';
$dbuser = 'root';
$dbpass = '';
$DBH = null;
$usb_flash_driver= "usb_flash_driver";
//$capacity(GB)=$_POST["programming"];
if(isset($_POST['programming'])){



try{
        # a DB Handler to manage the database connection
        $DBH = new PDO($dsn, $dbuser, $dbpass);
        # SQL query
        //$sql = 'SELECT * FROM ' . $usb_flash_driver;

        $sql = "SELECT * FROM usb_flash_driver WHERE capacity(GB) = '$capacity(GB)'";
        # STH means "Statement Handle"
        $STH = $DBH->query($sql);
        # setting the fetch mode (array indexed by column name)
        $STH->setFetchMode(PDO::FETCH_ASSOC);
        //echo $STH->columnCount(), $STH->rowCount(), "<br />";
        while($row = $STH->fetch()) {
                echo "You are selected :<br />";
                echo "{$row['name']} {$row['capacity(GB)']} {$row['image']} {$row['description']} {$row['price']}<br />";
}
/*
foreach ($STH as $row){
    echo "{$row['name']} {$row['capacity(GB)']} {$row['image']} {$row['description']} {$row['price']}<br />";
}*/

# close the connection
$DBH = null;
}
catch (PDOException $e){
echo '<b>PDOException: </b>', $e->getMessage();
die();
}
}
echo "</body></html>";
?>

Says that the $capacity variable is undefined. Note that you cannot use parenthesis in PHP variable names.

I changed the in the name in my datebase and in code line 29

$sql = "SELECT * FROM usb_flash_driver WHERE capacity = '$capacity'";

still get
Notice: Undefined variable: capacity in C:\xampp\htdocs\test\1Php.php on line 29

Did you set a value?

$capacity = 'some value here';

Hi sir
Lets try another time
When the user choose 2GB it must print out one row
http://puu.sh/5AWC3.png

it must just print the singel row like this
You are selected :
lego 32 lego.jpg Lego brick building block USB 2.0 memory stick U disk 32GB flash drive thumb drives

but my script print all rows
http://puu.sh/5AWDD.png
and this is my datebase
http://puu.sh/5AWLF.png

I appreciate your help

Here my code below

<?php
echo "<html><body>";
$dsn = 'mysql:host=localhost;dbname=usb;charset=UTF8';
$dbuser = 'root';
$dbpass = '';
$DBH = null;
$usb_flash_driver= "usb_flash_driver";

if(isset($_POST['programming'])){



try{
        # a DB Handler to manage the database connection
        $DBH = new PDO($dsn, $dbuser, $dbpass);
        # SQL query
        $sql = 'SELECT * FROM ' . $usb_flash_driver;
        //$sql = "select id, name, capacity, image, description";

        # STH means "Statement Handle"
        $STH = $DBH->query($sql);
        # setting the fetch mode (array indexed by column name)
        $STH->setFetchMode(PDO::FETCH_ASSOC);

        while($row = $STH->fetch()) {
                echo "You are selected :<br />";
                echo "{$row['name']} {$row['capacity']} {$row['image']} {$row['description']} {$row['price']}<br />";
}
/*
foreach ($STH as $row){
    echo "{$row['name']} {$row['capacity(GB)']} {$row['image']} {$row['description']} {$row['price']}<br />";
}*/

# close the connection
$DBH = null;
}
catch (PDOException $e){
echo '<b>PDOException: </b>', $e->getMessage();
die();
}
}
echo "</body></html>";
?>


//HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>form php pdo</title>

</head>
<body>

<h3>Choose capacity for USB flash driver:</h3>
<form action="42aphp.php" method="post">
2GB
<input type="radio" name="programming[]" value="2GB"><br>
4GB
<input type="radio" name="programming[]" value="4GB"><br>
8GB
<input  type="radio" name="programming[]" value="8GB"><br>
16GB
<input  type="radio" name="programming[]" value="16GB"><br>
32GB
<input  type="radio" name="programming[]" value="32GB"><br>

<input type="submit" name="submit" value="Submit"><br>

</form>


</body>
</html>

Like I said before, remove the brackets:

<form action="42aphp.php" method="post">
    2GB
    <input type="radio" name="programming" value="2GB"><br>
    4GB
    <input type="radio" name="programming" value="4GB"><br>
    8GB
    <input  type="radio" name="programming" value="8GB"><br>
    16GB
    <input  type="radio" name="programming" value="16GB"><br>
    32GB
    <input  type="radio" name="programming" value="32GB"><br>
    <input type="submit" name="submit" value="Submit"><br>
</form>

Then do:

$value = $_POST['programming'];
$sql = "SELECT * FROM $usb_flash_driver WHERE `capacity(GB)` = '$value'";

Thanks so much pritaeas
Its work very well

Thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.