1 Replies - 172 Views - Last Post: 30 January 2014 - 10:07 AM

#1 VB ASRAAF007  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 87
  • Joined: 11-April 12

Select Option to different tables in database

Posted 30 January 2014 - 12:52 AM

Hello people, i have a small problem. i have two tables and a select option in my form. i need to : when select the value of the select option e.g Port Louis, i need to fill the table port louis, and when i select the option : "Black River" i need to fill the table black river in the database...can u help?


<?php



$connect = mysql_connect("localhost", "root", "") or die(mysql_error());
$select_db = mysql_select_db("yakatrouver_test", $connect) or die(mysql_error());

if(@$_POST['annonce_envoyer']){

    error_reporting(0);

$district = $_POST['annonce_district_selection'];
$ville = mysql_escape_string($_POST['ville']);
$categorie = mysql_escape_string($_POST['annonce_categorie']);
$radio_particulier_professionelle = mysql_escape_string($_POST['annonce_radio']);
$radio_offres_demande = mysql_escape_string($_POST['annonce_offres']);
$nom = mysql_escape_string($_POST['annonce_nom']);
$email = mysql_escape_string($_POST['annonce_email']);
$telephone = mysql_escape_string($_POST['annonce_telephone']);
$checkbox_telephone = mysql_escape_string($_POST['checkbox_telephone']);  
$titre_annonce = mysql_escape_string($_POST['annonce_titre']);
$text_annonce = mysql_escape_string($_POST['annonce_text']);
$prix = mysql_escape_string($_POST['annonce_prix']);
$photo_principale1 = mysql_escape_string($_POST['annonce_photo_principale1']);
$photo_2 = mysql_escape_string($_POST['annonce_photo_principale2']);
$photo_3 = mysql_escape_string($_POST['annonce_photo_principale3']);



// image 1
$file = $_FILES['annonce_photo_principale1'];
$file_name = $file['name'];
$file_type = $file['type'];
$file_size = $file['size'];
$file_path = $file['tmp_name'];




if($file_name!="" &&  ($file_type="image/jpeg"||$file_type="image/png"||$file_type="image/gif") && $file_size<=2000000)
if(move_uploaded_file ($file_path, 'photo_principale/' .$file_name))


    // image 2

$file2 = $_FILES['annonce_photo_principale2'];
$file_name2 = $file2['name'];
$file_type2 = $file2['type'];
$file_size2 = $file2['size'];
$file_path2 = $file2['tmp_name'];

    if($file_name2!="" &&  ($file_type2="image/jpeg"||$file_type2="image/png"||$file_type2="image/gif") &&  $file_size2<=2000000)
if(move_uploaded_file ($file_path2, 'photo_principale/' .$file_name2))


    // image 3

$file3 = $_FILES['annonce_photo_principale3'];
$file_name3 = $file3['name'];
$file_type3 = $file3['type'];
$file_size3 = $file3['size'];
$file_path3 = $file3['tmp_name'];

    if($file_name3!="" && ($file_type3="image/jpeg"||$file_type3="image/png"||$file_type3="image/gif") && $file_size3<=2000000)
if(move_uploaded_file ($file_path3, 'photo_principale/' .$file_name3))


// selection pour insere dan la table de port louis


    for ($i=0; $i<sizeof($checkbox_telephone);$i++ ){

    $district_portLouis = $_POST['port_louis'];   

    if ($district_portLouis['']){          

    $query = mysql_query("INSERT INTO `port_louis`( ville, categorie,   radio_particulier_professionelle, radio_offres_demande, nom, email, telephone, checkbox_telephone, titre_annonce, text_annonce, prix) VALUES ('$ville', '$categorie', ' $radio_particulier_professionelle', '$radio_offres_demande', '$nom', '$email', '$telephone', '$checkbox_telephone' , '$titre_annonce', '$text_annonce', '$prix')"); 

        $query = mysql_query("UPDATE `port_louis` set district='$district_portLouis' WHERE `district`='$district'");


$query = mysql_query("UPDATE `port_louis` set photo_principale='photo_principale/$file_name' WHERE `photo_principale`='$photo_principale1'");



    if($query == true){
        $query = mysql_query("UPDATE `port_louis` set photo_2='photo_principale/$file_name2' WHERE `photo_2`='$photo_2'");
    }

    if($query == true){
        $query = mysql_query("UPDATE `port_louis` set photo_3='photo_principale/$file_name3' WHERE `photo_3`='$photo_3'");
    }



} // checkbox telephone ends


} // if district port louis == port_louis ENDS

    else {

                $district_blackRiver = $_POST['black_river']; 

    if ($district_blackRiver['']){         

    $query = mysql_query("INSERT INTO `black_river`( ville, categorie, radio_particulier_professionelle, radio_offres_demande, nom, email, telephone, checkbox_telephone, titre_annonce, text_annonce, prix) VALUES ('$ville', '$categorie', ' $radio_particulier_professionelle', '$radio_offres_demande', '$nom', '$email', '$telephone', '$checkbox_telephone' , '$titre_annonce', '$text_annonce', '$prix')");  

        $query = mysql_query("UPDATE `black_river` set district='$district_blackRiver' WHERE `district`='$district'");


$query = mysql_query("UPDATE `black_river` set photo_principale='photo_principale/$file_name' WHERE `photo_principale`='$photo_principale1'");



    if($query == true){
        $query = mysql_query("UPDATE `black_river` set photo_2='photo_principale/$file_name2' WHERE `photo_2`='$photo_2'");
    }

    if($query == true){
        $query = mysql_query("UPDATE `black_river` set photo_3='photo_principale/$file_name3' WHERE `photo_3`='$photo_3'");
    }


    }

    }


}



    $result = mysql_query("SELECT * FROM `port_louis` WHERE `photo_principale`=''") or die(mysql_error());
while($row = mysql_fetch_array($result))




mysql_close($connect);

?>



This post has been edited by VB ASRAAF007: 30 January 2014 - 12:53 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Select Option to different tables in database

#2 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 387
  • View blog
  • Posts: 1,408
  • Joined: 15-January 14

Re: Select Option to different tables in database

Posted 30 January 2014 - 10:07 AM

Why are you turning off error messages? Don't you want to fix the errors instead of hiding them? What about this:

    $district_portLouis = $_POST['port_louis'];   

    if ($district_portLouis['']){

What are you trying to check with that if statement? Is $_POST['port_louis'] an array?

If you're trying to select a table dynamically then have one form element where you choose the table. Look up that item in $_POST, and then you can use that variable in the query as the table name instead of hard-coding the table. You should validate the table name against a whitelist though so that you're only working with tables that you expect.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1