How to insert values present in variable into mysql table?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Amit Girish Deshpande
    New Member
    • Sep 2010
    • 2

    #1

    How to insert values present in variable into mysql table?

    Hi all,

    I have created a small Bank form GUI in gtk.
    I want to store the information in the database.
    Values(string) are present in variable say entry1.
    e.g. entry1[]="Amit"
    how can i insert string present in entry1 into table.

    I have tried different syntax's like

    insert into table01 values('entry1' );
    insert into table01 values('"entry1 "');
    insert into table01 values('$entry1 ');

    it didnt worked...

    please help me to sort this problem..

    Thanks...
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    If we are to be any help at all, we need to know a few basic things. Like:
    Which programming languages are you using?
    What does you code look like? (Exactly!)
    How do you expect it to work?
    How does it actually work?
    Do you get any errors? (This is crucial!)


    "It doesn't work" is not a very helpful problem description ;)

    I have tried different syntax's like

    insert into table01 values('entry1' );
    insert into table01 values('"entry1 "');
    insert into table01 values('$entry1 ');
    The first one is correct. The second one is incorrect, unless you plan on including the double-quotes in the string, in which case it should be fine. The third one looks like something you would do when injecting variables into strings in PHP; a fairly common sight in PHP code.

    In PHP we would create MySQL commands somewhat like this:
    [code=php]<?php
    // Some initial values, most likely comming from a database
    // or $_POST/$_GET.
    $values = array();
    $values[] = 'Something';
    $values[] = 'Something else';

    // Make them safe to be put into a MySQL query.
    // ALWAYS do this before putting things into a query,
    // or you risk SQL Injection.
    $first = mysql_real_esca pe_string($valu es[0]);
    $second = mysql_real_esca pe_string($valu es[1]);

    // Construct a query.
    $sql = "INSERT INTO myTable(col1, col2)
    VALUES('$first' , '$second')";

    // Etc...
    ?>[/code]

    Comment

    Working...