gpt4 book ai didi

php - 插入错误(PHP)

转载 作者:行者123 更新时间:2023-11-29 19:44:17 24 4
gpt4 key购买 nike

我一直在寻找解决这个问题的方法,但没有成功。基本上,当在表中插入项目时,它不会返回结果(不插入任何内容)。我已经检查了表字段并且全部匹配,我似乎找不到我正在做的错误。

这是我当前遇到问题的代码部分。

            <br>
<h3><center><b>Components management</b></center></h3>
<br>
<form name = "registo" method = "POST" action = "">
<center><fieldset>
<legend>Insert component</legend>
<br>
<p><b> Nome: <input type = "text" name = "component_name" required></b> </p><br>

<p> <b>Type:</b> </p>
<div class = "mytable">
<table><tr>
<?php
$queryComp_Type = "select id, name from conc_type order by name";
$resultadoQueryComp_Type = mysqli_query($conn, $queryComp_Type);
foreach ($resultadoQueryComp_Type as $rowComp_Type)
{
?>
<th><?php echo $rowComp_Type['name']; ?></th>
<?php
}
?>
</tr><tr>
<?php
foreach ($resultadoQueryComp_Type as $rowComp_Type)
{
?>
<td><input class= "radio" type= "radio" name= "component_type" value= "<?php echo $rowComp_Type['id']; ?>" required> </td>
<?php
}
?>
</tr></table>
</div><br>
<p> <b>State:</b></p>
<table class="mytable">
<tr><th>Active</th><th>Inactive</th></tr>
<tr>
<td> <input class= "radio" type= "radio" name= "component_state" value= "active" required></td>
<td> <input class= "radio" type= "radio" name= "component_state" value= "inactive"></td>
</tr>
</table>
<br>

<!--Insert button-->
<input type= "hidden" name= "state" value= "inserir">
<input class= "button" type= "submit" value= "Inserir Componente">
<br><br>

</fieldset></center>
</form>
<?php
}
else if($_REQUEST['state'] == "inserir")
{
?>
<h3>Components management - inserçtion</h3>
<?php
$component_name = $_REQUEST['component_name'];
$component_type = $_REQUEST['component_type'];
$component_state = $_REQUEST['component_state'];


$insert = sprintf("INSERT INTO `concept` (`name`,`conc_type_id`,`state`) VALUES ('%s', '$component_type', '%s')", mysqli_real_escape_string($conn, $component_name),
mysqli_real_escape_string($conn, $component_state));

$resultado_insert = mysqli_query($conn, $insert);

if($resultado_insert)
{
mysqli_query($conn,'COMMIT');
?>
<p>Insert successfuly</p>
<p>Click in <a href = "gestao-de-componentes">Continue</a> to advance.</p>
<?php
}
else
{
mysqli_query($conn,'ROLLBACK');
?>
<p>Insertion error.</p>
<?php
}
back();
}
}
?>

如果有人能提供帮助,我将不胜感激!

编辑:将 $$component_name 更改为 $$component_name。 (错字)

编辑:整页代码。

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css"href="/custom/css/ag.css">
</head>
<body>
<?php

require_once("custom/php/common.php");

if(!is_user_logged_in() && !current_user_can('manage_components'))
{
echo "You don't have permission to access this page";
}
else
{
if($_REQUEST['state'] == "")
{
$verify_component = "SELECT * FROM concept";
$result_component = mysqli_query($conn, $verify_component);
if(mysqli_num_rows($result_component) == 0)
{
echo "Não há componentes";
}
else
{
?>
<table class="mytable">
<thead><tr>
<th>Type</th>
<th>ID</th>
<th>Name</th>
<th>State</th>
<th>Ação</th>
</tr></thead>
<!--<tbody>-->
<?php

$choice_comp_type = "SELECT * FROM conc_type";
$resultado_comp_type = mysqli_query($conn, $choice_comp_type);
while($type = mysqli_fetch_array($resultado_comp_type))
{

$choice_components = "SELECT component.*
FROM concept,conc_type
WHERE conc_type.id = concept.conc_type_id
AND conc_type_id = '".$type['id']."'";
$result_components = mysqli_query($conn, $choice_components);
$num_rows = mysqli_num_rows($result_components);

if($num_rows > 0)
{
?>
<tr>
<td class= "Name" colspan = "1" rowspan = "<?php echo $num_rows; ?>"> <?php echo $type['name']; ?> </td>

<?php
while($row = mysqli_fetch_array($result_components))
{
?>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['name'];?></td>
<?php
if($row['state'] == "active")
{
?>
<td>active</td>
<td>[edit] [desactivate]</td>
<?php
}
else
{
?>
<td>inactive</td>
<td>[edit] [activate]</td>
<?php
}
?>
</tr>
<?php
}
}
}
?>
<!--</tbody>-->
</table>
<?php
}
?>
<br>
<h3><center><b>Management Compontent - introduction</b></center></h3>
<br>
<form name = "registo" method = "POST" action = "">
<center><fieldset>
<legend>Insert component</legend>
<br>
<p><b> Name: <input type = "text" name = "component_name" required></b> </p><br>

<p> <b>Type:</b> </p>
<div class = "mytable">
<table><tr>
<?php
$queryComp_Type = "select id, name from conc_type order by name";
$resultadoQueryComp_Type = mysqli_query($conn, $queryComp_Type);
foreach ($resultadoQueryComp_Type as $rowComp_Type)
{
?>
<th><?php echo $rowComp_Type['name']; ?></th>
<?php
}
?>
</tr><tr>
<?php
foreach ($resultadoQueryComp_Type as $rowComp_Type)
{
?>
<td><input class= "radio" type= "radio" name= "component_type" value= "<?php echo $rowComp_Type['id']; ?>" required> </td>
<?php
}
?>
</tr></table>
</div><br>
<p> <b>State:</b></p>
<table class="mytable">
<tr><th>active</th><th>Inactive</th></tr>
<tr>
<td> <input class= "radio" type= "radio" name= "component_state" value= "active" required></td>
<td> <input class= "radio" type= "radio" name= "component_state" value= "inactive"></td>
</tr>
</table>
<br>

<!--Botão Inserir-->
<input type= "hidden" name= "state" value= "inserir">
<input class= "button" type= "submit" value= "Inserir Componente">
<br><br>

</fieldset></center>
</form>
<?php
}
else if($_REQUEST['state'] == "inserir")
{
?>
<h3>Management component - insertion</h3>
<?php
$component_name = $_REQUEST['component_name'];
$component_type = $_REQUEST['component_type'];
$component_state = $_REQUEST['component_state'];


$insert = sprintf("INSERT INTO `concept` (`name`,`conc_type_id`,`state`) VALUES ('%s', '%s', '%s')", mysqli_real_escape_string($conn, $component_name), mysqli_real_escape_string($conn, $component_type),
mysqli_real_escape_string($conn, $component_state));

$resultado_insert = mysqli_query($conn, $insert);

if($resultado_insert)
{
mysqli_query($conn,'COMMIT');
?>
<p>Successfull insertion</p>
<p>Click in <a href = "gestao-de-componentes">Continue</a> to advance.</p>
<?php
}
else
{
mysqli_query($conn,'ROLLBACK');
?>
<p>Insertion error</p>
<?php
}
back();
}
}
?>
</body>
</html>

最佳答案

首先,当您创建表时...获取查询结果...我认为应该是这样的:

 <div class = "mytable">
<?php
$queryComp_Type = "select id, name from conc_type order by name";
$resultadoQueryComp_Type = mysqli_query($conn, $queryComp_Type);
$tableHeader = "";
$tableBody = "";
?>
<table>
<thead>
<?php
while ($row = mysqli_fetch_array($resultadoQueryComp_Type)) {
$tableHeader .= '<tr>
<th>'.$row['name'].';</th>
</tr>';
}
?>
<?=$tableHeader?>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_array($resultadoQueryComp_Type)) {
$tableBody .= '<tr>
<td>'.$row['id'].'</td>
</tr>';
}

?>
<?=$tableBody?>
</tbody>
</table>
</div>

关于php - 插入错误(PHP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41146013/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com