Okay first of I want to apologize for the horrible title, I really couldn't come up with a decent title to be honest.

Here's my problem, I am kind of new to all this but I'd like to try making (just for fun of course) a database.

I have a form that contains basically textfields and 3 checkbox fields.
When I mark let's say two of them and press "submit" I want the script to create TWO inputs in my mysql database. In other words if I mark the box called PS3 and the other called xbox360 I want it to create these two:

Name Console
Eternal Sonata PS3
Eternal Sonata Xbox360

Is there a way to accomplish this? And if not, is there a way to make it look like this instead:
Eternal Sonata Ps3,xbox360


Because right now even if I mark more than one box it only adds the first box value to my database. (So even though I mark both PS3 and XBOX it only writes PS3)

Just in case you want to have a look at my code (and please do feel free to point out things I should correct if you notice anything, for example the reason why everytime I refresh the page it adds an empty to the database)

<html>
<head>
<title> Add game to database </title>
</head>
<body style ="background-color:#300000 ">

<h1 style="color:#B8B8B8  ">Gamelist database</h1>
<hr />
<br />
<p style ="font-family:times;color:#B8B8B8 ; "> Type in the information in the fields below and click Add Game. </p>
<form style="font-family:times;color:#B8B8B8;" action="dbInsert.php" method ="post">

Name: <input type="text" name="Name" />
PS3: <input type ="checkbox" name="Console"[] value="PS3" />
Wii: <input type ="checkbox" name="Console"[] valie ="Wii" />
XBOX360: <input type ="checkbox" name="Console"[] value="XBOX360" /> </br></br>
Developer: <input type="text" name ="Developer" /> 
Publisher: <input type="text" name ="Publisher" /> 
Release Date: <input type ="text" name ="ReleaseDate" /> </br></br>
<input type="submit" value ="Add Game"/>
</form>




<?php
include("dbInfo.php");

// Genom att klicka submit i dbMain så skickas den inskrivna information till dbInsert.

$con = mysql_connect(localhost, $username, $password);
if (!$con)
	{
	die('Could not connect;' .mysql_error());
	}

mysql_select_db("$database", $con);

$sql="INSERT INTO games (Name, Console, Developer, Publisher, ReleaseDate)
VALUES
('$_POST[Name]','$_POST[Console]','$_POST[Developer]','$_POST[Publisher]','$_POST[ReleaseDate]')";

if (!mysql_query($sql,$con))
	{
	die('Error: ' . mysql_error());
	}
	
	mysql_close($con);
	?>

<h2 style="font-family:times;color:#B8B8B8;">	 Games on the list: </h2>


<?php

include("dbInfo.php");

// Genom att klicka submit i dbHome så skickas den inskrivna information till dbInsert.

$con = mysql_connect(localhost, $username, $password);
if (!$con)
	{
	die('Could not connect;' .mysql_error());
	}

mysql_select_db("$database", $con);

$result = mysql_query("SELECT * FROM games");

echo "<table border='3' cellspacing=2 bgcolor='#B0B0B0' cellpadding='8' align='left' frame='void' >
<tr>
<th >Name</th>
<th>Console</th>
<th>Developer</th>
<th>Publisher</th>
<th>ReleaseDate</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['Name'] . "</td>";
  echo "<td>" . $row['Console'] . "</td>";
  echo "<td>" . $row['Developer'] . "</td>";
  echo "<td>" . $row['Publisher'] . "</td>";
  echo "<td>" . $row['ReleaseDate'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);



?>
	


</body>
</html>

and the dbInsert:

<?php
// Header med refresh 3 gör så att sidan visas för att sedan koppla vidare dig till dbHome efter 3 sekunder.
header( 'refresh: 3; url=dbHome.php' );

include("dbInfo.php");

// Genom att klicka submit i dbHome så skickas den inskrivna information till dbInsert.

$con = mysql_connect(localhost, $username, $password);
if (!$con)
	{
	die('Could not connect;' .mysql_error());
	}

mysql_select_db("$database", $con);

$sql="INSERT INTO games (Name, Console, Developer, Publisher, ReleaseDate)
VALUES
('$_POST[Name]','$_POST[Console]','$_POST[Developer]','$_POST[Publisher]','$_POST[ReleaseDate]')";

if (!mysql_query($sql,$con))
	{
	die('Error: ' . mysql_error());
	}
echo "A game have been added to the database. You will be redirected in a moment.................";

mysql_close($con)


?>

Recommended Answers

All 2 Replies

dbHome.php
==========
line 15 - you have a typo (valie instead of value)

lines 26-49 are why you get a blank row added. This is inserting a row by simply loading the page, and since you didn't post anything, it's blank. Delete this section.

dbInsert.php
============
$_POST is an array of checkbox values that were checked. By not looping over this array, you are only pulling the first value. You need to loop, repeating the insert for each value in the console[].

I added the foreach loop on lines 23 & 33.
I also pulled out the POST values into local variables before their use. (made the sql look cleaner for my piece of mind)

<?php
// Header med refresh 3 gör så att sidan visas för att sedan koppla vidare dig till dbHome efter 3 sekunder.
header( 'refresh: 3; url=dbHome.php' );

include("dbInfo.php");

// Genom att klicka submit i dbHome så skickas den inskrivna information till dbInsert.

$con = mysql_connect(localhost, $username, $password);
if (!$con)
{
	die('Could not connect;' .mysql_error());
}

mysql_select_db("$database", $con);

$name=$_POST["Name"];
$console=$_POST["Console"]; // Is an array of console values, use in loop
$developer=$_POST["Developer"];
$publisher=$_POST["Publisher"];
$releasedate=$_POST["ReleaseDate"];

foreach($console as $eachconsole){
	$sql="INSERT INTO games (Name, Console, Developer, Publisher, ReleaseDate)
	VALUES
	('$name','$eachconsole','$developer','$publisher','$releasedate')";

	if (!mysql_query($sql,$con))
	{
		die('Error: ' . mysql_error());
	}
	echo "A game have been added to the database. You will be redirected in a moment.................";
}
mysql_close($con)

?>

Hi, thanks for your reply. It all works like a charm!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.