以下是代码:
HTML:
<div class="modal-body">
<input type="text" name="tradelink" class="tradeurlinput" id="tradeurlinput">
</div>
<div class="modal-footer">
<button class="savechanges" id="savechanges" type="submit" name="SubmitTlink" value="update" >Save Changes</button>
</div>
PHP:
<?php
$con = mysqli_connect("localhost","root","") or die("Connection Failed");
$db_found = mysqli_select_db($con, 'tutorial') or die("connection failed");
if (isset($_POST['SubmitTlink'])) {
$tradelink = $_POST['tradelink'];
$result = mysqli_query($con, "UPDATE tlink SET tlink= '$tradelink' WHERE steamid = '".$steamprofile['steamid']."'");
if (mysqli_query($result)) {
echo "updated";
mysqli_close($con);
} else {
echo "fail";}
mysqli_close($con);
}
}
?>
我已经尝试自己解决这个问题,但我无法让它工作,建议?
答案 0 :(得分:0)
问题在于:
// Some more code.
if(mysqli_query($result)) { // You should check the truthiness of this.
echo "updated";
mysqli_close($con); // You don't need this here.
}
else{
echo "fail";
}
mysqli_close($con); // This is optional, but fine.
}
$result
是一个更新查询,只返回true
或false
。所以做mysqli_query(true)
是没有意义的,而且它会抛出一个错误,说它期望MySQLi连接对象作为第一个参数而不是布尔值。
所以改变你的代码,你应该......
// Some more code.
if($result) {
echo "updated";
}
else {
echo "fail";
}
mysqli_close($con); // This is optional, but fine.
}
答案 1 :(得分:0)
No need of use mysqli_query twice.Try this code
<?php
$con=mysqli_connect("localhost","root","") or die("Connection Failed");
$db_found = mysqli_select_db($con, 'tutorial') or die("connection failed");
if(isset($_POST['SubmitTlink'])){
$tradelink = $_POST['tradelink'];
$result = mysqli_query($con, "UPDATE tlink SET tlink= '$tradelink' WHERE steamid = '".$steamprofile['steamid']."'");
if(($result)){
echo "updated";
mysqli_close($con);
}
else{
echo "fail";}
mysqli_close($con);
}
?>
答案 2 :(得分:0)
我注意到的一件事是你没有一个带有方法和动作的表单,你的代码应该是这样的
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div class="modal-body">
<input type="text" name="tradelink" class="tradeurlinput" id="tradeurlinput">
</div>
<div class="modal-footer">
<button class="savechanges" id="savechanges" type="submit" name="SubmitTlink" value="update" >Save Changes</button>
</div>
</form>
然后当点击按钮时你需要检查字段tradelink是否设置而不是空,然后你可以在真实时启动更新,并尝试一直使用mysqli预处理语句,你需要google那个为什么使用准备好的陈述很重要。
注意:始终验证,过滤和清理用户输入,处理每个输入 来自用户,好像来自一个非常危险的黑客。
你的php脚本应该是这样的。
<?php
$con = mysqli_connect("localhost", "root", "") or die("Connection Failed");
$db_found = mysqli_select_db($con, 'tutorial') or die("connection failed");
if (isset($_POST['SubmitTlink'])) {
if (empty($_POST['tradelink'])) {
die("enter tradelink");
} else {
$tradelink = $_POST['tradelink'];
$sql = $con->prepare("UPDATE tlink SET tlink =? where steamid = ?");
$sql->bind_param('si', $tradelink, $steamprofile);
if ($sql->execute()) {
echo "update";
} else {
echo "fail" . mysqli_error();
}
$sql->close();
$con->close();
}
}
?>
希望这有助于解决问题,
PS:您可以在一行中连接并选择数据库。
$con = new mysqli("host", "user, "password, DB);
我愿意接受建议,我想念一些事,谢谢。