Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

php retrieve data from mysql + display in form's table + select and submit one record with button submit + insert record into new mysql table

Posted on 2009-04-26
30
Medium Priority
?
2,307 Views
Last Modified: 2013-12-12
I have a php file form1.php all coded.  I connect to MySql DB, retrieve data from a table tblMyData with columns 'starttime', 'location', 'name', and 'description', and display them in the form using echo. Also I have added submit button to each record. When I click submit button, I would like to insert this record into new table "tblMyDataCompleted" using php file "insertRecordFromForm1". The problem is this php file doesn't see php variables and doesn't insert a record.
Second question: I have gotten error when I try to insert start time.
 Next question:
  If I use while loop to get all records from a table tblMyData, display all records in the form with submit button,  then I pick one record and push submit button, how can I associate  each button with each record?
  I appreciate any help! Thank you!
  Below form1.php and insertRecordFromForm1.php
// form1.php
<HTML>
<BODY style="COLOR: black; BACKGROUND-COLOR: powderblue"><script type="text/javascript">
function displaymessage()
{
alert("Mail Receipt Processed!");
}
</script>
  <FORM id=form1 name=WorkInfo  action="INSERTRecordFromForm1.php"  method="post" >
<TR><TD><input type="hidden"  id="startdate"  name="startdate"></TD>
<TD><input type="hidden"   id="Location"  name="Location"></TD>
<TD><input type="hidden"   id="name"  name="name"></TD>
<TD><input type="hidden"   id="description "    name="description "></TD></TR>
<input type="submit"  name ="submit"  value = " FINISHED";onclick="INSERT_04_26_09.php"  >
<?php
error_reporting(0);
mysql_connect("localhost","root",'tiger');
@mysql_select_db("WorkLog") or die( "Unable to select database");
$query="SELECT * FROM tblMyData";
$result=mysql_query($query);
//$Row = mysql_fetch_row($result);
$Row = mysql_fetch_array($result);
$num=mysql_numrows($result);
mysql_close();
echo "<b><center>Database Output</center></b><br><br>";
echo "<table class = 't1'  id ='t1' width='100%' border='1'>";
echo "<tr>
            <th><B>startdate</b></th>
            <th><B>Location</b></th>
            <th><B>name</b></th>
            <th><B>description</b></th BGCOLOR='#ff00ff'>
            
        </tr>";
$startdate=$Row[1];
$Location=$Row[2]; 
$name=$Row[3]; 
$description=$Row[4];
    echo "<tr><td style='BACKGROUND-COLOR:#00FFFF'>{$Schedule}</td></td>";
	echo "<td style='BACKGROUND-COLOR:#00FFFF'>{$Center}></td></td>";
                echo "<td style='BACKGROUND-COLOR:#00FFFF'>{$Assigned_To}</td></td>";
	echo "<td style='BACKGROUND-COLOR:#00FFFF'>{$Task}></td></td></tr>";
?><FONT face=Arial> </FONT>
</FORM>
</BODY>
</HTML>
 
// insertRecordFromForm1.php
<?php
error_reporting(0);
$con = mysql_connect("localhost","root", "tiger");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
@mysql_select_db("WorkLog", $con);
 
$sql = "Insert into tblDataCompleted  set startdate ='".$_POST['startdate']."',
        	    Location = '".$_POST['Location']."', 
		    name = '".$_POST['name'] ."', description ='".$_POST['description']."' ";
mysql_query($sql) or die (mysql_error());
 
if (!mysql_query($sql,$con))
{
  die('Error: ' . mysql_error());
  }
echo "record added";
$sql="COMMIT";
mysql_close($con);
?>

Open in new window

0
Comment
Question by:myfm09
  • 15
  • 13
  • 2
30 Comments
 
LVL 4

Expert Comment

by:Adam Chan
ID: 24240192
Hello myfm09,

First thing I noticed is that in form1.php, you do not need a "onclick" parameter, because being a submit type button, it will automatically perform the form's action when clicked. Iin this case, that would be

action="INSERTRecordFromForm1.php"

Secondly, there are no values in your form to post!

To pass variables via the submit button, you need to do something like the following

<form action="process_input.php">
<input type="hidden" name="input1" id="input1" value="Hello world!">
<input type="submit" value="Submit">
</form>

And then in process_input.php, you can do the following:

<?php
echo $_POST["input1"];
?>

And you will see "Hello world!" (w/o quotes) on the page.
The key here is the "value" parameter, that's what get posted to the action page.



>> Second question: I have gotten error when I try to insert start time.
The correct syntax for an INSERT statement is

INSERT INTO table_name (column_name, ...) VALUES (value, ...);

see attached code snippet for your case.



As for the button-record association, do you have one submit button next to each record, or just a single submit button for all of them?

In the former case, you can create a separate form for each record within the loop with a submit button and associated action in it, or you can use a onclick javascript on the submit button which gets the record to be submitted; as for the latter case, you would implement a checkbox or a radio button for each record and check for the selected one when you click on submit.

Attached code snippet is what I think you're trying to do, hope it helps.

// form1.php
<HTML>
<BODY style="COLOR: black; BACKGROUND-COLOR: powderblue">
<script type="text/javascript">
function displaymessage()
{
alert("Mail Receipt Processed!");
}
</script>
 
<FORM id="form1" name="WorkInfo" action="INSERTRecordFromForm1.php"  method="post" >
<table>
    <TR>
        <TD>startdate</TD>
        <TD>Location</TD>
        <TD>name</TD>
        <TD>description</TD>
    </TR>
    <?php
		error_reporting(0);
		mysql_connect("localhost","root",'tiger');
		@mysql_select_db("WorkLog") or die( "Unable to select database");
		$query="SELECT * FROM tblMyData";
		$result=mysql_query($query);
		$Row = mysql_fetch_array($result);
		//$num=mysql_numrows($result);
		mysql_close();
		
		while($Row) {
			echo "<tr>";
			echo "<td>$row['startdate']</td>";
			echo "<td>$row['Location']</td>";
			echo "<td>$row['name']</td>";
			echo "<td>$row['description']</td>";
			echo "</tr>";
		}
	?>
</table>
</FORM>
</BODY>
</HTML>
 
 
 
// insertRecordFromForm1.php
<?php
error_reporting(0);
$con = mysql_connect("localhost","root", "tiger");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
@mysql_select_db("WorkLog", $con);
 
$sql = "INSERT INTO tblDataCompleted (startdate, Location, name, description) VALUES ('".$_POST['startdate']."', '".$_POST['Location']."', '".$_POST['name']."', '".$_POST['description']."')";
 
$result = mysql_query($sql) or die (mysql_error());
 
if (!$result)
  {
  die('Error: ' . mysql_error());
  }
else 
  echo "record added";
$sql="COMMIT";
mysql_close($con);
?>

Open in new window

0
 

Author Comment

by:myfm09
ID: 24291278
Hello addamez,
When I run a form1, I got Error message:
Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in C:\Program Files\EasyPHP 3.0\www\050309\form1_0503\form1_adam.php on line 31

When I run a form "Hello World"(your example), I got a message
Notice: Undefined index: input1 in C:\Program Files\EasyPHP 3.0\www\050309\form1_0503\process_input.php on line 2
I
Thanks



0
 
LVL 4

Expert Comment

by:Adam Chan
ID: 24292072
Hi myfm09,

Can you please copy the respective lines from the files (and several lines before/after)? because the line numbers are probably different from the snippet I posted.
0
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
LVL 20

Expert Comment

by:NerdsOfTech
ID: 24302029
I went ahead and made it so that each record found has a checkbx by it. Foreach record that is selected it will be posted in processing for array
// form1.php
<HTML>
<BODY style="COLOR: black; BACKGROUND-COLOR: powderblue"><script type="text/javascript">
function displaymessage()
{
 alert("Mail Receipt Processed!");
}
</script>
 
<FORM id="form1" name="WorkInfo"  action="INSERTRecordFromForm1.php"  method="post">
 
<?php
error_reporting(0);
mysql_connect("localhost","root",'tiger');
@mysql_select_db("WorkLog") or die( "Unable to select database");
$query="SELECT * FROM tblMyData";
$result=mysql_query($query);
//$Row = mysql_fetch_row($result);
?>
 
<FONT face="arial">
<?php
echo "<b><center>Database Output</center></b><br><br>";
echo "<table class = 't1'  id ='t1' width='100%' border='1'>";
echo "<tr>
       <th width="10"><b>+</b></td>
       <th><B>Startdate</b></th>
       <th><B>Location</b></th>
       <th><B>Name</b></th>
       <th><B>Description</b></th>
      </tr>";
$i=0;
While($Row = mysql_fetch_array($result)){
 $startdate=$Row[1];
 $location=$Row[2]; 
 $name=$Row[3]; 
 $description=$Row[4];
 echo "<tr>";
 echo "<td style='BACKGROUND-COLOR:#00FFFF'><input type=\"checkbox\" name=\"index[$i]\"></td>";
 echo "<td style='BACKGROUND-COLOR:#00FFFF'>{$Schedule}</td>";
 echo "<td style='BACKGROUND-COLOR:#00FFFF'>{$Center}></td>";
 echo "<td style='BACKGROUND-COLOR:#00FFFF'>{$Assigned_To}</td>";
 echo "<td style='BACKGROUND-COLOR:#00FFFF'>{$Task}>\n";
 echo "<input type\"hidden\" name=\"startdate[$i]\" value=\"$startdate\" />\n";
 echo "<input type\"hidden\" name=\"location[$i]\" value=\"$location\" />\n";
 echo "<input type\"hidden\" name=\"name[$i]\" value=\"$name\" />\n";
 echo "<input type\"hidden\" name=\"description[$i]\" value=\"$location\" />\n";
 echo "</tr>";
 $i++;
}
mysql_close();
 ?>
</FONT>
<input type="submit" name="submit" value="FINISHED"> 
</FORM>
</BODY>
</HTML>

Open in new window

0
 
LVL 20

Expert Comment

by:NerdsOfTech
ID: 24302030

// insertRecordFromForm1.php
<?php
 
error_reporting(0);
$con = mysql_connect("localhost","root", "tiger");
if (!$con)
{
 die('Could not connect: ' . mysql_error());
}
@mysql_select_db("WorkLog", $con);
 
if (isset($_POST['startdate']){
 foreach($_POST[''] as $key => $value){
  $sql = "Insert into tblDataCompleted  set startdate ='".$_POST['startdate'][$key]."',
         Location = '".$_POST['location'][$key]."', 
         name = '".$_POST['name'][$] ."', description ='".$_POST['description'][$key]."' ";
  mysql_query($sql) or die (mysql_error());
 
  if (!mysql_query($sql,$con))
  {
   die('Error: ' . mysql_error());
  }
  echo "Record Added <br />";
  mysql_close($con);
 }
}
?>

Open in new window

0
 

Author Comment

by:myfm09
ID: 24308629
Hello NerdsOfTech,
 I am using EasyPHP with Apache Server and MySQL.
I had to change a few changes in my files( changed single quotes to double and etc.
Now I can run a form, but when I clicked a button "Insert", no any affect to db.
Attached is application1.zip with database files.
Thank you
0
 

Author Comment

by:myfm09
ID: 24308748
Hello NerdsOfTech,
I am sorry, but I have one more question:
I would like to delete the same records I picked using checkbox from mytblMyData.
So I need to connection and two actions.
Thanks
0
 

Author Comment

by:myfm09
ID: 24308888
Hello addamez,
Attached is a file "HelloWorld.zip" with errors.
Allso Attached is application.zip with new version. I was not able to isert records.
Also I would like to delete from mytblMyDatat he same records I picked using checkboxes .

You need to change file ext. from .txt to .php
Thanks
HelloForm.zip
application.zip
0
 

Author Comment

by:myfm09
ID: 24308925
Hello NerdsOfTech,
I am sorry, but first time .zip file was not accepted , so now I have changed .php ext. to txt.
and finally uploaded application.zip
Attached is application.zip
application.zip
0
 
LVL 20

Expert Comment

by:NerdsOfTech
ID: 24352687
I fixed your hello world example
<html>
 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Test</title>
</head>
 
<body>
<form action="process_input.php" method="POST">
<input type="hidden" name="input1" id="input1" value="Hello world!">
<input type="submit" value="Submit">
</form>
</body>
 
</html>

Open in new window

0
 
LVL 20

Expert Comment

by:NerdsOfTech
ID: 24352703
Form1 added delete
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Form1</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
 
<BODY style="COLOR: black; BACKGROUND-COLOR: powderblue"><script type="text/javascript">
function displaymessage()
{
 alert("Mail Receipt Processed!");
}
</script>
 
<FORM id="form1" name="WorkInfo"  action="insertRecordFromForm1.php"  method="post">
 
<?php
error_reporting(0);
mysql_connect("localhost","root",'tiger');
@mysql_select_db("worklog") or die( "Unable to select database");
$query="SELECT * FROM tblMyData";
$result=mysql_query($query);
//$Row = mysql_fetch_row($result);
?>
 
<FONT face="arial">
<?php
echo "<b><center>Database Output</center></b><br><br>";
echo "<table class = 't1'  id ='t1' width='100%' border='1'>";
echo "<tr>
       <th width='10'><b></b></td>
       <th><B>Startdate</b></th>
       <th><B>Location</b></th>
       <th><B>Name</b></th>
       <th><B>Description</b></th>
      </tr>";
$i=0;
while($Row = mysql_fetch_array($result)){
 $startdate=$Row[0];
 $location=$Row[1]; 
 $name=$Row[2]; 
 $description=$Row[3];
 echo "<tr>";
 echo "<td style='BACKGROUND-COLOR:#00FFFF'><input type='checkbox' name='index[$i]'></td>";
 echo "<td style='BACKGROUND-COLOR:#00FFFF'>{$startdate}</td>";
 echo "<td style='BACKGROUND-COLOR:#00FFFF'>{$location}</td>";
 echo "<td style='BACKGROUND-COLOR:#00FFFF'>{$name}</td>";
 echo "<td style='BACKGROUND-COLOR:#00FFFF'>{$description}</td>";
 echo "<input type='hidden' name='startdate[$i]' value='$startdate'>";
 echo "<input type='hidden' name='location[$i]' value='$location'>";
 echo '<input type="hidden" name="name[$i]" value="$name">';
 echo '<input type="hidden" name="description[$i]" value="$description">';
 echo "</tr>";
 $i++;
}
mysql_close();
 ?>
</FONT>
<input type="submit" name="submit" value="Insert">
<input type="submit" name="submit" value="Delete">
</FORM>
</BODY>
</HTML>

Open in new window

0
 
LVL 20

Expert Comment

by:NerdsOfTech
ID: 24352718
Fixed insertRecordFromForm1.php

<?php
 
error_reporting(0);
$con = mysql_connect("localhost","root", "tiger");
if(!$con)
{
 die('Could not connect: ' . mysql_error());
}
@mysql_select_db("worklog", $con);
 
if(isset($_POST['startdate']))
{
 foreach($_POST['startdate'] as $key => $value)
 { 
  $sql = "Insert into tblMyDataCompleted  set startdate ='".$_POST['startdate'][$key]."',
        Location = '".$_POST['location'][$key]."', 
        name = '".$_POST['name'][$key]."',
        description ='".$_POST['description'][$key]."' ";
  mysql_query($sql) or die (mysql_error());
 
  if(!mysql_query($sql,$con))
  {
   die('Error: ' . mysql_error());
   }
  echo "Record Added <br />";
  mysql_close($con);
 }
}
?>

Open in new window

0
 
LVL 20

Expert Comment

by:NerdsOfTech
ID: 24352733
Let me know how that works for you.
0
 

Author Comment

by:myfm09
ID: 24360921
Thank you very much. I will test and let you.
0
 

Author Comment

by:myfm09
ID: 24361326
Hello I have tested your fixed iserted. When I checked first chekbox( first row) and click insert, I got a message:
" Record Added
Access denied for user 'ODBC'@'localhost'(using password:NO)"
Then I selected data from my table, and I got a result(see below a screenshot).
mysql> SELECT *
    -> FROM tblMyDataCompleted;
+-----------+----------+------+-------------+
| startdate | Location | name | description |
+-----------+----------+------+-------------+
| 08:50:00  | Bostom   |      |             |
| 08:50:00  | Bostom   |      |             |
+-----------+----------+------+-------------+
2 rows in set (0.00 sec)
mysql>

Thanks
0
 
LVL 20

Expert Comment

by:NerdsOfTech
ID: 24361818
Cool. Is everything working then?
0
 

Author Comment

by:myfm09
ID: 24364513
Hello, no cool.
I checked one record, but inserted two and only two first columns.
When I checked a few checkboxes, I inserted the same record twice.
please help help
0
 
LVL 20

Expert Comment

by:NerdsOfTech
ID: 24367288
Oops. Ok let me check it out.
0
 
LVL 20

Expert Comment

by:NerdsOfTech
ID: 24367472
Form1 fixed.

The $i variables were inside of string "" in hidden inputs. Now they are fixed.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Form1</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
 
<BODY style="COLOR: black; BACKGROUND-COLOR: powderblue"><script type="text/javascript">
function displaymessage()
{
 alert("Mail Receipt Processed!");
}
</script>
 
<FORM id="form1" name="WorkInfo"  action="insertRecordFromForm1.php"  method="post">
 
<?php
error_reporting(0);
mysql_connect("localhost","root",'tiger');
@mysql_select_db("worklog") or die( "Unable to select database");
$query="SELECT * FROM tblMyData";
$result=mysql_query($query);
//$Row = mysql_fetch_row($result);
?>
 
<FONT face="arial">
<?php
echo "<b><center>Database Output</center></b><br><br>";
echo "<table class = 't1'  id ='t1' width='100%' border='1'>";
echo "<tr>
       <th width='10'><b></b></td>
       <th><B>Startdate</b></th>
       <th><B>Location</b></th>
       <th><B>Name</b></th>
       <th><B>Description</b></th>
      </tr>";
$i=0;
while($Row = mysql_fetch_array($result)){
 $startdate=$Row[0];
 $location=$Row[1]; 
 $name=$Row[2]; 
 $description=$Row[3];
 echo "<tr>";
 echo "<td style='BACKGROUND-COLOR:#00FFFF'><input type='checkbox' name='index[$i]'></td>";
 echo "<td style='BACKGROUND-COLOR:#00FFFF'>{$startdate}</td>";
 echo "<td style='BACKGROUND-COLOR:#00FFFF'>{$location}</td>";
 echo "<td style='BACKGROUND-COLOR:#00FFFF'>{$name}</td>";
 echo "<td style='BACKGROUND-COLOR:#00FFFF'>{$description}</td>";
 echo '<input type="hidden" name="startdate[' . $i . ']" value="' . $startdate .'">';
 echo '<input type="hidden" name="location[' . $i . ']" value="' . $location . '">';
 echo '<input type="hidden" name="name[' . $i . ']" value="' . $name . '">';
 echo '<input type="hidden" name="description[' . $i . ']" value="' . $description . '">';
 echo "</tr>";
 $i++;
}
mysql_close();
 ?>
</FONT>
<input type="submit" name="submit" value="Insert">
<input type="submit" name="submit" value="Delete">
</FORM>
</BODY>
</HTML>

Open in new window

0
 

Author Comment

by:myfm09
ID: 24369973
Doesn't work. Now I inserted all fields, but again first row twice. Even I checked different roes, I got the same first record.
0
 
LVL 20

Expert Comment

by:NerdsOfTech
ID: 24377964
Alright. I check the processing page.

Here is Form1 again:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Form1</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
 
<BODY style="COLOR: black; BACKGROUND-COLOR: powderblue"><script type="text/javascript">
function displaymessage()
{
 alert("Mail Receipt Processed!");
}
</script>
 
<FORM id="form1" name="WorkInfo"  action="insertRecordFromForm1.php"  method="post">
 
<?php
error_reporting(0);
mysql_connect("localhost","root",'tiger');
@mysql_select_db("worklog") or die( "Unable to select database");
$query="SELECT * FROM tblMyData";
$result=mysql_query($query);
//$Row = mysql_fetch_row($result);
?>
 
<FONT face="arial">
<?php
echo "<b><center>Database Output</center></b><br><br>";
echo "<table class = 't1'  id ='t1' width='100%' border='1'>";
echo "<tr>
       <th width='10'><b></b></td>
       <th><B>Startdate</b></th>
       <th><B>Location</b></th>
       <th><B>Name</b></th>
       <th><B>Description</b></th>
      </tr>";
$i=0;
while($Row = mysql_fetch_array($result)){
 $startdate=$Row[0];
 $location=$Row[1]; 
 $name=$Row[2]; 
 $description=$Row[3];
 echo "<tr>";
 echo "<td style='BACKGROUND-COLOR:#00FFFF'><input type='checkbox' name='index[" . $i . "]'></td>";
 echo "<td style='BACKGROUND-COLOR:#00FFFF'>{$startdate}</td>";
 echo "<td style='BACKGROUND-COLOR:#00FFFF'>{$location}</td>";
 echo "<td style='BACKGROUND-COLOR:#00FFFF'>{$name}</td>";
 echo "<td style='BACKGROUND-COLOR:#00FFFF'>{$description}</td>";
 echo '<input type="hidden" name="startdate[' . $i . ']" value="' . $startdate .'">';
 echo '<input type="hidden" name="location[' . $i . ']" value="' . $location . '">';
 echo '<input type="hidden" name="name[' . $i . ']" value="' . $name . '">';
 echo '<input type="hidden" name="description[' . $i . ']" value="' . $description . '">';
 echo "</tr>";
 $i++;
}
mysql_close();
 ?>
</FONT>
<input type="submit" name="submit" value="Insert">
<input type="submit" name="submit" value="Delete">
</FORM>
</BODY>
</HTML>

Open in new window

0
 
LVL 20

Expert Comment

by:NerdsOfTech
ID: 24378027
Fixed insertRecordFromForm1.php

INSERT syntax fixed.
INSERT INTO table (field1[, ..fieldn]) VALUES (value1[,..valuen])
<?php
 
error_reporting(0);
$con = mysql_connect("localhost","root", "tiger");
if(!$con)
{
 die('Could not connect: ' . mysql_error());
}
@mysql_select_db("worklog", $con);
 
if(isset($_POST['index']))
{
 foreach($_POST['index'] as $key => $value)
 { 
  $sql = "Insert into tblMyDataCompleted (startdate, Location, name, description)  VALUES ('" . $_POST['startdate'][$key]. "','".$_POST['location'][$key]."','".$_POST['name'][$key]."','".$_POST['description'][$key]."';";
 
  mysql_query($sql) or die (mysql_error());
 
  if(!mysql_query($sql,$con))
  {
   die('Error: ' . mysql_error());
   }
  echo "Record Added <br />";
  mysql_close($con);
 }
}
?>

Open in new window

0
 

Author Comment

by:myfm09
ID: 24380424
Hello, Minor typo in Insert Statement: Insert....."';";. I fixed it. "')";
Doesn't work again. Problems:
1. I checked checkbox for  record #2, result: inserted record 2 twice
2. I checked multiple checkboxes(3) for 3 recors, result: inserted one record 2 twice.
0
 
LVL 20

Expert Comment

by:NerdsOfTech
ID: 24387811
Please look at te source code where the checkboxes and inputs are generated. Let me know what the output looks like. Attach text file?

Thanks!
0
 

Author Comment

by:myfm09
ID: 24388327
Hello, Attached two files:
one-screen shots
second - View Source Form1
Thanks
ScreenShots051409.doc
form1-sourceView.txt
0
 
LVL 20

Expert Comment

by:NerdsOfTech
ID: 24393180
Thanks! Everything looks good on the form.

It was the processing page...

it had 1 to many mysql statements; hence the double rows.

REMOVED:
  if(!mysql_query($sql,$con))
  {
   die('Error: ' . mysql_error());
   }


Fixed!

Now everything should work perfectly

=NerdsOfTech
<?php
// insertRecordFromForm1.php
 
error_reporting(0);
$con = mysql_connect("localhost","root", "tiger");
if(!$con)
{
 die('Could not connect: ' . mysql_error());
}
@mysql_select_db("worklog", $con);
 
if(isset($_POST['index']))
{
 foreach($_POST['index'] as $key => $value)
 { 
  $sql = "Insert into tblMyDataCompleted (startdate, Location, name, description)  VALUES ('" . $_POST['startdate'][$key]. "','".$_POST['location'][$key]."','".$_POST['name'][$key]."','".$_POST['description'][$key]."';";
 
  mysql_query($sql) or die (mysql_error());
 
  echo "Record Added <br />";
  mysql_close($con);
 }
}
?>

Open in new window

0
 

Author Comment

by:myfm09
ID: 24399813
Hello, When I click one record, it works. If I selected two or three check boxes, it inserted only first one.
So multiple selection doesn't work.
Thanks
0
 
LVL 20

Expert Comment

by:NerdsOfTech
ID: 24400562
Oops. Try this
<?php
// insertRecordFromForm1.php
 
error_reporting(0);
$con = mysql_connect("localhost","root", "tiger");
if(!$con)
{
 die('Could not connect: ' . mysql_error());
}
@mysql_select_db("worklog", $con);
 
if(isset($_POST['index']))
{
 foreach($_POST['index'] as $key => $value)
 { 
  $sql = "Insert into tblMyDataCompleted (startdate, Location, name, description)  VALUES ('" . $_POST['startdate'][$key]. "','".$_POST['location'][$key]."','".$_POST['name'][$key]."','".$_POST['description'][$key]."';";
 
  mysql_query($sql) or die (mysql_error());
 
  echo "Record Added <br />";
 }
}
mysql_close($con);
?>

Open in new window

0
 

Author Comment

by:myfm09
ID: 24401080
Hello,
You are genius, I am stupid. I closed mysql after insertiing each record:)
Thank you very much!!!
Now I would like to use code(you used in some solution: function expandCollapse(objID) {) to hide records(that I cheked and inserted) in the form.
Also I want to display alert Recod(s) inserted and stay in the form. Now I open new page.

0
 
LVL 20

Accepted Solution

by:
NerdsOfTech earned 200 total points
ID: 24401475
Thanks I overlooked that line too so lets look at the positive; we fixed it. Cool!

:)

First and foremost, you should accept the answer that worked for you. In the case that more than one answer helped you you could split points amongst the correct answers or experts. If one expert helped you the most or gave the best answer(s) you would typically award them the most/or all the points. Moreover, if the best helper gave you a series of answers to help you, it is also typical to select the final answer or most helpful answer that solved the series of answers as the only "accept answer".

Second, experts typically won't touch a question if it is less than 250 points. So make sure, IF a question is IMPORTANT to YOU, offer points that is appropriately quantified to the DIFFICULTY of the question. Typically a question that is worth 50 points means it should take 10 seconds to figure out. 500 would be a question that would take hours upon hours; or days to solve. set points (250, 300, 400, 500, etc.) to CORRESPOND to the IMPORTANCE of the question too. Siunce, the more points you set the more help and the quicker responses you'll hypothetically receive.

Also sometimes askers will offer experts tips other than points to show appreciation of their work above and beyond their question; or if the expert exceeds the expectation of the asker. I have paid tips such as $20.00, $10.00, etc. to experts who have helped teach difficult concepts and/or fixed critical items for me in my journey for more knowledge at experts-exchange.

My paypal address for such tips, just in case you are interested, is:
admin {at} nerdsoftech {dot} com

( of course replace {at} with @ and {dot} with . )

Third, you can also use the above email to hire my services to help you further ( this would be help off-site from expert-exchange ). Email me for current programming rates if this is the case.

Last, here at expert-exchange, experts are allowed to answer one question at a time. I would love to help you on these next questions! If you ask a related question expert-exchange will send me an email saying so and I can look at your subsequent questions. Cool!

To create a related question:
After you select my answer(s) as the best solution; there will be a link that will appear on the bottom of this thread labeled "Ask a related question". Click that and make a new question regarding help on creating a hidable list, etc.

Again, if you would rather hire my services instead of posting additional questions, email me. Best regards, and I hope to hear from you soon.

Sincerely,
=NerdsOfTech
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses
Course of the Month20 days, 19 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question