0

I want to insert the data into postgresql database which includes radio button and dropdwn box. I tried 3 different insert queries.

1)

$query = sprintf("INSERT INTO onf VALUES('%s','%s','%s','%s','%s','%s','%d')",$_REQUEST['title'],$_REQUEST['name'],$_REQUEST['district'],$_REQUEST['rurban'],$_REQUEST['taluk'],$_REQUEST['village'],$_REQUEST['wardno']);

2)

$qry="INSERT INTO onf(title, name, district,rurban,taluk,village,wardno) VALUES ('$tile', '$name', '$district','$rurban','$taluk','$village','$wardno')";   

3)

 $qy="INSERT INTO onf(title, name, district,rurban,taluk,village,wardno) VALUES (('$_POST[title]','gen','$_POST[gen]),'$_POST[name]','$_POST[district]','$_POST[rurban]','$_POST[taluk]','$_POST[village]','$_POST[wardno]')";

   $res = pg_query($db,$qy);

My problem is in 1st query oly name alone gets inserted and in 2nd , 3rd no record gets inserted. Y dropdown nd radio button is not inserting into database? Tanx in advance..

3
  • 1
    show full code with form Commented Feb 17, 2014 at 8:25
  • In 3. add line echo pg_last_error($db); and let me know what u get Commented Feb 17, 2014 at 8:40
  • $qry="INSERT INTO onf(title, name, district,rurban,taluk,village,wardno) VALUES ('$_POST[title]','$_POST[name]','$_POST[district]','$_POST[rurban]','$_POST[taluk]','$_POST[village]','$_POST[wardno]')"; Using dis query i inserted the data but only name field is inserted al other fields are inserted as empty. how to insert drop down fields like district, taluk, village to database Commented Feb 17, 2014 at 10:51

1 Answer 1

1

May just be because neither of those 3 is proper PHP code.

Query 1: Don't use $_REQUEST. Use $_POST.

Query 2: Unless you have register_globals turned on, which I SERIOUSLY hope you don't, your vars ($tile, $name, etc.) will not contain the information submitted by the form. Again, you want to use $_POST.

Query 3: Your variables aren't properly escaped. Generally, I recommend not embedding variables into double-quoted strings if you don't quite have a grasp on string concatenation in PHP. Use single-quoted strings and build them by breaking the string and concatenating with ..

Now, I feel like your second query is the closest to what you want, so try this:

$qry = '
    INSERT INTO onf (
        title,
        name,
        district,
        rurban,
        taluk,
        village,
        wardno
    ) VALUES (
        "'.$_POST['tile'].'",
        "'.$_POST['name'].'",
        "'.$_POST['district'].'",
        "'.$_POST['rurban'].'",
        "'.$_POST['taluk'].'",
        "'.$_POST['village'].'",
        "'.$_POST['wardno'].'"
    );
';
Sign up to request clarification or add additional context in comments.

2 Comments

$qry="INSERT INTO onf(title, name, district,rurban,taluk,village,wardno) VALUES ('$_POST[title]','$_POST[name]','$_POST[district]','$_POST[rurban]','$_POST[taluk]','$_POST[village]','$_POST[wardno]')"; Using dis query i inserted the data but only name field is inserted al other fields are inserted as empty. how to insert drop down fields like district, taluk, village to database
Define them as such: <select name="booboo"><option value="1">one</option><option value="2">two</option> ... </select> and $_POST['booboo'] will contain your value.

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.