0

I am trying to use a select fetching data form the db with a post method to submit and I need to stay on the same page.

Here is what I've tried, I am using a MySQL.

$sql = "SELECT * FROM `client` WHERE 1";
if ($result = mysqli_query($conn, $sql)) {
    echo'<body>
         <div><p> vue global des client </p></div>
        <form action="#" method="post">
         <select name = "Client">';
    while ($row = mysqli_fetch_assoc($result)) {
        echo "<option value = '" . $row['client_code'] . "'>" . $row['client_nom'] . "</option>";
    }
    echo '</select>  
        <button type="submit" name"submit"> Vue specifique </button>
        </form><br>';
    if (isset($_POST['submit'])) {
        mysqli_free_result($conn);
        $clientCode = $_POST['Client'];
        echo "<p> You have selected :" .$clientCode."</p>";
        $sql = "SELECT a.*, b.client_nom, c.site_nom, d.contenant_nom, e.transporteur_nom FROM `mouvement`a
        left join `client` b on a.client_code = b.client_code
        left join `siteprod` c on a.site_code = c.site_code
        left join `contenant` d on a.contenant_code = d.contenant_code
        left join `transporteur` e on a.transporteur_code = e.transporteur_code
        WHERE a.client_code = $clientCode";
        if ($result = mysqli_query($conn, $sql)) {
            echo '<table border="1">';
            echo "<tr><td>Code</td><td>Type</td><td>Date</td><td>Site de prod</td><td>Client</td><td>Contenant</td><td>Quantit&eacute;</td><td>Transporteur</td></tr>";
            while ($row = mysqli_fetch_assoc($result)) {
                echo "<tr><td>{$row["mouvement_code"]}</td><td>{$row["mouvement_type"]}</td><td>{$row["mouvement_date"]}</td><td>{$row["site_nom"]}</td><td>{$row["client_nom"]}</td><td>{$row["contenant_nom"]}</td><td>{$row["contenant_quantite"]}</td><td>{$row["transporteur_nom"]}</td></tr>\n";
            }
            echo '<br>';
            mysqli_free_result($conn);
        }
    } else {
        echo "<p>no submit</p>";
        $sql = "SELECT a.*, b.client_nom, c.site_nom, d.contenant_nom, e.transporteur_nom FROM `mouvement`a
            left join `client` b on a.client_code = b.client_code
            left join `siteprod` c on a.site_code = c.site_code
            left join `contenant` d on a.contenant_code = d.contenant_code
            left join `transporteur` e on a.transporteur_code = e.transporteur_code
            WHERE 1";
        if ($result = mysqli_query($conn, $sql)) {
            echo '<table border="1">';
            echo "<tr><td>Code</td><td>Type</td><td>Date</td><td>Site de prod</td><td>Client</td><td>Contenant</td><td>Quantit&eacute;</td><td>Transporteur</td></tr>";
            while ($row = mysqli_fetch_assoc($result)) {
                echo "<tr><td>{$row["mouvement_code"]}</td><td>{$row["mouvement_type"]}</td><td>{$row["mouvement_date"]}</td><td>{$row["site_nom"]}</td><td>{$row["client_nom"]}</td><td>{$row["contenant_nom"]}</td><td>{$row["contenant_quantite"]}</td><td>{$row["transporteur_nom"]}</td></tr>\n";
            }
            echo '<br>';
            mysqli_free_result($conn);
        }

I'm ending on the else case every-time. Did I do something wrong?

marin dcv
  • 35
  • 1
  • 5
  • I'm well aware, but this page (not the complete code here) is only accessible once logged in (with prepared statement) and this site will be only working on a company LAN. no exterior access. So I'm not too worried about that. thanks anyway – marin dcv Jul 12 '19 at 11:40
  • You are not worried about bugs in your code? If your code is broken you should fix it whether other people have access to it or not. It is still a bug! – Dharman Jul 12 '19 at 11:44
  • I'm not using a prepared statement because when I do nothing show up. I don't really have time to look for a new solution. I'm already behind shedule. It will be fixed eventually but not this summer. – marin dcv Jul 12 '19 at 11:47
  • You can use ajax request to do your task. [Refference] (https://stackoverflow.com/questions/16616250/form-submit-with-ajax-passing-form-data-to-php-without-page-refresh) – MjM Jul 12 '19 at 11:59
  • AJAX seems like it'd just be a complicating factor in this mess. – waterloomatt Jul 12 '19 at 12:00

3 Answers3

1

The reason it always ends on the "else" statement is because you are checking if isset($_POST['submit']). When you use a button with type submit in a form it will act like a input type submit, but it will not send the name as a POST value. Meaning $_POST['submit'] will never exist even though you set the name to submit on the button.

I suggest trying one of these solutions:

  1. Change the button to a input type submit instead
  2. use if(isset($_POST['Client'])) instead. Beacause the select will be included in the POST

Hope that helps :)

Joachims
  • 48
  • 4
  • Yes, this will work but ` – waterloomatt Jul 12 '19 at 12:07
  • Thanks, it worked. I had another mistake, in my sql code, I wrote ```WHERE $clientCode```instead of where ```WHERE client_id = $clientCode``` – marin dcv Jul 12 '19 at 12:07
1

There is a typo in our code.

Replace this

<button type="submit" name"submit"> Vue specifique </button>

with

 <button type="submit" name="submit"> Vue specifique </button>
Sukhjinder Singh
  • 479
  • 3
  • 15
-1

Set form action as :

<form action="/" method="post">
    <input type="text" id="user_id" name="user_id">
    <input type="submit" id="submit_form" name="submit_form" value="Submit">
</form>

Use this at beginning of your php file.

<?php
if(isset($_POST['submit_form']) && $_POST['submit_form'] == "Submit" ){
$query = "SELECT * FROM users where id=".$_POST['user_id'];
 if ($result = mysqli_query($conn, $query)) {
      while ($row = mysqli_fetch_assoc($result)) {
            echo "<pre>";
               print_r($row);
            echo "</pre>";
        }
 }
}
?>