G. Evens G. Evens - 5 months ago 13
SQL Question

select multiple options sql and php

I'm working on a small project at work. The intention is to receive data from my MS SQL Express server 2008 to a webpage. This is all working fine. I use the ODBC connect extension to do this. I have set a callender, so you can pick date's between you want to receive. But like i said, this is all working fine.

My problem:
I want to get data from diffrent SQL servers. 3 in total. My sql statement is on every sql server the same. The only thing that need to change is the connection string and the database. Here you will find my PHP Code. I will use the $_POST in php ro receive the action from my form.

In HTML you can see that i use a select option. Whenever i choose for 'Grimbergen' the connection string must change to $connection_string = $grim; And when I choose for 'Gent' the connection string must change

$connection_string = $topo;

if (isset($_POST['knop'])) {
$user = 'someuser';
$pass = 'somepassword';
$grim = 'DRIVER={SQL Server};SERVER=10.10.10.10\UNIWIN_GRIM;DATABASE=UniCla';
$topo = 'DRIVER={SQL Server};SERVER=10.10.10.10\UNIWIN_TOPO;DATABASE=UniCla';
$connection_string = ; //don't know how i get this with $_POST!!!
echo $connection_string;
$connection = odbc_connect( $connection_string, $user, $pass );

if (odbc_error()) {
echo odbc_errormsg($connection);
}
$connection = odbc_connect( $connection_string, $user, $pass );

// you must set the connection first
if (odbc_error()) {
echo odbc_errormsg($connection);
}

$bdate= $_POST['bdate'];
$edate= $_POST['edate'];
$grim_s = 'SRGR';
$topo_s = 'SRTO';
$stmt = "SELECT String1, String3, String13, String15, String4, String8, InOut, Weight1, Date1, Time1, Weight2, Date2, Time2, Nett FROM dbo.Move WHERE Date1 BETWEEN '$bdate' AND '$edate' AND String13='$grim_s' ORDER BY Date1 desc;";
$result = odbc_exec($connection, $stmt);


And the HTML:

<form enctype="multipart/form-data" method="post" name="formulier" id="formulier" action="">
<div>
<h4>Site</h4>
<p>
<select name="place" id="place" class="form-control" >
<option value="grim">Grimbergen</option>
<option value="gent">Gent</option>
</select>
</p>
</div>
<div>
<h4>Begin Date</h4>
<input name="bdate" id="picker1" class="form-control" type="text">
</div>
<div>
<h4>End Date</h4>
<input name="edate" id="picker2" class="form-control" type="text">
</div>
<br>
<div>
<button type="submit" value="login" class="btn btn-info btn-fill pull-right" name="knop" id="knop">Submit</button>
</div>
</form>

Answer

Fine Use select value as your DB Name like this

        <select name="place" id="place" class="form-control" >
            <option value="GRIM">Grimbergen</option>
            <option value="TOPO">Gent</option>
        </select>

And when you post use one connection string like this

$connection_string ='DRIVER={SQL server};SERVER=10.10.10.10\UNIWIN_'.$_POST['place'].';DATABASE=UniCla';