First off, i have checked to make sure that the const.php is in the directory where the page is.
I am creating a page that would allow admin to add multiple entries to a MySQL table through the website. It uses a JavaScript to expand the array of textbox input fields so that an admin does not have to enter any more entries than he/she has to. But if the entry the admin is trying to add is already there, then it won't be added. After the code is run the user is told which entries were added to the table and which entries were not added because there was already such an entry.
Here is the form which passes input arrays to the PHP code.
form id=userform action="addplayers.php" method="post" >
<legend>Player Info</legend>
<ol>
<div id="dynamicInput">
<li>
<label for=player>Player</label>
<input id=player type="text" name="player[]">
</li>
<li>
<label for=team>Team</label>
<input id=team type="text" name="team[]">
</li>
<li>
<label for=path>Player page path</label>
<input id=path type="text" name="path[]">
</li>
<li>
<label for=image>Player image path</label>
<input id=image type="text" name="image[]">
</li>
<br/>
</div>
</ol>
<input type="button" value="ADD ANOTHER PLAYER" onClick="addInput();">
<button type=submit name=submit> Submit </button>
</form>
Here the javascript code dynamically creates textbox input fields which expands the input array.
<script language="Javascript" type="text/javascript">
function addInput(){
var newdiv = document.createElement('div');
newdiv.innerHTML = "<li><label for=player>Player</label><input id=player type='text' name='player[]'></li>";
document.getElementById('dynamicInput').appendChild(newdiv);
var newdiv = document.createElement('div');
newdiv.innerHTML = "<li><label for=team>Team</label><input id=team type='text' name='team[]'></li>";
document.getElementById('dynamicInput').appendChild(newdiv);
var newdiv = document.createElement('div');
newdiv.innerHTML = "<li><label for=path>Player page path</label><input id=path type='text' name='path[]'></li>";
document.getElementById('dynamicInput').appendChild(newdiv);
var newdiv = document.createElement('div');
newdiv.innerHTML = "<li><label for=image>Player image path</label><input id=image type='text' name='image[]'></li><br/>";
document.getElementById('dynamicInput').appendChild(newdiv);
}
</script>
Here is the php code that form posts to.
include "const.php";
$entry_results = "";
if( isset($_POST['submit']) )
{
$conn = mysql_connect(MYSQL_HOST, MYSQL_LOGIN, MYSQL_PASSWORD) or die("Could not connect: " . mysql_error());
mysql_select_db(MYSQL_DB);
$player = $_POST['player'];
$team = $_POST['team'];
$path = $_POST['path'];
$image = $_POST['image'];
$invalid = array();
$valid = array();
$j = 0;
$k = 0;
for($i=0; $i<count($player);$i++)
{
//Check to see if player is in the database
$query = "Select name FROM tblPlayers where name = '" . $player[i] ."'";
$result = mysql_query($query);
if(!empty($result))//if query gives a result add player to list of invalid entries
{
$invalid[$j++] = $player[$i];
}
else//otherwise add to database
{
$valid[$k++] = $player[$i];
if(empty($image[$i]))
{$image[$i] = '#';}
if(empty($path[$i]))
{$path[$i] = '#';}
$query = "SELECT entity_id FROM tblTeams WHERE team = '" . $team[$i] . "'";
$result = mysql_query($query);
$query = "INSERT INTO tblPlayers ( team_id, name, image, path) VALUES (
'" . $result . "',
'" . $player[$i] . "',
'" . $image[$i] . "',
'" . $path[$i] . "'
)";
$result = mysql_query($query);
}
}
if(!empty($invalid[0]))
{
for($i=0;$i<count($invalid);$i++){
$entry_results .= $invalid[$i];
if(($i+1)!=count($invalid))
$entry_results .= ', ';
}
$entry_results .= "were found in the database and were not enterered to prevent duplicant record. ";
}
if(!empty($valid[0]))
{
for($i=0;$i<count($valid);$i++){
$entry_results .= $invalid[$i];
if(($i+1)!=count($valid))
$entry_results .= ', ';
}
$entry_results .= "were entered into the players table.";
}
mysql_close($conn);
}
?>
This separate line of PHP code tells admin the result of the entry.
<?php
if( !empty($entry_results) )
{
echo "<h3>$register_message</h3><br />\n";
}
?>