4 Replies - 264 Views - Last Post: 14 November 2017 - 10:33 AM Rate Topic: -----

#1 testmetestyou  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 19-July 16

Select optiongroup php

Posted 14 November 2017 - 05:12 AM

If I post value "133" to database how I can get later this value from database?

I have like profile and there showing "Social" or "IT" it depends which value you selected.

So I selected "133" it must show now me "Social Facebook" not "Social 133".

If I select "153" then i get "IT Computer Science"

Its going to user profile:


My code:

<select name="slectopt">
      <option value="-">-- Select --</option>
      <optgroup label="Social">
        <option value="133">Facebook</option>
        <option value="161">Twitter</option>
        <option value="183">Tumblr</option>
        <option value="178">Google+</option>
      </optgroup>
      <optgroup label="IT">
        <option value="144">Information Technology</option>
        <option value="134">Information System</option>
        <option value="153">Computer Science</option>
      </optgroup>
    </select>


I've tryed this:

<?php
    $dbb = mysqli_connect("localhost", "root", "root", "test")
      or die('Error communicating to MySQL server.');
    if(isset($_POST['submit'])){
        $department=$_POST['selectopt'];
        $querye = "INSERT INTO profile (profile) VALUES ('$department')";
        if(mysqli_query($dbb,$querye)){
        echo "Records inserted successfully.";
    } else{
        echo "ERROR: Could not able to execute $querye. " . mysqli_error($dbb);
    }
     
    // Close connection
    mysqli_close($dbb);             
    }
    ?>


Here is code where im tryng to get posted value, but I get number what it posted But I need Profile name..

<?php
    
    $sql = "SELECT * FROM profile";
    if($result = mysqli_query($dbb, $sql)){
        if(mysqli_num_rows($result) > 0){
            echo "<table>";
                echo "<tr>";
                    echo "<th>id</th>";
                    echo "<th>Profile</th>";
                echo "</tr>";
            while($row = mysqli_fetch_array($result)){
                echo "<tr>";
                    echo "<td>" . $row['id'] . "</td>";
                    echo "<td>" . $row['profile'] . "</td>";
                echo "</tr>";
            }
            echo "</table>";
            mysqli_free_result($result);
        } else{
            echo "No records matching your query were found.";
        }
    } else{
        echo "ERROR: Could not able to execute $sql. " . mysqli_error($dbb);
    }
     
    // Close connection
    mysqli_close($dbb);
    ?>


Is This A Good Question/Topic? 0
  • +

Replies To: Select optiongroup php

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3722
  • View blog
  • Posts: 13,513
  • Joined: 08-August 08

Re: Select optiongroup php

Posted 14 November 2017 - 06:18 AM

First thing first: Use prepared statements.
Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3722
  • View blog
  • Posts: 13,513
  • Joined: 08-August 08

Re: Select optiongroup php

Posted 14 November 2017 - 06:18 AM

Deprecated MySQL functions are simpler to use (not counting all the sanitizing you need to add) than PDO or MySQLi prepared statements, but far less secure. That's why they've been deprecated. If you're just learning PHP you should start with
http://www.dreaminco...duction-to-pdo/
or
http://php.net/manua...-statements.php
When dealing with old code that uses deprecated MySQL functions you should replace them if possible. If it's not possible, your experience with PDO or MySQLi will benefit you when dealing with MySQL functions.
Was This Post Helpful? 0
  • +
  • -

#4 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1882
  • View blog
  • Posts: 5,839
  • Joined: 15-January 14

Re: Select optiongroup php

Posted 14 November 2017 - 09:50 AM

It's going to save whatever you submit in the form. You're not sending the name, you're sending the number. If you want the name then either send the name also, or better yet, store all of that data in the database also so that you can look up a name from a number.
Was This Post Helpful? 0
  • +
  • -

#5 benanamen  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 143
  • Joined: 28-March 15

Re: Select optiongroup php

Posted 14 November 2017 - 10:33 AM

Do not create variables for nothing and do not depend on the name of a button to be submitted in order for your script to work. You need to check the Request Method.

if ($_SERVER['REQUEST_METHOD'] == 'POST')


Do not output internal system errors to the user. That info is useless to the user and only of value to a hacker.

There is no need to manually close the DB connection. php will do it automatically.

Unless it is a typo in the post, slectopt != selectopt

Do not SELECT *. Specify the column names explicitly

Never ever put variables in a query.

You would do better to use PDO with prepared statements. Here is a tutorial to get you going. PDO Tutorial

As mentioned, you need to store the paired data in your DB to pull out a name from a number.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1