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 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序