0

So just like always, I'm working on code that up until now I have had no experience with.

What I'm trying to do is create a ticket type system for a department here at work to use to contact potential students. I'm ok with the HTML and PHP for the most part, it's learning the SQL that has me stumped. I'm really not even sure if it's all being done right. I may need to incorporate AJAX as well. Basically, the contact will fill out a form with information about what type of degree they want to pursue, and contact info etc... Everything inserts fine into the SQL but the problem comes when I want to edit it from my viewtable.php page.

This is just the working prototype, eventually it will have user login and other such things, as well as admin console, etc... so without further ado, here is my viewtable code. If it's insufficient, please let me know. I'll keep researching but any help you guys can offer would be appreciated.

<?php
session_start();
//Must be called before any code or white space is executed
/*
TODO:
    1: Get checkboxes to load and display only what was posted via checkboxes from a previously loaded page
    2: Cleanup
    3: Hover messages for table headers
    4: Program search bar
    5: Get dropdown to do something
    6: Program to only show 10 entries at a time, and be able to move to the next ten entries
    7: program to be able to sort by "Last Name" , "First Name", etc...
    8: Move database info to a called file for security and ease of access.
*/
$action=$_POST['action'];
$id=array($_POST['id']);
//Create Connection
$con = mysqli_connect("localhost","username","password","database");

//Check Connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else {
//echo "Connection Successful! \n";
}
//If the action dropdown was set
if($action = 'delete') {
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql ="DELETE FROM persons WHERE PID='$del_id'";
$result = mysql_query($sql);}
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">";}
}
$query = "Select * FROM persons";
$result = mysqli_query($con, $query);

$num=mysqli_num_rows($result);
echo $id;
echo $action;
?>
<!DOCTYPE html>
<html>
    <head>
        <style>
        tr:nth-child(even) {
        background-color:#00fff0;
        }
        </style>
        <script>
        </script>
    </head>
    <body>
        <table id="persons" name="persons" border="0" cellpadding="1">
        <form name="database" action="viewtable.php" type="post">
        <div type="hidden" class="pidmessage" style="display:none;">Unique Identifier</div>
        <div type="hidden" class="fnamemessage" style="display:none;">Contact's First Name</div>
        <div type="hidden" class="lnamemessage" style="display:none;">Contact's Last Name</div>
        <div type="hidden" class="emailmessage" style="display:none;">Contact's Email Address</div>
        <div type="hidden" class="phonemessage" style="display:none;">Contact's Home Phone Number</div>
        <div type="hidden" class="cellmessage" style="display:none;">Contact's Mobile Phone Number</div>
        <div type="hidden" class="campusmessage" style="display:none;">Is the contact interested in residential or online courses?</div>
        <div type="hidden" class="statemessage" style="display:none;">Contact's Home State</div>
        <div type="hidden" class="studenttypemessage" style="display:none;">The contact is interested in enrolling as this</div>
        <div type="hidden" class="termmessage" style="display:none;">The contact is interested in enrolling beginning this term</div>
        <div type="hidden" class="enrollmentmessage" style="display:none;">The contact is interested in enrolling in this many credit hours</div>
        <div type="hidden" class="degreemessage" style="display:none;">The contact is interested in pursuing this type of degree</div>
        <div type="hidden" class="messagemessage" style="display:none;">The contact heard about TTU in this manner</div>
        <div type="hidden" class="commentsmessage" style="display:none;">These are comments the contact left</div>
        <?php
        $i = 0;
        $data = array();
        while($row = mysqli_fetch_assoc($result)) { 
                $data[] = $row;
                }

            $colNames = array_keys(reset($data));
        ?>

        <tr><th>&nbsp;</th>
            <?php
                foreach($colNames as $colName)
                    {
                        echo"<th class='{$colName}'>$colName</th>";
                    }
            ?>
        </tr>
        <?php
            foreach($data as $row)
                {
                echo "<tr>";
                echo "<td><input type='checkbox' name='checkbox[]' value='{$row['PID']}'></input></td>";
                foreach($colNames as $colName)
                    {
                    echo "<td>".$row[$colName]."</td>";
                    }
                    echo"</tr>";
                }
        ?>
        <tr class="clear"><td>&nbsp;</td><td colspan="7">
            <select name="action">
                <option value="">--Select One--</option>
                <option value="delete">Delete Entry</option>
                <option value="assign">Assign</option>
                <option value="contacted">Move to     contacted</option>
                <option value="edit">Edit</option>
                <option value="">Cancel</option>
            </select></td>
            <td colspan="7">
                <input type="submit" name="submit" value="Submit">    </input></td></tr>
        </form>
        </table>
    </body>
</html>
<?php
mysqli_close($con);
?>

Note: The todo list that is commented out on top is on the list of things (clearly) todo...

Menelmor
  • 72
  • 1
  • 12

1 Answers1

0

So far from what I see its this section:

for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql ="DELETE FROM persons WHERE PID='$del_id'";
$result = mysql_query($sql);}

I don't see where $count is declared, nor $checkbox. If $checkbox is an array, where are you getting it from?

Otherwise, making this work from ajax shouldn't be to hard as long as you're familiar with jQuery. Just cut the code that does the updating to its own php file, and have jQuery pass the form values through ajax to the new script.

CP510
  • 2,297
  • 15
  • 15
  • plus the fact that OP used mysqli then used `mysql_query` on that last line you quoted, shouldn't it be `mysqli_query()`.. or would that be fine? – reikyoushin May 17 '13 at 21:52
  • Very true! Didn't notice that myself. – CP510 May 17 '13 at 22:00
  • @cp510 I've fixed the checkbox array and attempted to code some AJAX myself, but again, i'm very inexperienced with AJAX and SQL (and have only just got my feet wet in PHP and JavaScript) and have zero experience with jQuery. I'm not sure if I was clear what I wanted to happen before. My goal is to dynamically alter the database. i.e. when the checkbox is checked and the action posts delete, I want to drop that field with that PID from the SQL database. I'm slowly working my way through it all, but I appreciate any help you can offer. i fixed the mysqli_query too. it's all mysqli now. – Menelmor May 22 '13 at 20:55