1

Hi I am trying to get a form to post to a database i can connect and the database and table are set up. but rather than post the contents of the fields in it posts the text firstname and secondname in to the columns.

below is my code:

mysql_select_db("company", $conn);

    $sqlCmd = sprintf("INSERT INTO names (firstname, secondname) VALUES ('%firstname','%secondname')", 
        mysql_real_escape_string($_POST["firstname"]),
        mysql_real_escape_string($_POST["secondname"]));

    //echo $sqlCmd;
    //die();    

    mysql_query($sqlCmd);

    mysql_close($conn);
}

?>



    <form method="post">
<input type="text" id="firstname" name="firstname"/>
<input type="text" id="secondname" name="secondname"/>
    <input name="submit" type="submit" value="Submit"/>
</form>

I need it to post the values from the fields. i am new to php and this is my first project, i would love some help.

just to add this is what i have managed after following a tutorial.

Thanks

Ryan

4
  • 1
    you forgot to mysql_connect Commented Jul 12, 2013 at 14:44
  • In the sptintf function replace %firstname and %secondanem with %s Commented Jul 12, 2013 at 14:45
  • I have already got this and it works Commented Jul 12, 2013 at 14:46
  • changing it also did not work *it now does work it kept changing it when i published it. thanks Commented Jul 12, 2013 at 14:48

2 Answers 2

1

Do not use php's mysql_ methods any more.

It is outdated: https://wiki.php.net/rfc/mysql_deprecation

Use mysqli_ or pdo instead

In your code you forgot the mysql_connect() anyways ;)

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

Comments

0

Just change your sprintf call to:

$sqlCmd = sprintf("INSERT INTO names (firstname, secondname) VALUES ('%s','%s')", 
    mysql_real_escape_string($_POST["firstname"]),
    mysql_real_escape_string($_POST["secondname"]));

Also consider using the newer mysqli function: http://www.php.net/manual/en/intro.mysqli.php

HTH; Pacific

Comments

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.