Oh please help...Foreach & Insert SQL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pjb2u
Forum Newbie
Posts: 3
Joined: Wed Aug 25, 2010 5:50 am

Oh please help...Foreach & Insert SQL

Post 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

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

Post 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()
Post Reply