M.Doe M.Doe - 4 months ago 33
MySQL Question

Mysql Insert form into database table PDO

I have a problem.. I tried today a lot to make this working but it seems to be impossible for me.. so I decided to ask help here. So I have a form which needs to be added on database table when submit button is pressed.
This is php code I currently use.. I removed my tries btw:



<?php
$pdo = new PDO('mysql:host=;dbname=', '', '');
$sql = "SELECT * FROM games LIMIT 10";
foreach ($pdo->query($sql) as $row) {
?>





my HTML code (form):



<form class="form-group" method="POST" action="">
<div role="tabpanel" class="tab-pane active" id="home">
<div class="form-group" style="margin-top: 15px;">
<label for="exampleInputEmail1">Game Title</label>
<input type="text" class="form-control" id="exampleInputEmail1" placeholder="" name="gtitle" />
</div>
<div class="form-group">
<label for="exampleInputPassword1">YouTube Link</label>
<input type="text" class="form-control" id="exampleInputPassword1" placeholder="" name="ytlink" />
</div>
<div class="form-group">
<label for="exampleInputPassword1">Link Source</label>
<input type="text" class="form-control" id="exampleInputPassword1" placeholder="ex: GLEAM, DLH, FAILMID, HRKGAME, INDIEGALA, OTHER, STEAM" name="slink" />
</div>
<div class="form-group">
<label for="exampleInputPassword1">Link to Free Steam Keys</label>
<input type="text" class="form-control" id="exampleInputPassword1" placeholder="KEY MUST GIVE +1 TO THE STEAM LIBRARY GAME COUNT" name="keysl" />
</div>
<label for="exampleInputPassword1">Steam App ID</label>
<div class="input-group" style="padding-bottom: 10px;">
<span class="input-group-addon" id="basic-addon3">http://store.steampowered.com/app/</span>
<input type="text" class="form-control" id="basic-url" aria-describedby="basic-addon3" placeholder="App ID" name="appid" />
</div>
<div class="form-group">
<label for="exampleInputPassword1">Categories</label>
<div class="checkbox">

<label class="radio-inline">
<input type="radio" name="inlineRadioOptions" id="inlineRadio1" value="option1" /> 1 <h4><span class="label label-success">Keys Available</span></h4>
</label>
<label class="radio-inline">
<input type="radio" name="inlineRadioOptions" id="inlineRadio2" value="option2" /> 2 <h4><span class="label label-danger">No Keys left</span></h4>
</label>


</div>
<button type="submit" class="btn btn-default" name="insert">Submit</button>
</div>
</div>

</form>





Thank those who help me.

nkh nkh
Answer

Try this and in the radio button put value as '1' and '2' instead of 'option1' and 'option2'

        if(isset($_POST['insert']))
    {
        $game_title=$_POST['gtitle']; 
        $youtube_link=$_POST['ytlink'];
        $link_source=$_POST['slink'];
        $link_steam_keys=$_POST['keysl'];
        $app_id=$_POST['appid'];
        $categories=$_POST['inlineRadioOptions'];

        $query_ins="INSERT INTO tbl_game(game_title,youtube_link,link_source,link_steam_keys,app_id,categories) VALUES(:game_title,:youtube_link,:link_source,:link_steam_keys,:app_id,:categories)";
        $stmt_query=$dbh->prepare($query_ins);
        $games_ins=$stmt_query->execute(array(":game_title"=>$game_title,":youtube_link"=>$youtube_link,":link_source"=>$link_source,":link_steam_keys"=>$link_steam_keys,":app_id"=>$app_id,":categories"=>$categories));
        if(!$games_ins)
        {
            $error=$stmt_query->errorInfo();
            echo $error['2'];
        }
    }