douzhi3779 2016-07-28 09:11
浏览 48
已采纳

Mysql将表单插入数据库表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.

</div>
  • 写回答

2条回答 默认 最新

  • dpca31461 2016-07-28 10:59
    关注

    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'];
            }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵