954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

[Help] insert my data to database

hello to *..
I am new in php can you please help me here!
cant insert my data into my database.

<?php
error_reporting (E_ALL ^ E_NOTICE); 
$add = $_POST['add']; //Add button
$date = $_POST['date'];
$project = $_POST['project'];
$task = $_POST['task'];
$originated = $_POST['originated'];
$incharge = $_POST['incharge'];
$deadline = $_POST['deadline'];
$status = $_POST['status'];
$comment = $_POST['comment'];
$fin = $_POST['fin'];

if ($add)
{
	
	if(is_array($incharge))
		{
		foreach($incharge as $val2)
			{
				$con = mysql_connect ("localhost","root","");
				if (!$con)
				{
					die ('Not connected to DB' . mysql_error());
				}
				
				mysql_select_db ("profound_master", $con);
				$sql = "INSERT INTO bulletin  VALUES ('','$date','$project','$task','$originated','$val2','$deadline','$status','$comment','$fin')"; 
				
				if (!mysql_query ($sql, $con));
				{
					die ('Error Sir' . mysql_error()); //I am stock here.
					
				}
				echo "1 record added";
				
				mysql_close($con);			
			}
		}
}
?>


Thanks in advance.

pakunoda
Newbie Poster
17 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

What's the error message ?

pritaeas
Posting Expert
Moderator
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

I got it sir!
But now when i click the add button this are always comes out "Error Sir" which is this one die ('Error Sir' . mysql_error());

but when i tried to look for my database everything what i inserted was there. Can you please help me to fix my problem?

Thank you so much. This forum is very helpful.

God bless.

<?php
error_reporting (E_ALL ^ E_NOTICE); 
$add = $_POST['add']; //Add button
$date = $_POST['date'];
$project = $_POST['project'];
$task = $_POST['task'];
$originated = $_POST['originated'];
$incharge = $_POST['incharge'];
$deadline = $_POST['deadline'];
$status = $_POST['status'];
$comment = $_POST['comment'];
$fin = $_POST['fin'];
//If add button click
if ($add)
{
	//This is for checkbox group
	if(is_array($incharge))
		{
		foreach($incharge as $val2)
			{
				$tstring = implode(', ' , $incharge);
				
				//Database Connection
				$con = mysql_connect ("localhost","root","");
				if (!$con)
				{
					die ('Not connected to DB' . mysql_error());
				}
				//Selecting Database
				mysql_select_db ("profound_master", $con);
				//Adding of data to te Database
				$sql = "INSERT INTO bulletin VALUES ('','$date','$project','$task','$originated','$tstring','$deadline','$status','$comment','$fin')"; 
				
				
				if (!mysql_query ($sql, $con));
				{
					die ('Error Sir' . mysql_error()); //I always stop here.
					
				}
				echo "1 record added";
				
				mysql_close($con);			
			}
		}
}
?>
pakunoda
Newbie Poster
17 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

The problem is foreach operation db is connected to and closed, so i change the code to this;

<?
error_reporting (E_ALL ^ E_NOTICE); 
$add = $_POST['add']; //Add button
$date = $_POST['date'];
$project = $_POST['project'];
$task = $_POST['task'];
$originated = $_POST['originated'];
$incharge = $_POST['incharge'];
$deadline = $_POST['deadline'];
$status = $_POST['status'];
$comment = $_POST['comment'];
$fin = $_POST['fin'];

//Database Connection
$con = mysql_connect ("localhost","root","") or die ('Not connected to DB' . mysql_error());	//to avoid uneccessary connection for each operation
mysql_select_db ("profound_master", $con);	//Selecting Database

//If add button click
if ($add)
{
	//This is for checkbox group
	if(is_array($incharge))
		{
		foreach($incharge as $val2)
			{
				$tstring = implode(', ' , $incharge);
				
				
				//Adding of data to te Database
				$sql = "INSERT INTO bulletin VALUES ('','$date','$project','$task','$originated','$tstring','$deadline','$status','$comment','$fin')"; 
				
				
	mysql_query ($sql, $con)) or die ('Error Sir' . mysql_error()); //I always stop here.
					
				echo "1 record added";
							
			}
		}
}

mysql_close($con);	//to avoid colsing db foreach operation
?>


Note how your error massageif blocks were condensed to;

$con = mysql_connect ("localhost","root","") or die ('Not connected to DB' . mysql_error());

and

mysql_query ($sql, $con)) or die ('Error Sir' . mysql_error());


respectively.

faroukmuhammad
Junior Poster
140 posts since Sep 2008
Reputation Points: 20
Solved Threads: 16
 

Try what faroukmuhammad suggested. and Remove the semicolon; from your line 35.

Karthik_pranas
Posting Pro
564 posts since Feb 2011
Reputation Points: 96
Solved Threads: 97
 
The problem is foreach operation db is connected to and closed...

Can you explain why you think this would be the issue ? The code, however inefficient, is still correct. (The semi-colon as mentioned by Karthik_pranas is the real issue.)

pritaeas
Posting Expert
Moderator
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: