Jump to content

Sort Order Not Inserting Into Database


Xtremer360

Recommended Posts

For some reason every time I run this form it inserts an undefined value into the field called sortorder into my database table. The rest of the form inputs insert properly but that one. Here's the necessary coding. Anyone see why?

 

 

form page

<?php
   
// Include the database page
require ('../inc/dbconfig.php');

?>

<script type="text/javascript">
    $(document).ready(function() {
    $('div.message-error').hide();
    $('div.message-success').hide();
     $("input.submit").click(function() {
             $('div.message-error').hide();
            var charactername = $("input#charactername").val();
       		if (charactername == "") {
             $("div.message-error").show();
             $("input#charactername").focus();
             return false;
           } 
           var charactershortname = $("input#charactershortname").val();
       		if (charactershortname == "") {
             $("div.message-error").show();
             $("input#charactershortname").focus();
             return false;
           } 
           var sortorder = $("input#sortorder").val();
       		if (sortorder == "") {
             $("div.message-error").show();
             $("input#sortorder").focus();
             return false;
           } 
            var style = $("select#style").val();
       		if (style == "") {
             $("div.message-error").show();
             $("select#style").focus();
             return false;
           } 
            var status = $("select#status").val();
       		if (status == "") {
             $("div.message-error").show();
             $("select#status").focus();
             return false;
           } 
            var alignment = $("select#alignment").val();
       		if (alignment == "") {
             $("div.message-error").show();
             $("select#alignment").focus();
             return false;
           } 
           var division = $("select#division").val();
       		if (division == "") {
             $("div.message-error").show();
             $("select#division").focus();
             return false;
           } 
             var dataString = 'charactername=' + charactername + '&charactershortname=' + charactershortname + '&sortorder=' + sortorder + '&style=' + style + '&status=' + status + '&alignment=' + alignment + '&division=' + division + '&submitcharacter=True';
             $.ajax({
             type: "POST",
             url: "processes/character.php",
             data: dataString,
             
              success: function() {
              $('div.message-error').hide();
                $("div.message-success").html("<h6>Operation successful</h6><p>Character " + charactername + " saved successfully.</p>");
                $("div.message-success").show().delay(10000).hide("slow");
                $(':input','#characterform')
                .not(':submit')
                .val('')
                return true;
            }
              
       });
       return false;    
         });
  });
  
</script>

<!-- Form -->
<form action="#" id="characterform" >
<fieldset>
	<legend>Add New Character</legend>

	<div class="field required">
		<label for="charactername">Character Name</label>
		<input type="text" class="text" name="charactername" id="charactername" title="Character Name"/>
		<span class="required-icon tooltip" title="Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. ">Required</span>
	</div>
        <div class="field required">
		<label for="charactershortname">Character Short Name</label>
		<input type="text" class="text" name="charactershortname" id="charactershortname" title="Character Short Name"/>
		<span class="required-icon tooltip" title="Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. ">Required</span>
	</div>
        <div class="field required">
		<label for="sortorder">Sort Order</label>
            <select class="dropdown" name="sortorder" id="sortorder" title="Sort Order">
            <option value="0">- Select -</option>
            <?php
                $sortorder = array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","0-9");
                foreach($sortorder as $so):
            ?>        
            <option value="<?php echo $so; ?>"><?php echo $so; ?></option>
         
         <?php endforeach; ?>
            </select>
		<span class="required-icon tooltip" title="Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. ">Required</span>
	</div>
        <div class="field required">
		<label for="style">Character Style</label>
            <select class="dropdown" name="style" id="style" title="Style">
            <option value="0">- Select -</option>
            <?php
            $query = 'SELECT id, stylename FROM styles LIMIT 2';
            $result = mysqli_query ( $dbc, $query ); // Run The Query
            while ( $row = mysqli_fetch_array ( $result, MYSQL_ASSOC ) ) { 
                print "<option value=\"".$row['id']."\">".$row['stylename']."</option>\r";
            }
            ?>
            </select>
		<span class="required-icon tooltip" title="Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. ">Required</span>
	</div>
        <div class="field required">
		<label for="status">Character Status</label>
            <select class="dropdown" name="status" id="status" title="Status">
            <option value="0">- Select -</option>
            <?php
            $query = 'SELECT id, statusname FROM statuses';
            $result = mysqli_query ( $dbc, $query ); // Run The Query
            while ( $row = mysqli_fetch_array ( $result, MYSQL_ASSOC ) ) { 
                print "<option value=\"".$row['id']."\">".$row['statusname']."</option>\r";
            }
            ?>
            </select>
		<span class="required-icon tooltip" title="Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. ">Required</span>
	</div>
        <div class="field required">
		<label for="alignment">Alignment</label>
            <select class="dropdown" name="alignment" id="alignment" title="Alignment">
            <option value="0">- Select -</option>
            <?php
            $query = 'SELECT id, alignmentname FROM alignments';
            $result = mysqli_query ( $dbc, $query ); // Run The Query
            while ( $row = mysqli_fetch_array ( $result, MYSQL_ASSOC ) ) { 
                print "<option value=\"".$row['id']."\">".$row['alignmentname']."</option>\r";
            }
            ?>
            </select>
		<span class="required-icon tooltip" title="Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. ">Required</span>
	</div>
        <div class="field required">
		<label for="division">Division</label>
            <select class="dropdown" name="division" id="division" title="Division">
            <option value="0">- Select -</option>
            <?php
            $query = 'SELECT id, divisionname FROM divisions';
            $result = mysqli_query ( $dbc, $query ); // Run The Query
            while ( $row = mysqli_fetch_array ( $result, MYSQL_ASSOC ) ) { 
                print "<option value=\"".$row['id']."\">".$row['divisionname']."</option>\r";
            }
            ?>
            </select>
		<span class="required-icon tooltip" title="Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. ">Required</span>
	</div>
        	<input type="submit" class="submit" name="submitcharacter" id="submitcharacter" title="Submit Character" value="Submit Character"/>
</fieldset>
</form>
<!-- /Form -->

<!-- Messages -->
<div class="message message-error">
    <h6>Required field missing</h6>
    <p>Please fill in all required fields. </p>
</div>

<div class="message message-success">
    <h6>Operation succesful</h6>
    <p>Character was added to the database.</p>
</div>
<!-- /Messages -->

 

 

validation page

<?php

// Include the database page
require ('../inc/dbconfig.php');

if (isset($_POST['submitcharacter'])) {
    $charactername = mysqli_real_escape_string($dbc, $_POST['charactername']);
    $charactershortname = mysqli_real_escape_string($dbc, $_POST['charactershortname']);
    $sortorder = mysqli_real_escape_string($dbc, $_POST['sortorder']);
    $style = mysqli_real_escape_string($dbc, $_POST['style']);
    $status = mysqli_real_escape_string($dbc, $_POST['status']);
    $alignment = mysqli_real_escape_string($dbc, $_POST['alignment']);
    $division = mysqli_real_escape_string($dbc, $_POST['division']);

    $query = "INSERT INTO `characters` (charactername, charactershortname, status_id, style_id, division_id, alignment_id, sortorder, creator_id, datecreated) VALUES ('$charactername','$charactershortname','$status','$style','$division', '$alignment', '$sortorder', 1, NOW())";
    mysqli_query($dbc, $query) ;

}
        
?>

Link to comment
https://forums.phpfreaks.com/topic/220070-sort-order-not-inserting-into-database/
Share on other sites

I'm sure you've echoed your query string already to see what values it holds, right? Then you've pasted it into phpMyAdmin and executed it there to see if the problem is reproducible, right?

That's the odd part about it because it goes through ajax and I can't figure out how to get it to echo the query. I know how to echo the query but to get it to show up when I do echo $query; after it runs it it won't show anything because of the ajax I'm sure and trying to figure out how to get it to show the echoed query.

either pass the sql back through the ajax and display it via javascript alert(), have the php script write the query to a log file, or have php email the query to you.

 

oh yeah:  or execute the php via browser, passing the query parameters as ajax would.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.