Adding value into mysql database

Posted on 2012-08-17
Last Modified: 2012-08-17
Hello Experts,

I am trying to add the value from the select element into mysql database table like below...

<td align="center"><form method="POST"><select name="ss1" type="list">
		<option value="0">0</option>
		<option value="1">1</option>
		<option value="2">2</option>
		<option value="3">3</option>
		</select><input type="submit"/></form></td>
		$query=mysql_query("SELECT test FROM test_1 WHERE id='1' AND testid='2'");
	"The above query returns returns the resource id #12. "
	$row = mysql_fetch_row($query);
		$query1 = mysql_query("INSERT INTO '$row' VALUES ('$name')");

Open in new window

The value were not get inserted into the table in the column "test".
Can you suggest what's gone wrong in the above code or is there any better way to do this?

Thanks in Advance!
Question by:ShaileshShinde
    LVL 11

    Expert Comment


    Your mysql Syntax is wrong. You need to tell in what table it has to store. Do you want to insert a new row or update it?

    To insert a new value you would do
    $query1 = mysql_query("INSERT INTO test_1 SET test = '{$name}'");

    Open in new window


    PS: You should mysql_real_escape_string your vairables that are beeing passed by POST or GET to prevent people of misusing it


    Open in new window



    Open in new window

    LVL 50

    Expert Comment

    by:Julian Hansen
    The standard syntax for an INSERT statement is

    INSERT INTO table_name (`field1`,`field2`, ...) VALUES ('value1','value2', ...);

    Open in new window

    Note: it is recemmonded you use backtics [`] to enclose your fieldnames to avoid having the query fail because a fieldname happens to be a reserved word.
    String values should be enclosed in single quotes;

    Any string value you send to the database should be "cleaned" with mysql_real_escape_string.

    INSERT INTO table_name (`field1`,`field2`, ...) 
      VALUES (

    Open in new window

    Integer values do not need quotes but will work with or without them. Date values need to be submitted as strings usually in the form yyyy-mm-dd.
    LVL 1

    Author Comment

    Hello Expert,
    I would like to insert the value into the table "test_1" in column "test". the table were already been created with data entered for other columns. this particular column rows were empty in which i would like to insert the values in the row where id='1' and testid='2'.
    table looks like below...
    Here test_1 column is blank, needs to add the value where id='1' and testid='2'.

    Open in new window

    LVL 50

    Accepted Solution

    Then you need to use an update statement not an insert.

    Update is for when the row already exists but in order to update the table you need a means to specify the row you want to update.

    the syntax is
    $query = "UPDATE test_1 SET test='$test' WHERE id=1 AND testid=2";

    Open in new window

    This will update the row that has id=1 and testid=2 - change if you need to update a different row.
    LVL 1

    Author Closing Comment

    Thanks Expert.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now