Use a $variable inside a SQL string?

前端 未结 3 1379
傲寒
傲寒 2020-12-22 15:02

I would like to be able to select a category from a dropdown and depending on the category it will add it to whatever SQL table is it equal with.



        
相关标签:
3条回答
  • 2020-12-22 15:24

    You cannot use that type of variables, change last code to $sql="INSERT INTO $article (headline, content) VALUES ('" . $_POST['headline'] " . ', '" . $_POST['content'] . "')";

    0 讨论(0)
  • 2020-12-22 15:37

    I know this answer won't be too helpful for you right now, but sice there is just too much wrong with that code and that approach, here are a few tips:

    • Use PDO instead of PHP's MySQL functions. It'll seem daunting at first, especially if you haven't got any experience with object-oriented programming, but it's definately worth the effort.
    • Sanitize that $article value! if($article == 'foo' || $article == 'bar') {...}
    • The best ways to use variables in strings are: "This is a ".$adjective." string" and "This is a {$adjective} string"
    0 讨论(0)
  • 2020-12-22 15:47

    Try:

    "INSERT INTO `{$article}` ...."
    

    Don't forget to sanitize your input! (mysql_real_escape_string, for starters)

    0 讨论(0)
提交回复
热议问题