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>
insertion problem
Moderator: General Moderators
Re: insertion problem
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.
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.
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
}
?>
There is probably a better way to do it but this is what I can come up with off the top of my head.
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: insertion problem
If you aren't receiving an error the query must be ok, what does your HTML form look like?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Re: insertion problem
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.
What exactly is your problem as I don't really understand what you are trying to do.