Php Select 行通过 html 形式,并写入txt文件
Php Select rows through html form, and write them to txt file
我有一个产品库存数据库,其中包含每个产品的库存量和我们的价格。我试图做到这一点,以便当我键入 12345
部分 # 时,它会在 table inventory
中搜索 $POST_partnumber
。并获取零件号 + 我们对该商品的价格,然后将我输入的每个零件号写入一个 txt 文件,然后按回车键。
partnumber ourprice
partnumber ourprice
partnumber ourprice
partnumber ourprice
我开始获取代码,我将自己添加一些额外的功能,但无法获取它来搜索用户输入并将列 our price
和 part number
放入一个 txt 文件。
谢谢
这是我发现的:
$fp1 = fopen( 'obspg.txt', 'w' );
$outPut = "RETSKU\tProduct Title\tDetailed Description\tProduct Condition\tSelling Price\tAvailability\tProduct URL\tImage URL\tManufacturer Part Number\tManufacturer Name\tCategorization\tGender\tsize\tColor\n";
//retrive records from database and write to file
$result = mysqli_query($con,"SELECT * FROM `TABLE 1` ");
while($row = mysqli_fetch_array($result))
{
$outPut .= $row[`id`]."\t".$row[`title`]."\t". $row[`description`]."\t".$row[`condition`]."\t". $row[`price`]."\t".$row[`availability`]."\t".$row[`link`]."\t". $row[`image_link`]."\t".$row[`mpn`]."\t".$row[`brand`]."\t".$row[`google_product_category`]."\t".$row[`Gender`]."\t".$row[`size`]."\t".$row[`Color`]."\n";
}
fwrite( $fp1,$outPut);
fclose( $fp1 );
Link to post
您可以尝试这样的操作:
<form action="yourFile.php" method="POST">
<input type="text" name="PartNumber" id="PartNumber">
<input type="submit" name="submit" value="Submit">
</form>
<?php
if(isset($_POST['submit']) && !empty($_POST['PartNumber'])){
$fp1 = fopen( 'obspg.txt', 'w' );
$outPut = "";
//retrive records from database and write to file
$result = mysql_query("SELECT PartNumber, Price FROM `TABLE 1` WHERE PartNumber = '".$_POST['PartNumber']."'");
while($row = mysql_fetch_assoc($result))
{
$outPut .= $row['PartNumber']."\t".$row['Price']."\t\n";
}
fwrite( $fp1,$outPut);
fclose( $fp1 );
}
?>
脚注:
您在数组中使用刻度,例如:
$row[`id`]
它们应该是常规引号 $row['id']
,否则会引发错误。
http://php.net/manual/en/mysqli.error.php
所有这些数组都需要更改为常规引号。
The link 到您在问题中包含的 post,也包含刻度线;这是不正确的。
你也可以这样做。使用 file_put_contents()
& file_get_contents()
你只需要设置一些东西
- 将路径 test.php 更改为您的文件名
- 连接数据 $host, $user, $pass, $dbdb
- Table & 字段名称
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<!-- path to current page -->
<form action="test2.php" method="POST">
Partnumber: <input type="text" name="part"><input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
<?php
//connection data
$user = "";
$pass = "";
$host = "";
$dbdb = "";
$connect = mysqli_connect($host, $user, $pass, $dbdb);
if(!$connect)
{
trigger_error('Error connection to database: '.mysqli_connect_error());
}
if(isset($_POST['submit']) && !empty($_POST['part'])){
//retrive records from database and write to file
$result = mysqli_query($connect,"SELECT * FROM `table` WHERE `partnumber` = '".$_POST['part']."'");
//count rows, number > 0 then partnumber exists
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result))
{
//check if part is in stock, when > 0 its in stock
if ($row['stock'] > 0) {
echo "You already have " . $row['stock'] . " of part " . $row['partNumber'] . "in stock.";
} else {
echo "<br>Added partnumber: ". $_POST['part'] . "<br><br>";
$output = $row['partNumber']." ". $row['promoCode'] . PHP_EOL;
$file = 'test.txt';
//check file existence
if (file_exists($file)) {
$current = file_get_contents($file);
} else{
$current = null;
}
$current .= $output;
echo nl2br($current);
file_put_contents($file, $current);
}
}
}else{
echo "<br>Partnumber doesn't exist";
}
}
?>
我有一个产品库存数据库,其中包含每个产品的库存量和我们的价格。我试图做到这一点,以便当我键入 12345
部分 # 时,它会在 table inventory
中搜索 $POST_partnumber
。并获取零件号 + 我们对该商品的价格,然后将我输入的每个零件号写入一个 txt 文件,然后按回车键。
partnumber ourprice
partnumber ourprice
partnumber ourprice
partnumber ourprice
我开始获取代码,我将自己添加一些额外的功能,但无法获取它来搜索用户输入并将列 our price
和 part number
放入一个 txt 文件。
谢谢
这是我发现的:
$fp1 = fopen( 'obspg.txt', 'w' );
$outPut = "RETSKU\tProduct Title\tDetailed Description\tProduct Condition\tSelling Price\tAvailability\tProduct URL\tImage URL\tManufacturer Part Number\tManufacturer Name\tCategorization\tGender\tsize\tColor\n";
//retrive records from database and write to file
$result = mysqli_query($con,"SELECT * FROM `TABLE 1` ");
while($row = mysqli_fetch_array($result))
{
$outPut .= $row[`id`]."\t".$row[`title`]."\t". $row[`description`]."\t".$row[`condition`]."\t". $row[`price`]."\t".$row[`availability`]."\t".$row[`link`]."\t". $row[`image_link`]."\t".$row[`mpn`]."\t".$row[`brand`]."\t".$row[`google_product_category`]."\t".$row[`Gender`]."\t".$row[`size`]."\t".$row[`Color`]."\n";
}
fwrite( $fp1,$outPut);
fclose( $fp1 );
Link to post
您可以尝试这样的操作:
<form action="yourFile.php" method="POST">
<input type="text" name="PartNumber" id="PartNumber">
<input type="submit" name="submit" value="Submit">
</form>
<?php
if(isset($_POST['submit']) && !empty($_POST['PartNumber'])){
$fp1 = fopen( 'obspg.txt', 'w' );
$outPut = "";
//retrive records from database and write to file
$result = mysql_query("SELECT PartNumber, Price FROM `TABLE 1` WHERE PartNumber = '".$_POST['PartNumber']."'");
while($row = mysql_fetch_assoc($result))
{
$outPut .= $row['PartNumber']."\t".$row['Price']."\t\n";
}
fwrite( $fp1,$outPut);
fclose( $fp1 );
}
?>
脚注:
您在数组中使用刻度,例如:
$row[`id`]
它们应该是常规引号 $row['id']
,否则会引发错误。
http://php.net/manual/en/mysqli.error.php
所有这些数组都需要更改为常规引号。
The link 到您在问题中包含的 post,也包含刻度线;这是不正确的。
你也可以这样做。使用 file_put_contents()
& file_get_contents()
你只需要设置一些东西
- 将路径 test.php 更改为您的文件名
- 连接数据 $host, $user, $pass, $dbdb
- Table & 字段名称
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<!-- path to current page -->
<form action="test2.php" method="POST">
Partnumber: <input type="text" name="part"><input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
<?php
//connection data
$user = "";
$pass = "";
$host = "";
$dbdb = "";
$connect = mysqli_connect($host, $user, $pass, $dbdb);
if(!$connect)
{
trigger_error('Error connection to database: '.mysqli_connect_error());
}
if(isset($_POST['submit']) && !empty($_POST['part'])){
//retrive records from database and write to file
$result = mysqli_query($connect,"SELECT * FROM `table` WHERE `partnumber` = '".$_POST['part']."'");
//count rows, number > 0 then partnumber exists
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result))
{
//check if part is in stock, when > 0 its in stock
if ($row['stock'] > 0) {
echo "You already have " . $row['stock'] . " of part " . $row['partNumber'] . "in stock.";
} else {
echo "<br>Added partnumber: ". $_POST['part'] . "<br><br>";
$output = $row['partNumber']." ". $row['promoCode'] . PHP_EOL;
$file = 'test.txt';
//check file existence
if (file_exists($file)) {
$current = file_get_contents($file);
} else{
$current = null;
}
$current .= $output;
echo nl2br($current);
file_put_contents($file, $current);
}
}
}else{
echo "<br>Partnumber doesn't exist";
}
}
?>