2

I want to submit drop-down with dynamic value. I concatenate all variables inside the string but data not submit any one help me how is it done

if(isset($_POST['submit'])){
     $name = $_POST['name'];
     $value1 = $_POST['value1'];
     $value2 = $_POST['value2'];


        $data = "<select name=''$name''>";
        $data .="<option>Selet data</option>";
        $data .="<option value='**$value1**'> **$value1**</option>";
        $data .="<option value='**$value2**'> **$value2**</option>";
        $data .="</select>";

        mysql_query("insert into options (value) values ('".$data."')");
    }

With variable string not submit in to the database.

HTML is below

<form action="" method="POST">

        <input type="text" name="name">
        <input type="text" name="value1">
        <input type="text" name="value2">
        <input type="submit" name="submit">

</form>
shan
  • 21
  • 5
  • 1
    show us please the full code and your html part... btw dont use `mysql_` functions they are deprecated since php 5.5.0 – Blueblazer172 Dec 07 '16 at 12:21
  • You want to insert the whole shebang, including HTML tags? Or just the values? – Ionut Dec 07 '16 at 12:22
  • Do query the database for errors. $data WILL contain illegal characters in the context of your query. – Florian Heer Dec 07 '16 at 12:22
  • "How it is done" is to use prepared statements and query parameters. What you're building here is one great big SQL syntax error. `mysql_error()` is undoubtedly telling you about the problem, you should check that for errors. While not necessarily a duplicate, this would be a great place for you to start: http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – David Dec 07 '16 at 12:22
  • Check your table field strength – Shanu k k Dec 07 '16 at 12:23
  • Possible duplicate of [Inserting html code in a mysql table](http://stackoverflow.com/questions/2371093/inserting-html-code-in-a-mysql-table) – Nikhil Vaghela Dec 07 '16 at 12:23
  • Also you have an extra `}`. – Ionut Dec 07 '16 at 12:29
  • i want to insert HTML tags with php variable values – shan Dec 07 '16 at 12:29
  • I still don't understand what you wanna achieve or why you doing this – Masivuye Cokile Dec 07 '16 at 12:32
  • i remove the extra } – shan Dec 07 '16 at 12:32
  • i want to submit a string with php variable values ...but when i use php variable inside the string then data not submit – shan Dec 07 '16 at 12:34
  • what datatype are you using, you have to use htmlspecialchars – Naveen Kumar Dec 07 '16 at 12:35
  • no i can not using – shan Dec 07 '16 at 12:37
  • @shan -please echo the query and directly paste to database and let us know its working or not – Shanu k k Dec 07 '16 at 12:38
  • @shan can you please show us the html form too? – Shanu k k Dec 07 '16 at 12:42
  • i echo the query value show inside query but value not submit it give the syntax error like Error description: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '*`value**'> **`value****value** – shan Dec 07 '16 at 12:45
  • Buddy my table field strength is 1000 with varchar – shan Dec 07 '16 at 12:49

1 Answers1

0

Check with below:

    if(isset($_POST['submit'])){
     $name = $_POST['name'];
     $value1 = $_POST['value1'];
     $value2 = $_POST['value2'];

        $data = "<select name='$name'>";
        $data .="<option>Selet data</option>";
        $data .="<option value='**$value1**'> **$value1**</option>";
        $data .="<option value='**$value2**'> **$value2**</option>";
        $data .="</select>";

        if(!mysql_query("insert into options (`value`) values (".json_encode($data).")")) {
          echo("Error description: " . mysql_error());
        }
}
Rupal Javiya
  • 571
  • 4
  • 14