Page 1 of 1

Oh please help...Foreach & Insert SQL

Posted: Fri Jul 08, 2011 2:18 pm
by pjb2u
Hi guys, I am a beginner in both PHP and MySQL, I currently have some javascript creating me new text inputs depending on how many the user wants, this is the following line of code:

Code: Select all

document.getElementById('text').innerHTML += + fields +":<input name='extra_id[]' class='test' type='text' value='' /><br /><br />";
Here is my current SAVE CHANGES Insert statement:

Code: Select all

if(isset($_POST['save_changes'])) {
		
		$bet_name = $_POST['bet_name'];
		$time_date = $_POST['time_date'];
		$bet_sp = $_POST['bet_sp'];	
		$event_url = $_POST['event_url'];	
		
$sqlAddContent = "INSERT INTO bets (`id`, `bet_name`, `time_date`, `bet_sp`, `bookmaker_id`, `event_url`) VALUES ('$currentuser', '$bet_name', '$time_date', '$bet_sp', '$currentusername', '$event_url')";
			
		mysql_query($sqlAddContent) or die (mysql_error());
		header("Location: index.php");
	
	}	
Basically I want all extra_id[] to be put into my sql field called extra_id which helps lol - but im guessing I would need to use some kind of FOREACH statement (which I am new to, so I need help really)


EDIT
I have just tried the following code, however I am getting an unexpected '}' error, just before the FIRST insert statement?

Code: Select all

if(isset($_POST['save_changes'])) {
		
		foreach($_POST['extra_id'] as $value)
		{
		$insert="INSERT INTO bets (`extra_id`) VALUES ('$value')";
		
		mysql_query($insert) OR die(mysql_error())
		
		}
		
		$bet_name = $_POST['bet_name'];
		$time_date = $_POST['time_date'];
		$bet_sp = $_POST['bet_sp'];	
		$event_url = $_POST['event_url'];	
		
$sqlAddContent = "INSERT INTO bets (`id`, `bet_name`, `time_date`, `bet_sp`, `bookmaker_id`, `event_url`) VALUES ('$currentuser', '$bet_name', '$time_date', '$bet_sp', '$currentusername', '$event_url')";
			
		mysql_query($sqlAddContent) or die (mysql_error());
		header("Location: index.php");
	
	}	
I hope this latest part of the code gives you a better idea of what I am trying to achieve.

Thanks in advance!!!
Peter

Re: Oh please help...Foreach & Insert SQL

Posted: Fri Jul 08, 2011 10:43 pm
by Weirdan
I have just tried the following code, however I am getting an unexpected '}' error, just before the FIRST insert statement?
You missed semicolon after die()