我一直坚持这个问题几个小时,我研究的对我也没有帮助。我只是在输入与数据库中已有的名称匹配时才尝试更新我的数据库。如何修复此代码并感谢您的帮助。
//Input
<form>
<input type="text" class="form-control" aria-describedby="emailHelp" name="name_input" placeholder="Enter Name">
</div>
<a href="" name="name_btn" class="btn btn-primary">Enter</a>
</div>
</form>
//The PHP code
$name = "";
$name_input = "";
$points = "";
if(isset($_POST['name_btn'])){
$name_input = strip_tags($_POST['name_input']);
$name = $user['name'];
$points = $user['points'];
if($name_input == $name) {
$points = points + 3;
$upate_data = mysqli_query($con, "UPDATE users SET points='$points' WHERE id='4'");
}
}
答案 0 :(得分:0)
您可以使用select查询并获取返回行的计数来测试结果是否存在。例如;
$query = "SELECT * FROM $table WHERE username = '$name_input'";
$result = mysqli_query($conn, $query);
$count = mysqli_num_rows($result);
if($count > 0){
//the result exists
$points = points + 3;
$upate_data = mysqli_query($con, "UPDATE users SET points='$points' WHERE id='4'");
}else{
//result does not exist
}
此外,我看到您在此处获得name
变量的$name_input = strip_tags($_POST['name']);
变量name_input
的数据,但输入字段的名称属性为<input type="text" class="form-control" aria-describedby="emailHelp" name="name_input" placeholder="Enter Name">
的格式$('#members-search .typeahead').on("input", function(){
$('.tt-suggestion').on("click", changeFunction);
});
您应该检查出来。
答案 1 :(得分:0)
你应该在你的表单中添加一个方法,同时href不适用于post,应该是这样的:
//Input
<form method="post">
<input type="text" class="form-control" aria-describedby="emailHelp" name="name" placeholder="Enter Name">
</div>
<!-- <a href="" name="name_btn" class="btn btn-primary">Enter</a> -->
<input type="submit" value="Submit" name="name_btn">
</div>
</form>
//The PHP code
<?php
$databaseHost = 'localhost';
$databaseName = 'testing';
$databaseUsername = 'root';
$databasePassword = '';
$con = mysqli_connect($databaseHost, $databaseUsername, $databasePassword, $databaseName);
$name = "";
$name_input = "";
$points = "";
if(isset($_POST['name_btn'])){
echo "aaa";
$name_input = strip_tags($_POST['name']);
$name = $user['name'];
$points = $user['points'];
echo "   Data updated successfully!";
if($name_input == $name) {
$points = points + 3;
$upate_data = mysqli_query($con, "UPDATE users SET points='$points' WHERE id='4'");
}
}
?>