Mysql将表单插入数据库表PDO

时间:2016-07-28 09:11:07

标签: php mysql pdo

我有一个问题..我今天尝试了很多工作,但这对我来说似乎是不可能的......所以我决定在这里寻求帮助。所以当按下提交按钮时,我有一个需要在数据库表上添加的表单。 这是我目前使用的PHP代码..我删除了我的尝试btw:

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

我的HTML代码(表单):

<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> 

 

感谢那些帮助我的人。

2 个答案:

答案 0 :(得分:0)

您需要在PHP代码中提交后检查POST变量。如果提交了表单,请获取post值并运行INSERT查询。

请参阅$_POST

答案 1 :(得分:0)

试试这个并在单选按钮中将值设为“1”和“2”而不是“option1”和“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'];
        }
    }