Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
另一双眼睛可以检查我的php代码,看看为什么当有人向表单提交某个内容时,它会显示两次?_Php_Database_Forms_Mysqli - Fatal编程技术网

另一双眼睛可以检查我的php代码,看看为什么当有人向表单提交某个内容时,它会显示两次?

另一双眼睛可以检查我的php代码,看看为什么当有人向表单提交某个内容时,它会显示两次?,php,database,forms,mysqli,Php,Database,Forms,Mysqli,我正在学习如何制作一个表单来显示提交的内容。当我将信息提交到表单中时,信息会显示两次,我不知道为什么。我的代码可能会有很多错误,因为我仍然对此一无所知。我正在仔细检查所有东西,看看为什么它会显示两次,但我似乎找不到问题 <?php $mysqli = new mysqli("localhost","root", "", "hits"); if(!$mysqli){ die('Could not connect: ' . mysqli_connect_error()); } $d

我正在学习如何制作一个表单来显示提交的内容。当我将信息提交到表单中时,信息会显示两次,我不知道为什么。我的代码可能会有很多错误,因为我仍然对此一无所知。我正在仔细检查所有东西,看看为什么它会显示两次,但我似乎找不到问题

<?php
$mysqli = new mysqli("localhost","root", "", "hits");

if(!$mysqli){
    die('Could not connect: ' . mysqli_connect_error());
}

$db_selected = mysqli_select_db($mysqli, "hits"); 

if(!$db_selected){
    die('can not use' . "hits" . ': ' . mysqli_connect_error());
}

$hit = $_POST['hit'];
$amount = $_POST['amount'];
$category = $_POST['category'];

$result = mysqli_query($mysqli, "INSERT into hit (hit, amount, category) VALUES ('$hit', '$amount', '$category')");

if(!mysqli_query($mysqli, "INSERT into hit (hit, amount, category) VALUES ('$hit', '$amount', '$category')")){
    die('Error: ' . mysql_Error());
}

$data = $mysqli->query("SELECT * FROM hit");

while($row = $data->fetch_assoc()) {

 Print "<tr>"; 
 Print "<th>Hit:</th> <td>".$row['hit'] . "</td> "; 
 Print "<th>Amount:</th> <td>".$row['amount'] . " </td>"; 
 Print "<th>Category:</th> <td>".$row['category'] . "</td></tr> ";
 Print "<br><br/>"; 
Print "</table>";
 // array_sum
 } 

?>

您可以运行两次查询

$result = mysqli_query($mysqli, "INSERT into hit (hit, amount, category) VALUES ('$hit', '$amount', '$category')");

if(!mysqli_query($mysqli, "INSERT into hit (hit, amount, category) VALUES ('$hit', '$amount', '$category')")){
    die('Error: ' . mysql_Error());
}
我想你的意思是:

if( ! $result ){
    die('Error: ' . mysql_Error());
}

我将改为使用。

尝试删除此行:

while($row = $data->fetch_assoc()) {

 Print "<tr>"; 
 Print "<th>Hit:</th> <td>".$row['hit'] . "</td> "; 
 Print "<th>Amount:</th> <td>".$row['amount'] . " </td>"; 
 Print "<th>Category:</th> <td>".$row['category'] . "</td></tr> ";
 Print "<br><br/>"; 
Print "</table>"; // Remove This
 // array_sum
 } 
while($row=$data->fetch_assoc()){
打印“”;
打印“命中:.$row['Hit']”;
打印“金额:.”行['Amount']。”;
打印“类别:.”行['Category']。”;
打印“

”; 打印“”;//删除此文件 //数组和 }

把它放在while语句之外

要求一双眼睛绝对是一个“过于本地化的问题”。无论如何,程序员应该运行代码,而不是观察它。您正在执行两次
INSERT
查询。。。而且您的代码也非常容易受到攻击,在未检查/消毒/清理的情况下,永远不要将从远程接收的值插入数据库!您插入了两次,因此twiceit没有运行两次!hi只是定义了变量,没有执行it@Memolitionfrom:mysqli_query-对数据库执行查询-强调我的。因此,如果我说
$variable='Something'
然后
echo$变量它回显两次?我知道mysqli_query的功能和工作原理,但hi没有调用
变量
$result
!谢谢Xeoncross说的话解决了问题。。也。。对不起,这个问题的标题是什么?哦,天哪,你们这些高级编码人员吓坏了我;)编辑:关于pdo。。我被告知要换成那样。我还没有。我的密码现在到处都是。。如你所知,谢谢你的回复。事实上,我一开始把它放在支架外面,然后把它放进去,因为我一开始认为这就是问题所在。不是,但我会把它放在外面。