0

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.

4
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by pritaeas
0

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);			
			}
		}
}
?>
1

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 massage if 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.

Votes + Comments
useful
1

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

Edited by karthik_ppts: n/a

Votes + Comments
This is the actual issue, well spotted
0

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.)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.