Page 1 of 1

insertion problem

Posted: Mon Jan 03, 2011 5:59 am
by madu
hi friends,,,,'
in the following code only insert the empty data,,,,,,wai shall i do,,,,


<html>
<body>
<?php
$fn=$_POST['fname'];
$ln=$_POST['lname'];
$pwd=$_POST['pwd'];
$mn=$_POST['mnum'];
$hn=$_POST['hnum'];
$em=$_POST['email'];
$con=mysql_connect("localhost","root","root");
if(!$con)
{
echo "problem in connection".mysql_errror();
}
mysql_select_db("my_db",$con);

mysql_query("insert into mob(fname,lname,pwd,mnum,hnum,eid)values('".$fn."','".$ln."','".$pwd."','".$mn."','".$hn."','".$em."')") or die("Error in insertion");

echo '<script language="javascript">';

echo 'alert("Data addded successfully")';

echo '</script>';

?>
Do you want to Ad another data:<a href="ninstfor.php">yes</a>
</body>
</html>

Re: insertion problem

Posted: Mon Jan 03, 2011 9:06 am
by Neilos
I am assuming that you only want to add data that has been set from the form. A way to do this would be to use isset() and a foreach loop.

Code: Select all

<?php

$data = array();

if (isset($_POST['fname'])) {
    $index = count($data);
    $data[$index] = $_POST['fname'];
    $variable[$index] = '$fn';
}

if (isset($_POST['lname'])) {
    $index = count($data);
    $data[$index] = $_POST['lname'];
    $variable[$index] = '$ln';
}

if (isset($_POST['pwd'])) {
    $index = count($data);
    $data[$index] = $_POST['pwd'];
    $variable[$index] = '$pwd';
}

// and so on and so on

if (count($data) > 0) {
    $i = 0;
    foreach ($data as $value) {
        $i++;
        if ($i = 0) {
            $values = $value;
        } elseif ($i != 0) {
            $values = $values . "," . $value;
        }
    }
    $j = 0;
    foreach ($variable as $value) {
        $j++;
        if ($i = 0) {
            $variables = $value;
        } elseif ($i != 0) {
            $variables = $variables . "," . $value;
        }
    }
    // now make the query
    $query = '("insert into mob( ' . $values . ')values(' . $variables . ')"';
    mysql_query($query);

    echo '<script language="javascript">';
    echo 'alert("Data addded successfully")';
    echo '</script>';

} else {
    // no data added
}

?>
You might need to play with it a bit as the strings may not be formatted properly but I have to go out now lol so you're on your own with that for a bit.

There is probably a better way to do it but this is what I can come up with off the top of my head.

Re: insertion problem

Posted: Mon Jan 03, 2011 10:08 am
by social_experiment
If you aren't receiving an error the query must be ok, what does your HTML form look like?

Re: insertion problem

Posted: Mon Jan 03, 2011 10:19 am
by Neilos
hmm yeah, i just got back and looked at this again. My code won't necessarily work in its current form. However I don't think that I answerd the question for the problem you are having.

What exactly is your problem as I don't really understand what you are trying to do.