无法回复来自phpmyadmin的数据

时间:2017-07-04 12:24:59

标签: php html mysql

Am已经像这样创建并且工作正常但是在最后两个表单不起作用时,它显示警告 - 未定义变量:reg_no和cost。我试图按照以前的形式遵循算法但没有任何反应。我的目标是更新插入的数据,这是我的表格

<!doctype html>
<html>
<head>

<meta charset="utf-8">
<title>Edit invoice</title>
<link rel="stylesheet"  href="box_style.css" />
</head>

<body>

<?php
	
include ("db_con.php");

if(isset($_GET['edit_invoice'])){

        $edit_i_id = $_GET['edit_invoice'];

        $select_invoice = "select * from invoice where i_id='$edit_i_id'";

        $run_query = mysqli_query($con, $select_invoice);

        while ($row_invoice=mysqli_fetch_array($run_query)){
			
		$i_id = $row_invoice['i_id'];	
        $reg_no = $row_invoice['reg_no'];
        $cost = $row_invoice['cost'];

        }
    }

?>

<div class='form'>
<form action="" method="post" enctype="multipart/form-data" >
	
<table width="745" align="center" border="2">
	
	
<p style="text-align: center;"><strong><span style="text-decoration: underline;">EDIT INVOICE:</span></strong></p>
	
	<tr>		
		<td align="right" bgcolor="#dbe5f1"><strong>Registration Number:</strong></td>
		<td><input type="text" name="reg_no" id="reg_no" size="35" class="text" placeholder="Registration Number" value="<?php echo $reg_no; ?>" required=""/></td>		
	</tr>
	
	<tr>		
		<td align="right" bgcolor="#dbe5f1"><strong>Cost(Tshs):</strong></td>
		<td><input type="text" name="cost" id="cost" size="35" class="text" placeholder="Cost" value="<?php echo $cost; ?>" required=""/></td>
	</tr>

	
	<tr>
		<td colspan="6" align="center" bgcolor="#dbe5f1" ><input type="submit" name="update" class="submit-button" value="SAVE CHANGES"></td>		
	</tr>
	
	</table>
	
</form>
</div>


</body>


</html>

2 个答案:

答案 0 :(得分:1)

如果isset($_GET['edit_invoice'])为false,则您的$reg_no不会出现在以后的脚本中(您要回显它)。

将$ reg_no置于您的isset($ _ GET ...)检查之上并将其设置为null或空字符串。

$reg_no = null;
if (isset($_GET['edit_invoice'])) {
   // your code...
}

修改:对$cost$i_id;)

执行相同操作

请考虑Tom Uddings对SQL注入发表评论!

答案 1 :(得分:1)

从php代码中删除while循环,因为更新是基于id的一条记录 代码如下:

if(isset($_GET['edit_invoice'])){
    $edit_i_id = $_GET['edit_invoice'];
    $select_invoice = "select * from invoice where i_id='$edit_i_id'";
    $run_query = mysqli_query($con, $select_invoice);
    $row_invoice = mysqli_fetch_array($run_query);

    $i_id = $row_invoice['i_id'];   
    $reg_no = $row_invoice['reg_no'];
    $cost = $row_invoice['cost'];
}