1

i have a php page where i press a button to edit on another page the mysql db records: page name 'distintaBase.php' here there is a list of names that are products name. Each product cointains a list of components. When i press the edit button for one product i'm redirect to the new page 'modifDistBase.php'.

<a href="modifDistBase.php?dist_base=<?php echo $dist_base; ?>"></a>

the $dist_base variable is passed to the new page. I use it on the page title

<h2>Modifica distinta base <?php echo $_GET['dist_base']; ?></h2>

and then to load the mysql db

<?php 
                $dist_base = $_GET['dist_base'];
                $sql = "SELECT $dist_base.Id, $dist_base.Designator, $dist_base.Quantity, $dist_base.Description, $dist_base.Package, 
                                $dist_base.Manufacturer, $dist_base.Pn_Manufacturer, $dist_base.Pn_Utterson, $dist_base.Mounted
                                FROM $dist_base";                                                                                                     
?>

pressing ADD button i add a new component

if(isset($_POST['add_item'])){
                    $designator = $_POST['designator'];
                    $quantity = $_POST['quantity'];
                    $description = $_POST['description'];
                    $package = $_POST['package'];
                    $manufacturer = $_POST['manufacturer'];
                    $pn_manufacturer = $_POST['pn_manufacturer'];
                    $pn_utterson = $_POST['pn_utterson'];
                    $mounted = $_POST['mounted'];
                    $sql = "INSERT INTO $dist_base (designator,quantity,description,package,manufacturer,pn_manufacturer,pn_utterson,mounted)
                                            VALUES ('$designator','$quantity','$description','$package','$manufacturer','$pn_manufacturer','$pn_utterson','$mounted')";

                 if ($conn->query($sql) === TRUE) {                         
                        echo '<script>window.location.replace("modifDistBase.php?dist_base=" + $dist_base.value)</script>';
                    } else {
                        echo "Errore: " . $sql . "<br>" . $conn->error;
                        }
                }           

there is something wrong probably on the javascript.

echo '<script>window.location.replace("modifDistBase.php?dist_base=" + $dist_base.value)</script>';

the new component is added to the database (for example with #5 id) but do not appear on the page modifDistBase.php

if i press refresh on the browser or f5 now i can see the new component (#5 id) but on the database a new one is added #6 with the same items as #5

PS - header is already sent by menù page - have tried window.location.href with same result

2
  • Have you already output any data to the browser when you need to refresh? Commented Nov 1, 2019 at 12:26
  • I think $dist_base.value in echo '<script>window.location.replace("modifDistBase.php?dist_base=" + $dist_base.value)</script>'; should be $dist_base. like. echo '<script>window.location.replace("modifDistBase.php?dist_base=" + $dist_base)</script>'; Commented Nov 1, 2019 at 12:26

2 Answers 2

3

Why don't you try with:

header('Location: modifDistBase.php?dist_base='.$dist_base);

instead of echo javascript you can redirect immediatelly to the page + the variable

Sign up to request clarification or add additional context in comments.

1 Comment

Illia is right, and output of headers could be delayed untill all backend operations have been completed, can’t you do this Massimo?
1

there are some thing to fix on your code

Note the javascript you output with

echo '<script>window.location.replace("modifDistBase.php?dist_base=" + $dist_base.value)</script>';

will be precisely

<script>window.location.replace("modifDistBase.php?dist_base=" + $dist_base.value)</script>

Without checking the correctness of the js related to your goal, it's immediate to see you should write instead

echo '<script>window.location.replace("modifDistBase.php?dist_base="' . $dist_base . '")</script>';

to inject the php value into the javascript string. (you've also forgot a double quote b.t.w)

If you have doubts regarding your javascript, you can inspect the page on the browser and look at it (right click and "inspect element" or "analyse element").

Mind it is not recommended that you take the user input and use it as it is in an SQL instruction. You should do some sanification and use a prepared statement or escape the values before putting them in the sql.

That said, the javascript redirection you do looks a bit odd, maybe you can redirect from the php script without asking the client to do so. Look at the header php function for instruction about redirection done server side.

6 Comments

``` echo '<script>window.location.replace("modifDistBase.php?dist_base="' . $dist_base.value . '")</script>'; ``` give this error: Warning: Use of undefined constant value - assumed 'value' (this will throw an Error in a future version of PHP). In the menu page i have ``` if(empty($_SESSION['user_name'])){ header("location:login.php"); } ```
You're right, $dist_base.value is not correct php syntax. What type is this $dist_base = $_GET['dist_base']; ? That .value part is probably not necessary (I've also edited the answer to remove it)
Thanks Ruth that solved my problem but there is one to many doble quote after the =. The right command is ''' echo '<script>window.location.replace("modifDistBase.php?dist_base=' . $dist_base . ' ")</script>'; '''
Anyway, in order to avoid adding the record twice on page reload, consider the possibility of redirecting server side
Yes i also see this problem, but unfortunately i'm very beginner in PHP and also other languages. Can you tell me what i can search for on google precisely? Thanks very much
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.