如何解决注意:Undefined index: id in C:\xampp\htdocs\invmgt\manufactured_goods\change.php on line 21
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26670444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to solve Notice: Undefined index: id in C:\xampp\htdocs\invmgt\manufactured_goods\change.php on line 21
提问by NoviceProgrammer
I have a problem with my PHP code saying that "Notice: Undefined index" I am sure its very simple, since I am a beginner i am not getting well what is wrong exactly so please help me.
我的 PHP 代码有一个问题,上面写着“注意:未定义的索引”,我确信它很简单,因为我是初学者,我不太清楚到底是哪里出了问题,所以请帮助我。
Here's the code
这是代码
<?php require_once('../Connections/itemconn.php'); ?>
<?php
$id=$_GET['id'];
$query=mysql_query("select * from manuf where id='$id' ")or die(mysql_error());
$row=mysql_fetch_array($query);
?>
<form action="updateprice.php" method="post" enctype="multipart/form-data">
<table align="center">
<tr>
<td> <label><strong>Item Name</strong></label></td>
<td> <label> <?php echo $row['itemname']; ?></label><input type="hidden" name="id" value="<?php echo $id; ?> " />
<br /></td>
</tr>
<tr>
<td><label><strong>Unit price </strong></label></td>
<td> <input type="text" name="pass" value="<?php echo $row['unitprice']; ?> " /><br /></td>
</tr>
<tr>
<td>
<input type="reset" name="Reset" value="CANCEL" />
<br></td>
<td>
<input type="submit" name="Submit2" value="Update" /> </td>
</tr>
</table>
</form>
</body>
</html>
回答by Pupil
You are not getting value of $id=$_GET['id'];
你没有得到价值 $id=$_GET['id'];
And you are using it (before it gets initialised).
你正在使用它(在它被初始化之前)。
Use php's in built isset()function to check whether the variable is defied or not.
使用 php 内置的isset()函数来检查变量是否被定义。
So, please update the line to:
因此,请将该行更新为:
$id = isset($_GET['id']) ? $_GET['id'] : '';
回答by Rakesh Sharma
if you are getting id from url try
如果您从 url 获取 id 尝试
$id = (isset($_GET['id']) ? $_GET['id'] : '');
if getting from form you need to use POST
method cause your form has method="post"
如果从表单获取,您需要使用POST
方法,因为您的表单有method="post"
$id = (isset($_POST['id']) ? $_POST['id'] : '');
For php notices use isset()
or empty()
to check values exist or not or initialize variable first with blank or a value
对于 php 通知,使用isset()
或empty()
检查值是否存在或首先使用空白或值初始化变量
$id= '';
回答by BKY
Simply add this
只需添加这个
$id = '';
if( isset( $_GET['id'])) {
$id = $_GET['id'];
}