0

it is a error in the VALUES in the php. It says it is a undefined index in every single value. I have tried for a long time now, but i can't figure out what the problem is. is it in my database or inside the code? Please help me :)

<html>
<body>
<h1>A small example page to insert some data in to the MySQL database using PHP</h1>
<form action="insert.php" method="post">
Firstname: <input type="text" name="navn" /><br><br>
Lastname: <input type="text" name="adresse" /><br><br>

<input type="submit" />
</form>

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("forum", $con);

$sql="INSERT INTO bruker (navn, adresse)
VALUES
('$_POST[navn]',
'$_POST[adresse]')";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";

mysql_close($con)
?>

</body>
</html>
4
  • 1
    1) Stop using the deprecated mysql_ functions and switch to MySQLi or PDO instead 2) Don't blindly insert values into your MySQL query. Your code is vulnerable to SQL injection 3) You can use isset() to check if the value is actually defined in your $_POST array. print_r($_POST); will list the entire contents. Commented Dec 13, 2013 at 11:04
  • You should not use MySql as it is deprecated. use PDO or MySqli instead! Commented Dec 13, 2013 at 11:05
  • You're tying to get the index of an array in the wrong way. $_POST[navn] it's in fact $_POST['navn'] and $_POST['adresse']. But you've got bigger issues than that: you're blindly inserting values in the database. That's a big no-no. You should look into prepared statements and use mysqli instead. Commented Dec 13, 2013 at 11:06
  • are you writing html and php in a single file? Commented Dec 13, 2013 at 11:14

2 Answers 2

1

your post value will work once submit button is clicked

<form action="" method="post">

<input type="submit" name="submit" />

<?php
if(isset($_POST['submit']))
{
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("forum", $con);

$sql="INSERT INTO bruker (navn, adresse)
VALUES
('$_POST[navn]',
'$_POST[adresse]')";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";

mysql_close($con)
}
?>

you can also hide your warning by using

error_reporting(0);

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

3 Comments

-1 for "your post value will work once submit button is clicked".the OP is already using method =post so whn you will click on submit the data will transferred to php file.giving name to submit is a good practice but it doesnot effect anything when there is only one submit button.
The error is gone, but the data don not insert into my database
@Rishabh Raj !!!Its working for him now ,what you have written it will work but it will show warning.And yes thanks for -1
1

Try not to use old mysql functions. Switch to mysqli or PDO instead. I've updated the code for BASIC security, but prepared statements give much more security.

Try this:

<?php
$con = mysqli_connect("localhost","root","pw","dbname");
if (mysqli_connect_errno($con))
{
die('Could not connect: ' . mysqli_connect_error());
}

$navn = mysqli_real_escape_string($con,$_POST['navn']);
$adresse = mysqli_real_escape_string($con,$_POST['adresse']);

$sql="INSERT INTO bruker (navn, adresse)
VALUES
('$navn',
'$adresse')";

if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";

mysqli_close($con);
?>

EDIT: And you're accessing the index of your arrays ($_POST[navn] and $_POST[adresse]) at the wrong way. You have to put the index-names in single quotes like $_POST['navn'].

3 Comments

how it solves op's problem?also other than security reasons how is your code different from op.
Well he access $_POST[navn] which is not the right way to access the index. He is missing the '' around the index-name.
you should write that to inform others what you are doing :) +1

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.