INSERT data into multiple tables using one form

So far I have this code.


			<?php
			
				if(isset($_POST['artists'])):
				
				include("./db-connect.inc.php");
				
				$artists = $_POST['artists'];
				$bio = $_POST['bio'];
				$gigdate = $_POST['gigdate'];
				$gigtime = $_POST['gigtime'];
				$price = $_POST['price'];
				$venue_name = $_POST['venue_name'];
				$venue_city = $_POST['venue_city'];
				$venue_address = $_POST['venue_address'];
				$venue_postcode = $_POST['venue_postcode'];
				$venue_tel = $_POST['venue_tel'];
				
				$sql = "INSERT INTO `artists` ( `artists` , `bio` )
					VALUES ('$artists', '$bio');";
					
				$sql2 = "INSERT INTO `listings` ( `gigdate` , `gigtime` , `price` )
					VALUES ('$gigdate', '$gigtime', '$price');";
				
				$sql3 = "INSERT INTO `venue` ( `venue_name`, `venue_city`, `venue_address`, `venue_postcode`, `venue_tel` )
					VALUES ('$venue_name', '$venue_city', '$venue_address', '$venue_postcode', '$venue_tel');";
					
				
				if(@mysql_query($sql and $sql2 and $sql3))
				{	
					echo '<p>New Listing!</p>';
				}
				else
				{
					echo '<p>Error:' . mysql_error() . '</p>';
				}
			?>
			
			<?php else: ?>

			<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
				<fieldset>
					<legend>Basic Information</legend>
					<ol>
						<li>
							<label for="artists">Artists/Band*</label>
							<input name="artists" />
						</li>
												<li>
							<label for="bio">Bio*</label>
							<textarea name="bio" rows="8" cols="40"> </textarea>
						</li>
						<li>
							<label for="gigdate">Date*</label>
							<input name="gigdate" />
						</li>
						<li>
							<label for="gigtime">Time*</label>
							<input name="gigtime" />
						</li>
						<li>
							<label for="price">Price*</label>
							<input name="price" />
						</li>
						<li>
							<label for="venue_name">Venue*</label>
							<input name="venue_name" />
						</li>
						<li>
							<label for="venue_city">Venue City*</label>
							<input name="venue_city" />
						</li>
					</ol>
				</fieldset>

				<fieldset>
					<legend>Additional Venue Information</legend>
					<ol>
						<li>
							<label for="venue_address">Venue Address</label>
							<input name="venue_address" />
						</li>
						<li>
							<label for="venue_postcode">Venue Postcode</label>
							<input name="venue_postcode" />
						</li>
						<li>
							<label for="venue_tel">Venue Tel No.</label>
							<input name="venue_tel" />
						</li>
					</ol>
				</fieldset>

				<input type="submit" value="Add New Listing!" />
			</form>
			

			
			<?php endif; ?>

Now every time I run it I get nothing back. It goes straight back to the start of the page and loads the form up cleanly again. Does anyone know what I’m doing wrong, or where I can go to fix it?

Run the mysql_query() seperately:


$sql = mysql_query("INSERT INTO `artists` ( `artists` , `bio` )
                    VALUES ('$artists', '$bio')");

                $sql2 = mysql_query("INSERT INTO `listings` ( `gigdate` , `gigtime` , `price` )
                    VALUES ('$gigdate', '$gigtime', '$price')");

                $sql3 = mysql_query("INSERT INTO `venue` ( `venue_name`, `venue_city`, `venue_address`, `venue_postcode`, `venue_tel` )
                    VALUES ('$venue_name', '$venue_city', '$venue_address', '$venue_postcode', '$venue_tel')");
 

Also before you insert ANYTHING from a user into the database, run it through mysql_real_escape_string() to filter any bad input.

example


$sql = mysql_query("
	INSERT INTO
		`artists`
	( `artists` , `bio` )
		VALUES
	('". mysql_real_escape_string($artists). "',
	'". mysql_real_escape_string($bio). "'
	)");

That did the job. Thanks for the help, and writing out examples of the code. :slight_smile:

np :smiley: