我有一个PHP生成的HTML表,它从MySQL数据库加载数据,在这个表中有一个用于编辑某些信息的按钮......当按下按钮时,它会将用户发送到另一个带有文本字段的页面,一个按钮。
用户可以插入新文本,按下按钮,新信息应存储在数据库中,用新信息更改旧信息...
但是当我点击按钮提交新信息时,会出现以下错误:
注意:未定义的索引:第11行的/...patch.../upload2.php中的ident
我做错了什么? (我是PHP的新手)
这是我的代码:
Resposta.php
<?php
ini_set('default_charset','UTF-8');
$con=mysqli_connect(“*******”,”*******”,”*******”,”*******”);
mysqli_set_charset($con,"utf8");
if (mysqli_connect_errno($con))
{
echo '{"query_result":"ERROR"}';
}
$emp_id = $_GET['id'];
$result = mysqli_query($con,"SELECT * FROM prefeitura WHERE id = $emp_id") ;
while($row = mysqli_fetch_array($result))
{
if($row['ID']) {
echo '<p><b>ID: </b>'. $row['ID'] .'';
}
if($row['nome']) {
echo '<p><b>SOLICITANTE: </b>'. $row['nome'] .'';
}
if($row['rua']) {
echo '<p><b>RUA: </b>'. $row['rua'] .'';
}
if($row['bairro']) {
echo '<p><b>BAIRRO: </b>'. $row['bairro'] .'</p>';
}
if($row['problema']) {
echo '<p><b>PROBLEMA: </b>'. $row['problema'] .'';
}
echo '<br>';
if($row['solucionado']) {
echo '<br><p><b>SITUAÇÃO: </b>'. $row['solucionado'] .'';
}
}
echo '<br><br><form enctype="multipart/form-data" action="upload2.php" method="post">';
echo '<br><input type="text" class="input-text text-area" name="resposta" id="resposta" placeholder="Escreva a resposta" required/>';
echo '<input type="submit" class="input-btn" ident="' .$row['ID']. '" value="Enviar Resposta" />';
mysqli_close($con);
?>
upload2.php
<?php
ini_set('display_errors', true); error_reporting(E_ALL);
ini_set('default_charset','UTF-8');
$con=mysqli_connect(“*******”,”*******”,”*******”,”*******”);
mysqli_set_charset($con,"utf8");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$emp_id = $_POST['ident'];
$resposta = nl2br(htmlentities($_POST['resposta'], ENT_QUOTES, 'UTF-8'));
$result = mysqli_query($con,"UPDATE prefeitura SET solucionado = '$resposta' WHERE ID = '$emp_id';");
header('Location: mensagem_enviada.html');
?>
答案 0 :(得分:0)
首先,问题是:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Price tool</title>
</head>
<body>
<form name="operation_form">
<label style="font-size:20px"><b>Price Tool</b></label>
<br/>
<br/>
<br/>
<label>Item Price: </label><input type="number" name='acertscore' value="" />
<br/>
<br/>
<label><b>Total is:</b></label><input type="number" name="answerbox">
<br/>
<input type="button" value="Calculate" onclick="costCalc();" />
<br/>
<input type="button" value="Reset" onClick="this.form.reset()" />
</form>
<script type="text/javascript">
function costCalc() {
var form = document.forms["operation_form"];
var x = form["acertscore"].value;
var cost = (x * .1) + x;
var answer = (cost * .08) + cost;
form["answerbox"].value = answer;
}
</script>
</body>
</html>
这告诉你$ _POST变量中没有'ident'。 查看发送表单时是否设置了'ident'。
这是你的......
Notice: Undefined index: ident in /...patch.../upload2.php on line 11
;
你应该做这样的事情 - &gt; (编辑)的 '';
你应该调整它以使它工作。我可能会错过'。 ;
然后,我看到的另一件事,你不应该这样做,你可能容易受到SQL INJECTION的攻击。</ p>
'<input type="submit" class="input-btn" ident="' .$row['ID']. '" value="Enviar Resposta" />'
您应该使用准备语句和PDO。 http://php.net/manual/en/pdo.prepared-statements.php
答案 1 :(得分:0)
我设法解决了问题:按下按钮时没有发送ID值...
这是有效的代码:
$mudaid = $_GET['id'];
echo <<<HTML
<form enctype="multipart/form-data" action="upload2.php" method="post">
<br><textarea rows="10" cols="70" name="resposta" id="resposta"></textarea>
<br><button type="submit" name="ident" value="$mudaid">Enviar resposta</button>
HTML;
echo '</center>';