数据库字段数据不出现在PHP的表单文本框中

时间:2015-07-29 02:32:49

标签: php html mysql sql

我有这个代码在PHP和数据库sql ..情况是..如果我键入1,2或3(productID)..文本框将填充和字段与数据库值..但当我运行程序..幸运的是它没有错误..但当我键入id或1并单击提交按钮..它没有得到必要的值..对不起,这是一个完整的新手,我现在正在练习PHP一段时间。任何帮助都会做..谢谢..

<?php
session_start();
include_once 'dbconnect.php';

if(!isset($_SESSION['user'])){
header("Location: index.php");
}

$res = mysql_query("SELECT * FROM users WHERE user_id=".$_SESSION['user']);
$userRow = mysql_fetch_array($res);
?>

<?php
require('dbconnect.php');
$id = (isset($_REQUEST['productID']));

$result = mysql_query("SELECT * FROM tblstore WHERE productID = '$id'");
$sql = mysql_fetch_array($result);

if(!$result){
die("Error: Data not found");
} else {
$brandname = $sql['brandname'];
$price = $sql['price'];
$stocks = $sql['stocks'];
}

?>

<html>
<body>
<p>
hi' <?php echo $userRow['username']; ?>&nbsp;<a href="logout.php?logout">Sign Out</a>
</p>

<form method="post">
<table align="center">
<tr>
<td>Search Apparel:</td>
<td><input type="text" name="search" name="productID" /></td>
</tr>
<tr>
<td>Brandname:</td>
<td><input type="text" name="brandname" value="<?php echo $brandname; ?>"/ </td>
</tr>
<tr>
<td>Price:</td>
<td><input type="text" name="price" value="<?php echo $price; ?>"/></td>
</tr>
<tr>
<td>Stocks:</td>
<td><input type="text" name="stocks" value="<?php echo $stocks; ?>"/></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" name="submit" value="Search" /></td>
</tr>
</table>
</form>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

你错误地获取了id,你有:

<?php
$_REQUEST['productID']=8; //for testing
$id = (isset($_REQUEST['productID']));

如果你检查它,你会发现isset

返回的输出为true \ false
var_dump($id); //true

你应该使用的是:

<?php

if(isset($_REQUEST['productID'])){ //maybe also check its a number and or valid range
$id=$_REQUEST['productID'];
}