Jump to content

Recommended Posts

<form name="input" action="NewProduct.php" method="post">
ID: <input type="text" name="ID" />
Product: <input type="text" name="Product" />
Price: <input type="text" name="Price" />
<input type="submit" value="Submit" />
</form>

<?php 

$server = '********';
$connectionInfo = array( "Database"=>"********");
$conn = sqlsrv_connect($server,$connectionInfo);

$insert_query = "INSERT INTO Car (Make,Model,Price) VALUES (?,?,?)";
$Make = $_POST['Make'];
$Model = $_POST['Model'];
$Price = $_POST['Price'];

$params = array("$Make","$Model", "$Price" );

$result = sqlsrv_query($conn,$insert_query,$params);


$describeQuery="select Make,Model,Price from Cars";
$results = sqlsrv_query($conn, $describeQuery);


echo '<table border="1" BORDERCOLOR=Black>';
echo '<tr><th bgcolor = "LightBlue">Make</th><th bgcolor = "LightBlue">Model</th><th bgcolor = "LightBlue">Price</th></tr>';


while($row = sqlsrv_fetch_array($results, SQLSRV_FETCH_ASSOC)) 
{
    echo '<tr>';

echo '<td >' .$row['Make'].'</td>'; 
echo '<td >' .$row['Model'].'</td>'; 
echo '<td >£' .$row['Price'].'</td>'; 
echo '</tr>';
} 

echo '</table>';


sqlsrv_close($conn);

 

This is suppose to let me enter new cars into a table however when i refresh this page it keeps re entering the previously entered car.

 

Link to comment
https://forums.phpfreaks.com/topic/259871-php-clearing-post/#findComment-1331897
Share on other sites

You are not checking for a POST request, so you are just going to be inserting empty rows every time you load the page. The $_POST superglobal is only going to be populated when you send a POST request.

 

So do something like;

if (!empty($_POST)) {
$insert_query = "INSERT INTO Car (Make,Model,Price) VALUES (?,?,?)";
$Make = $_POST['Make'];
$Model = $_POST['Model'];
$Price = $_POST['Price'];

$params = array("$Make","$Model", "$Price" );

$result = sqlsrv_query($conn,$insert_query,$params);


$describeQuery="select Make,Model,Price from Cars";
$results = sqlsrv_query($conn, $describeQuery);
}

Link to comment
https://forums.phpfreaks.com/topic/259871-php-clearing-post/#findComment-1331900
Share on other sites

Yes this will solve the problem of entering empty rows however there is still the problem of entering a car and that being entered twice into the table.

Eg. when i enter 'ferrari' into the table 'ferrari' is added again when the page is refreshed

 

:wtf:

Link to comment
https://forums.phpfreaks.com/topic/259871-php-clearing-post/#findComment-1331904
Share on other sites

Are you talking about when you refresh the page and you are asked to send the POST data again by your browser?

 

To get around that you'll need to header redirect to the same page.

if (!empty($_POST)) {
$insert_query = "INSERT INTO Car (Make,Model,Price) VALUES (?,?,?)";
$Make = $_POST['Make'];
$Model = $_POST['Model'];
$Price = $_POST['Price'];

$params = array("$Make","$Model", "$Price" );

$result = sqlsrv_query($conn,$insert_query,$params);


$describeQuery="select Make,Model,Price from Cars";
$results = sqlsrv_query($conn, $describeQuery);

header('location: yourpage.php');
}

 

However this is going to give you a header's already sent error because you have output above it. You'll either need to move your form below this code or use output buffering.

Link to comment
https://forums.phpfreaks.com/topic/259871-php-clearing-post/#findComment-1331909
Share on other sites

Right that seems now to be working however it will not now print out the table on the web page :(

 

would i have to change anything where the table is drawn?

 

Here is the code:

 

if (!empty($_POST)) {
$insert_query = "INSERT INTO Cars (Make,Model,Price) VALUES (?,?,?)";
$Make = $_POST['Make'];
$Model = $_POST['Model'];
$Price = $_POST['Price'];

$params = array("$Make","$Model", "$Price" );

$result = sqlsrv_query($conn,$insert_query,$params);


$describeQuery="select Make, Model, Price from Cars";
$results = sqlsrv_query($conn, $describeQuery);

header('location: NewCar.php');
}


echo '<table border="1" BORDERCOLOR=Black>';
echo '<tr><th bgcolor = "LightBlue">Make</th><th bgcolor = "LightBlue">Model</th><th bgcolor = "LightBlue">Price</th></tr>';


while($row = sqlsrv_fetch_array($results, SQLSRV_FETCH_ASSOC)) 
{
    echo '<tr>';

echo '<td >' .$row['Make'].'</td>'; 
echo '<td >' .$row['Model'].'</td>'; 
echo '<td >£' .$row['Price'].'</td>'; 
echo '</tr>';
} 

echo '</table>';

Link to comment
https://forums.phpfreaks.com/topic/259871-php-clearing-post/#findComment-1331919
Share on other sites

Oops, went too far with the conditional.

if (!empty($_POST)) {
$insert_query = "INSERT INTO Car (Make,Model,Price) VALUES (?,?,?)";
$Make = $_POST['Make'];
$Model = $_POST['Model'];
$Price = $_POST['Price'];

$params = array("$Make","$Model", "$Price" );

$result = sqlsrv_query($conn,$insert_query,$params);
}

Link to comment
https://forums.phpfreaks.com/topic/259871-php-clearing-post/#findComment-1331924
Share on other sites

I can see what you mean however i am back to square one, The table is now shown however there are duplicate records.

When i could not see the table before and i had this code:

$describeQuery="select name, Id, Price from Products";
$results = sqlsrv_query($conn, $describeQuery);

in the if statement as you originally suggested it seemed to be working fine. When i checked the table it only had one of each entry which is what i want it to have.

 

Link to comment
https://forums.phpfreaks.com/topic/259871-php-clearing-post/#findComment-1331936
Share on other sites

I forgot to stick the header redirect back in. Does that help?

if (!empty($_POST)) {
$insert_query = "INSERT INTO Car (Make,Model,Price) VALUES (?,?,?)";
$Make = $_POST['Make'];
$Model = $_POST['Model'];
$Price = $_POST['Price'];

$params = array("$Make","$Model", "$Price" );

$result = sqlsrv_query($conn,$insert_query,$params);

header('location: yourpage.php');
}

Link to comment
https://forums.phpfreaks.com/topic/259871-php-clearing-post/#findComment-1331946
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.