[英]Php Select rows through html form, and write them to txt file
我有我的產品庫存數據庫,每個產品的庫存數量和我們的價格。 我試圖做到這一點,當我輸入例如12345
part # 時,它會在表inventory
搜索$POST_partnumber
。 取零件號 + 我們的價格,並在 txt 文件中為我輸入的每個零件號寫入並按 Enter。
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 );
您也可以這樣做。 使用file_put_contents()
和file_get_contents()
你只需要設置一些東西
<!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";
}
}
?>
您可以嘗試如下操作:
<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/zh/mysqli.error.php
所有這些數組都需要更改為常規引號。
您問題中包含的帖子的鏈接也包含對勾; 這是不正確的。
您實際上可以只使用error_log
$log = "whatever \n";
error_log($log, 3 , "log.txt");
$ log設置為$ log都會將其記錄到log.txt
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.