0

I am using form to insert data into SQL.

This is my form:

<form action="insert.php" method="POST">
    Name:<input type="text" name="firstname[]" />
</form>

and this is my PHP code:

<?php

$con = mysql_connect("localhost", "root", "root");
if (!$con) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
while ($array = $result->fetch_assoc()) {
    $query = "INSERT INTO test(firstname) VALUES ('{$array['firstname']}')";
    $result = $database->query($query);
}
mysql_close($con);

?>

It shows an error. Any help?

3
  • 2
    Wow. Lots of confusion there. Perhaps start with the error message you receive, and work your way back from there. Or follow a tutorial. Perhaps consider first how $result is defined before one enters your while loop... Commented Dec 1, 2012 at 11:18
  • Please don't build yet another web app using w3schools techniques. It's ten years out of date. Try a framework like CakePHP or CodeIgnighter to take care of routine stuff like this for you. It's not even hard. If you're just getting started you're going to make too many mistakes trying to go it alone. Commented Dec 1, 2012 at 11:18
  • you are right, am just started php. But can you tell me how to insert array data using form in php ? So i can get my mistake.. Commented Dec 1, 2012 at 11:23

1 Answer 1

1

To insert POST data in a table, you don't have to first fetch it from database (* this would not exist). So remove some extra code:

<?php

$con = mysql_connect("localhost","root","root");

if (!$con) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);

$first_name = mysql_real_escape_string($_POST["firstname"]);

$query = "INSERT INTO test (firstname) VALUES ('$first_name')";
$result = mysql_query($query);
mysql_close($con);

?>

And update your HTML so that the field is named firstname and not firstname[].

Edit: mysql_* functions are not really recommended to be used anymore. But I had to answer within your question's requirements.

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

7 Comments

+1 for really answering the question as stated in the answer ("had to answer within your question's requirements").
can you tell me, array store data into a single row in database ?
for example? you mean if your table has more than one fields?
if i have 3 names along with 3 city, will they store into single row if i use array ? ie. name and city are column and have 3 row data..
You can create a table with those 3 fields. Then you can add equal number of fields to your html form. for example last_name and city. When you call "INSERT" then you can provide those values and all of them will be stored in 1 row per insert
|

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.