Php Select rows through html form, and write them to txt file

1.1k views Asked by At

I have a database of my products inventory, with each products amount that is in stock and our prices for them. I'm trying to make it so that when I type for example 12345 part # for it to search $POST_partnumber in table inventory. And take part number + our price of this item and write in a txt file for every part number I type in and hit enter.

partnumber ourprice
partnumber ourprice
partnumber ourprice
partnumber ourprice

I started getting the code and there is a few extra feature I will add on my own, but can't get it to search for user imput and put colum our price and part number into a txt file. Thanks

This is what I found:

$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

2

There are 2 answers

3
PHPeter On BEST ANSWER

You could also do something like this. Using file_put_contents() & file_get_contents()

You just need to set some things

  1. Change path test.php to your filename
  2. Connection data $host, $user, $pass, $dbdb
  3. Table & field names

<!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";
    }

}

?>
5
cmo On

You can try something like this:

<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 );

}
?>

Footnotes:

You're using ticks in the arrays, such as:

$row[`id`]

where they should be regular quotes $row['id'], which would have thrown an error.
http://php.net/manual/en/mysqli.error.php

  • All of those arrays need to be changed to regular quotes.

  • The link to the post you included in your question, also contains ticks; it's incorrect.