0

I am a bit new to PHP development. I have a form that I need to submit and insert values into a specific table.

For example:

I have H&M value in a drop down list, and when I choose this option in a drop down list all the values are inserted in all other tables and in a table that I have chosen.

I have this part first:

$projectIndex = 0;
switch ('ddlProjekat') 
{
    case "0":
        $projectIndex = 0;
        break;
    case "H&M":
        $projectIndex = 1;
        break;
    case "Takko Fashion":
        $projectIndex = 2;
        break;
    case "Deichmann":
        $projectIndex = 3;
        break;
    case "CortefielSRB":
        $projectIndex = 4;
        break;
    case "CortefielBiH":
        $projectIndex = 5;
        break;
    case "CortefielMNE":
        $projectIndex = 6;
        break;
    case "CortefielCRO":
        $projectIndex = 7;
        break;          
    case "C&A Fashion":
        $projectIndex = 8;
        break;
    case "Sephora":
        $projectIndex = 9;
        break;
    case "Lurdy":
        $projectIndex = 10;
        break;
}

Then goes the HTML form with all the fields, and then this part:

if(isset($_POST['btnSubmit']))
{
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}

if(! get_magic_quotes_gpc() )
{
   $prodavnica = addslashes ($_POST['tbBrProd']);
   $grad = addslashes($_POST['tbGrad']);
   $adresa = addslashes($_POST['tbAddress']);
   $telefon = addslashes($_POST['taTelefon']);
   $rvreme = addslashes($_POST['taRVreme']);
   $aglk = addslashes($_POST['taAGLK']);
   $datumtp = addslashes($_POST['tbDatum']);
   $usb = addslashes($_POST['tbUSB']);
   $iprange = addslashes($_POST['tbIPRange']);
   $projekat = addslashes($_POST['ddlProjekat']);
   }
else
{
   $prodavnica = addslashes ($_POST['tbBrProd']);
   $grad = addslashes($_POST['tbGrad']);
   $adresa = addslashes($_POST['tbAddress']);
   $telefon = addslashes($_POST['taTelefon']);
   $rvreme = addslashes($_POST['taRVreme']);
   $aglk = addslashes($_POST['taAGLK']);
   $datumtp = addslashes($_POST['tbDatum']);
   $usb = addslashes($_POST['tbUSB']);
   $iprange = addslashes($_POST['tbIPRange']);
   $projekat = addslashes($_POST['ddlProjekat']);
   }
if($projectIndex = 1){


$sql = "INSERT INTO hm ".
       "(StoreID, Grad, Adresa, Telefon, RadnoVreme, AGLK, DatumTP, BeetleModel, IPRange)".
       "VALUES('$prodavnica','$grad','$adresa', '$telefon', '$rvreme', '$aglk', '$datumtp', '$usb', '$iprange')";


mysql_select_db('fiskali');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not enter data: ' . mysql_error());
}
}
?>
<?php
if($projectIndex = 2){


$sql = "INSERT INTO takko ".
       "(StoreID, Grad, Adresa, Telefon, RadnoVreme, AGLK, DatumTP, BeetleModel, IPRange) ".
       "VALUES('$prodavnica','$grad','$adresa', '$telefon', '$rvreme', '$aglk', '$datumtp', '$usb', '$iprange')";


mysql_select_db('fiskali');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not enter data: ' . mysql_error());
}
}
?>
<?php
if($projectIndex = 3){


$sql = "INSERT INTO deichmann ".
       "(StoreID, Grad, Adresa, Telefon, RadnoVreme, AGLK, DatumTP, BeetleModel, MS4Sync) ".
       "VALUES('$prodavnica','$grad','$adresa', '$telefon', '$rvreme', '$aglk', '$datumtp', '$usb', '$iprange')";


mysql_select_db('fiskali');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not enter data: ' . mysql_error());
}
}
?>
<?php
if($projectIndex = 4){


$sql = "INSERT INTO cortefiel ".
       "(StoreID, Grad, Adresa, Telefon, RadnoVreme, AGLK, DatumTP, BeetleModel, IPRange) ".
       "VALUES('$prodavnica','$grad','$adresa', '$telefon', '$rvreme', '$aglk', '$datumtp', '$usb', '$iprange')";


mysql_select_db('fiskali');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not enter data: ' . mysql_error());
}
}
?>
<?php
if($projectIndex = 5){


$sql = "INSERT INTO cortefiel_bih ".
       "(StoreID, Grad, Adresa, Telefon, RadnoVreme, AGLK, DatumTP, BeetleModel, IPRange) ".
       "VALUES('$prodavnica','$grad','$adresa', '$telefon', '$rvreme', '$aglk', '$datumtp', '$usb', '$iprange')";


mysql_select_db('fiskali');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not enter data: ' . mysql_error());
}
}
?>
<?php
if($projectIndex = 6){


$sql = "INSERT INTO cortefiel_mne ".
       "(StoreID, Grad, Adresa, Telefon, RadnoVreme, AGLK, DatumTP, BeetleModel, IPRange) ".
       "VALUES('$prodavnica','$grad','$adresa', '$telefon', '$rvreme', '$aglk', '$datumtp', '$usb', '$iprange')";


mysql_select_db('fiskali');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not enter data: ' . mysql_error());
}
}
?>
<?php
if($projectIndex = 7){


$sql = "INSERT INTO cortefiel_cro ".
       "(StoreID, Grad, Adresa, Telefon, RadnoVreme, AGLK, BeetleModel, IPRange) ".
       "VALUES('$prodavnica','$grad','$adresa', '$telefon', '$rvreme', '$aglk', '$usb', '$iprange')";


mysql_select_db('fiskali');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not enter data: ' . mysql_error());
}
}
?>
<?php
if($projectIndex = 8){


$sql = "INSERT INTO ca ".
       "(StoreID, Grad, Adresa, Telefon, RadnoVreme, AGLK, DatumTP, BeetleModel, IPRange) ".
       "VALUES('$prodavnica','$grad','$adresa', '$telefon', '$rvreme', '$aglk', '$datumtp', '$usb', '$iprange')";


mysql_select_db('fiskali');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not enter data: ' . mysql_error());
}
}
?>

I hope this was understandable.

I am open to all kind of questions and also critics.

Thanks in advance

4
  • What's your specific problem? Is it that your code isn't working? Please be more specific. Commented May 27, 2014 at 15:03
  • My code is working. Form is submitted, values are inserted. But drop down list choice is not working. When I choose an option, it is inserted in the right table, but also in all other tables. Commented May 27, 2014 at 15:20
  • Your if statements are assigning, rather than equality checking: Change if($projectIndex = 1){ to if($projectIndex == 1){. (All 8.) These assignments return true therefore each condition is executed. Commented May 27, 2014 at 15:52
  • The switch ('ddlProjekat') condition is looking either for a boolean true or string 'ddlProjekat', so $projectIndex remains 0. Commented May 27, 2014 at 16:03

2 Answers 2

2

Two problems that I can see, 1) the switch statement value was incorrectly formatted (string given, ought to be a POST variable), and 2) the conditions contained assignments, not a equality checks. Those and there is excessive redundancy. Consider this:

<?php

if(isset($_POST['btnSubmit']))
{

    $conn = new mysqli('localhost ', 'root', '', 'fiskali');
    if ($conn->connect_error)
    {
      trigger_error('Database connection failed: '  . $conn->connect_error, E_USER_ERROR);
    }

    switch ( $_POST['ddlProjekat'] )
    {
        case 'H&M'          : $tablename = 'hm';    break;
        case 'Takko Fashion': $tablename = 'takko'; break;
        case 'Deichmann'    : $tablename = 'takko'; break;
        // remaining case statements here
    }

    $sql = "INSERT INTO $tablename (StoreID, Grad, Adresa, Telefon, RadnoVreme, AGLK, DatumTP, BeetleModel, IPRange) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";

    $stmt = $conn->prepare($sql);
    $stmt->bind_param('issssssss', $_POST['tbBrProd'], $_POST['tbGrad'], $_POST['tbAddress'], $_POST['taTelefon'], $_POST['taRVreme'], $_POST['taAGLK'], $_POST['tbDatum'], $_POST['tbUSB'], $_POST['tbIPRange']);
    $stmt->execute();

    if($stmt === false)
    {
        trigger_error('Error: ' . $sql . $conn->error, E_USER_ERROR);
    }
    $stmt->close();

}

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

1 Comment

You ought to determine the types of the values inserted into the database and enter the correct indicator in the bind_param. php.net/manual/en/mysqli-stmt.bind-param.php
0

I think your switch case not working because in switch 'ddlProjekat' is static. No case follow this, Try to make a variable it. eg, $ddlProjekat. The value of $projectIndex is always 0.

2 Comments

'ddlProjekat' is the name of HTML dropdown list. I think it should stay static. Maybe I am wrong
My first guess is what you're checking is $_POST['ddlProjekat'] .

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.