0

I am having a problem with some code I wrote I hope the title makes sense. I am trying to insert peoples names and age into a table I made. I have been over my code lots of times and can not find out what is wrong with it. Is there something wrong with the code I wrote or is it something else? The connection to the server is not the problem. Thanks for reading

    <html>
 <head>
 </head>
 <body>
<form action="insertforrm.php" method="post">
    First Name:<input type="text" name= "first"><br/>
    Last Name: <input type ="text" name= "last"><br/>
    Age <input type= "text" name="age"><br/>
    <input type="submit" name="submit">

</form>
 <?php
 if (isset($_POST['submit'])) {

        $con = mysql_connect("localhost" , "root" , "root");
        if (!$con) {
            die("cant covnnect:" . mysql_error());

        }
        mysql_select_db("names",$con);

        $sql = "INSERT INTO jack (First,Last,Age) VALUES ('$_POST[first]','$_POST[last]','$_POST[age]')";
mysql_query ($sql,$con);

 mysql_close ($con);
}
?>
</body>
</html>
  • 1
    Add error reporting to the top of your file(s) `error_reporting(E_ALL); ini_set('display_errors', 1);` since you're not already checking for errors. – Funk Forty Niner Jun 23 '14 at 02:05
  • 4
    Your present code is open to [**SQL injection**](http://stackoverflow.com/q/60174/). Use [**`mysqli_*` with prepared statements**](http://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php), or [**PDO**](http://php.net/pdo) with [**prepared statements**](http://php.net/pdo.prepared-statements). – Funk Forty Niner Jun 23 '14 at 02:05
  • 2
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](http://j.mp/XqV7Lp). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. – Kermit Jun 23 '14 at 02:05
  • 1
    This is the time to upgrade to **prepared statements**... Before it is tooooo late!!! – ErickBest Jun 23 '14 at 02:26

3 Answers3

3

You can also try:

$sql = "INSERT INTO jack (First,Last,Age) VALUES ('".$_POST['first']."','".$_POST['last']."','".$_POST['age']."')";

Notice the '". , the dots(.) are for string concatenation in PHP.

One more thing: the $_POST is an associative array. So, make sure the key passed is in a string format.

DO: $_POST['first'] and NOT $_POST[first]... Notice the single quotes surrounding the 'first'

ErickBest
  • 4,586
  • 5
  • 31
  • 43
0

Besides the fact that you shouldn't be using mysql_* functions and are leaving yourself crazy open to unintended errors and sql injection, you're not using string interpolation properly. Try this:

$sql = "INSERT INTO jack (First,Last,Age) VALUES ('{$_POST['first']}','{$_POST['last']}','{$_POST['age']}')";

Then, once you get that working, trash this code and use prepared statements to keep yourself safe. They're a little extra work in the beginning, but they handle a lot of debugging that you won't have to do later.

FrankieTheKneeMan
  • 6,645
  • 2
  • 26
  • 37
  • Thanks for telling me that I am new to all this so I will look into that and make sure I dont make that mistake again thanks man :) – user3765841 Jun 23 '14 at 02:26
0

your syntax is wrong try to pass the value of $_POST here is the code..

$fname=$_POST['first'];
$last =$_POST['last'];
$age = $_POST['age'];

$sql = "INSERT INTO jack (First,Last,Age) VALUES ('$fname','$last','$age')";
mysql_query ($sql,$con);

mysql_close ($con);

if your a beginner in php or in programming you should know how to make your code clean..it's a good progamming practice to pass the value of $_POST in local variable...

Krisztián Balla
  • 19,223
  • 13
  • 68
  • 84
CodeSlayer
  • 1,318
  • 1
  • 12
  • 34