Heyo,
这是名为api
+----+----------+--------+-------+
| id | name | symbol | price |
+----+----------+--------+-------+
| 1 | bitcoin | btc | 10 |
| 2 | ethereum | eth | 5 |
+----+----------+--------+-------+
这些是表testing
+----+--------+-------+-------+
| id | symbol | coins | price |
+----+--------+-------+-------+
我要完成的是symbol
和price
,当我填写此内容时,api
中的值将插入到各自行的testing
表中形式:
<form method="post">
<div class="form-group row justify-content-center">
<label class="col-sm-3 col-form-label">Choose coin</label>
<div class="col-sm-6">
<select class="form-control" name="coin">
<?php
$stmt = $pdo->query('SELECT id, name, symbol FROM api');
while ($row = $stmt->fetch()) {
echo '<option id="'.$row['id'].'" value="'.$row['symbol'].'">'.$row['name'].'</option>';
}
?>
</select>
</div>
</div>
<div class="form-group row justify-content-center">
<label class="col-sm-3 col-form-label">How many coins do you have?</label>
<div class="col-sm-6">
<input type="number" class="form-control" name="coins" placeholder="1">
</div>
</div>
<div class="form-group row justify-content-center text-center">
<div class="col-sm-12">
<button type="submit" class="btn btn-primary">Add</button>
</div>
</div>
</form>
但我的主要问题是我还想插入一些不在api
表格内的东西。我有<input type="number" class="form-control" name="coins" placeholder="1">
我可以在其中键入一个数字,该数字将插入coins
行。
我认为选择
<select class="form-control" name="coin">
<?php
$stmt = $pdo->query('SELECT id, name, symbol FROM api');
while ($row = $stmt->fetch()) {
echo '<option id="'.$row['id'].'" value="'.$row['symbol'].'">'.$row['name'].'</option>';
}
?>
</select>
这里有一个因素,通过使用我想从选定的echo '<option id="'.$row['id'].'" value="'.$row['symbol'].'">'.$row['name'].'</option>';
获取数据的选择字段,但我不知道如何继续这个。
我认为我已经使用此代码做了一些事情:
<?php
$host = '127.0.0.1';
$db = 'db_learning';
$user = 'root';
$pass = '';
$charset = 'utf8mb4';
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
$pdo = new PDO($dsn, $user, $pass, $opt);
if(!empty($_POST)) {
if(empty($_POST['coins'])) {
#die
die("Please enter how many coins you have.");
}
$stmt = $pdo->prepare("INSERT INTO testing (coin, coins) VALUES (:coin, :coins)");
$stmt->bindParam(':coin', $coin);
$stmt->bindParam(':coins', $coins);
# insert the row
$coin = $_POST['coin'];
$coins = $_POST['coins'];
$stmt->execute();
}
?>
但在提交上述表格时,它只给出了coin
和coins
中的价值观,由于我已经告诉它,这不会令人震惊。
有人可以在这里指导我吗?
谢谢你,以及新生儿php新手的节日快乐。
答案 0 :(得分:1)
根据表testing
提供的原理图,它没有列coin
,因此该语句可能无法准备。
至少你应该在继续之前测试语句对象是否被创建 - 像这样:
$stmt = $pdo->prepare("INSERT INTO testing (coin, coins) VALUES (:coin, :coins)");
if( $stmt ){
$stmt->bindParam(':coin', $coin);
$stmt->bindParam(':coins', $coins);
}
但是,接收该值的列看起来更有可能是symbol
$stmt = $pdo->prepare("INSERT INTO testing (symbol, coins) VALUES (:symbol, :coins)");
if( $stmt ){
$stmt->bindParam(':symbol', $coin);
$stmt->bindParam(':coins', $coins);
}
或插入价格〜使用try/catch
检测并响应失败的sql prepare call ...
try{
$stmt = $pdo->prepare("insert into testing (`symbol`, `coins`, `price` ) values ( :symbol, :coins, ( select `price` from `api` where `symbol`=:apisymbol ) )");
if( $stmt ){
$stmt->bindParam(':symbol', $coin );
$stmt->bindParam(':coins', $coins );
$stmt->bindParam(':apisymbol',$coin );
} else {
throw new Exception( "Statement failed" );
}
}catch( Exception $e ){
echo $e->getMessage();
}