This is my first post to DaniWeb, and I've read the Member Rules, so I think this is the correct forum.
I have created a page in PHP that collects seven items of data then displays them lower on the same page. The data columns are: name, alignment, origin, archetype, primary, secondary, and notes. Name and notes are text field values, the rest are selected values from drop down menus.
For some reason, when I do an INSERT query using all seven values the query fails. However, if I remove the selected values it works. Here is the code:
<?php
if(isset($_POST['submitted'])) {
$errors = array();
$name = $_POST['name'];
$alignment = $_POST['alignment'];
$origin = $_POST['origin'];
$archetype = $_POST['archetype'];
$primary = $_POST['primary'];
$secondary = $_POST['secondary'];
$notes = $_POST['notes'];
if(empty($errors)) {
include('includes/passwordsetc.inc'); //not the real extention
$con = mysqli_connect(HOST, USER, PASS, DB) OR die('Could not connect to MySQL: '.mysqli_connect_error());
$query = "SELECT origin FROM names WHERE name = '$name'";
$result = mysqli_query($con, $query);
if(mysqli_num_rows($result) == 0) {
$query = "INSERT INTO names (name, alignment, origin, archetype, primary, secondary, notes)
VALUES ('$name', '$alignment', '$origin', '$archetype', '$primary', '$secondary', '$notes')";
if($result = mysqli_query($con, $query)) {
$success = "Successful!";
} else {
$failure = "Unable to INSERT into DB.";
}
} else {
$failure = "The name '".$name."' is already used.";
$name = "";
$notes = "";
}
} else {
$failure = "Please fix the following errors.";
}
}
$kwords = '';
$title = 'CoH/CoV Names';
$page = 'names';
include('includes/header.inc');
include('includes/dropdown.js');
?>
</head>
<?php
include('includes/body.inc');
?>
<div id="content" class="column">
<h2>Names</h2>
<div class="toppara" id="cntr">
<?php echo $failure;
echo $success;?>
<table>
<tr><form name="addname" method="post" action="index.php">
<td><input type="text" name="name" value="<?php if(isset($_POST['name'])) echo $name;?>"/></td>
<td><select name="alignment" size="1" onchange="setAlign(document.addname.alignment.options[document.addname.alignment.selectedIndex].value);">
<option value="select"></option>
<option value="hero">Hero</option>
<option value="villain">Villain</option>
</select></td>
<td><?php include('includes/origin.inc');?></td>
<td><select name="archetype" size="1" onchange="setPrimary(document.addname.archetype.options[document.addname.archetype.selectedIndex].value);">
<option value=""></option>
</select></td>
<td><select name="primary" size="1">
<option value=""></option>
</select></td>
<td><select name="secondary" size="1">
<option value=""></option>
</select></td>
<td><input type="text" name="notes" value="<?php if(isset($_POST['notes'])) echo $notes;?>" /></td>
<td><input class="button" type="submit" value="Add" />
<input type="hidden" name="submitted" value="true" /></td>
</form></tr>
<tr><td><label for="names"><span class="sort"><a href="index.php?sort=name">NAME</a></span></label></td>
<td><span class="sort"><a href="index.php?sort=alignment">ALIGNMENT</a></span></td>
<td><span class="sort"><a href="index.php?sort=origin">ORIGIN</a></span></td>
<td><span class="sort"><a href="index.php?sort=archetype">ARCHETYPE</a></span></td>
<td><span class="sort"><a href="index.php?sort=origin">PRIMARY</a></span></td>
<td><span class="sort"><a href="index.php?sort=origin">SECONDARY</a></span></td>
<td>NOTES</td>
</tr>
<?php
include('includes/threeolives.inc');
$con = @mysqli_connect(HOST, USER, PASS, DB) OR die('Could not connect to MySQL: '.mysqli_connect_error());
$query = "SELECT * FROM names ORDER BY $sort";
$result = @mysqli_query($con, $query);
if(mysqli_num_rows($result) == 0) {
echo "<tr><td>There are no names to view.</td></tr>";
} else {
while($row = mysqli_fetch_assoc($result)) {
$name = $row['name'];
$alignment = $row['alignment'];
$origin = $row['origin'];
$archetype = $row['archetype'];
$primary = $row['primary'];
$secondary = $row['secondary'];
$notes = $row['notes'];
echo "<tr><td><label for='first'>$name</label></td>
<td>$alignment</td>
<td>$origin</td>
<td>$archetype</td>
<td>$primary</td>
<td>$secondary</td>
<td>$notes</td>
<td></td>
</tr>\n";
}
}
?>
</table>
</div>
</div>
<div class="clr"></div>
</div>
</div>
<?php
include('includes/footer.inc');
?>
I didn't include the included Javascript code or PHP code - it's not relevant to the problem (at least, I don't think so). Thanks for any help on this.