1

The title nearly explains itself to be honest. I am trying to change my Delete column from 0 to 1, depending on what player has been selected from my listplayers drop down.

My listPlayers.php file is working 100%. The players are showing up fine in the dropdown. I should add that the error is happening within my $sql variable.

<?php
include 'dbconnect.php';
$sql = "UPDATE players SET Delete = 1 WHERE playersID = $_POST[playersIDHidden]";

if(! mysql_query($sql, $conn))
{
    echo "Error " . mysql_error();
}

else
{
    if(mysql_affected_rows() != 0)
    {
        echo $_POST[pName] . " has been deleted <br>";
        echo "Person ID: " . $_POST ['playersID'] . "<br>";
        echo "Players Name: " . $_POST ['pName'] . "<br>";

        echo "Deletion complete";
    }

    else
    {
        echo "No records were changed";
    }
}

mysql_close($conn);
?>

<form action = "viewPlayers.php" method = "POST" />
    <input type = "submit" value = "View Players">
</form>

And finally my listPlayers.php that is showing my players names in the drop down.

<?php
include "dbconnect.php"; //Opening Database Connection

$sql = "SELECT * FROM `players` WHERE `Delete` = 0;";

if (!$result = mysql_query($sql, $conn))
{
    die('Error in querying the database' . mysql_error());
}

echo "<br><select name = 'listPersons' id = 'listPersons' onclick = 'populate()'>";

while ($row = mysql_fetch_array($result))
{
    $id = $row['playersID'];
    $pName = $row['playersName'];
    $dob = $row['playersDateOfBirth'];
    $dob = date  ("d-m-Y", strtotime($dob));

    $allText = "$id, $pName, $dob";
    echo "<option value = '$allText'> $pName </option>";
}

echo "</select>";
mysql_close($conn);
?>

Sorry for the relatively long post. I am generally able to fix 99% of undefined errors. This one has me lost.

Cheers!

BudgieBr0phy
  • 21
  • 1
  • 1
  • 8
  • *"Could this be happening because my playersID text box is disabled?"* - Try it without the disabled and see what results you get. – Funk Forty Niner Mar 08 '15 at 17:43
  • Makes sense. It works, but the textboxes are not allowed to be edited. Any way I can leave the textboxes enabled, but still not allow the user to edit them? – BudgieBr0phy Mar 08 '15 at 17:45
  • You can always try using a hidden attribute. – Funk Forty Niner Mar 08 '15 at 17:46
  • I would use the hidden attribute, but when I select a player in the listPlayers drop down, the players data is to be shown in the corresponding textboxes before the "delete" takes place. So hidden is not going to work in this case unfortunately. – BudgieBr0phy Mar 08 '15 at 17:49

1 Answers1

0

Use your disabled textbox to display it for the user.

Use another hidden field to hold the playersID.

Populate both of them with your script and then you can use the value in the hidden field when you need to delete.

For clarity I'm showing you what your HTML might look like - here's the script:

<script>
    function populate()
    {
        var select = document.getElementById("listPersons"); 
        var result = select.options[select.selectedIndex].value;

        var personDetails = result.split(", ");
        document.getElementById("playersID_display").value = personDetails[0];
        document.getElementById("playersID").value = personDetails[0];
        document.getElementById("pName_display").value = personDetails[1];
        document.getElementById("pName").value = personDetails[1];
....

and the html:

<form name = "myForm" action = "deletePlayer.php" method = "POST">    
    <p>
        <label for> Players ID:
            <input type = "text" name = "playersID_display" id = "playersID_display" disabled/>
            <input type = "hidden" name = "playersID" id = "playersID" />
        </label>
    </p>

    <p>
        <label for> Players Name:
            <input type = "text" name = "pName_display" id = "pName_display" disabled/>
            <input type = "hidden" name = "pName" id = "pName" />
        </label>
    </p>

    <p>
        <label for> Date of Birth:
            <input type = "date" name = "DoB_display" id = "DoB_display" disabled/>
            <input type = "hidden" name = "DoB" id = "DoB" />
        </label>
    </p>

    <input type = "Submit" value = "Delete" class = "button" onClick = "return confirm('Are you sure you want to delete this player?')"/>

</form>     

Note that I have maintained unique names for the fields and unique IDs for the HTML input elements. I also prefer the type="hidden" rather than putting hidden at the end of the <input ... hidden /> element - I'm not familiar with that syntax so I'm using my preferred way.

Obviously your script will have to update EACH of these fields individually.

Peter Bowers
  • 3,063
  • 1
  • 10
  • 18
  • Ok, very good idea. I have it written out now so that I have two input boxes, as shown below. But when I click delete, I am now getting an error in my sql syntax. A problem with a " on line 1. But line one has my opening php tag. `

    `
    – BudgieBr0phy Mar 08 '15 at 18:23
  • When SQL reports an error it isn't the line # from the script but the line # from the SQL query. What is the specific error you're getting? – Peter Bowers Mar 08 '15 at 18:25
  • `You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1` It is showing this error in the deletePlayer.php file – BudgieBr0phy Mar 08 '15 at 18:26
  • I didn't mention it in my answer, but mysql_* is deprecated - you should really be using either mysqli_* or PDO with prepared statements. – Peter Bowers Mar 08 '15 at 18:26
  • Which query is causing the error? And which script - I've gotten lost between the various ones you have posted in your question... – Peter Bowers Mar 08 '15 at 18:29
  • You may need to edit your question and put your (changed) script which is causing the problem in at the bottom as an update to the question so we can see the new code. – Peter Bowers Mar 08 '15 at 18:31
  • `$sql = "UPDATE `players` SET `Delete`= 1 WHERE `playersID` = $_POST[playersID]";` It is present in the middle block of code in the original question. – BudgieBr0phy Mar 08 '15 at 18:31
  • I have updated the first block of code, which is the only thing I have changed since the start of this question. – BudgieBr0phy Mar 08 '15 at 18:34
  • Whatever quote you have right after the table name (`players`) is not a normal backquote. If it were a normal backquote then it would have registered as inline code in your comment above, assuming you did copy/paste. Delete it and enter it again as a backquote (or get rid of the backquotes there entirely - they aren't needed) and see what happens. – Peter Bowers Mar 08 '15 at 18:34
  • I have changed that line. Error has changed to the following `You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Delete = 1 WHERE playersID =' at line 1` – BudgieBr0phy Mar 08 '15 at 18:36
  • Never mind - I just tested it and the reason why it shows funny in your comment is because the backquote is at the end of a word when syntactically it would be starting a chunk of inline code. That's probably not the problem. – Peter Bowers Mar 08 '15 at 18:36
  • I just reviewed your text up there - you're going to run into problems there. You need to have different names for each input field. And use `type="hidden"` rather than `type="text" ... hidden` for your hidden fields. And having the same ID for different elements is a big no-no in HTML. – Peter Bowers Mar 08 '15 at 18:39
  • So I have changed the input type = "hidden" and changed the id and name of the hidden fields to playerIDHidden, etc. Updated the code above. – BudgieBr0phy Mar 08 '15 at 18:42
  • Your script doesn't update the hidden fields. And your PHP now needs to look at the hidden field names rather than the names without "hidden" in them. (That's why I changed the disabled one with `_display` rather than renaming the hidden ones - to avoid having to go back and update your PHP scripts.) – Peter Bowers Mar 08 '15 at 18:46
  • Afraid you have me lost now. I have reverted my changes back to earlier, without the hidden text boxes. Gonna see if I can make sense of everything and try again – BudgieBr0phy Mar 08 '15 at 19:06
  • Sorry Peter. I just got to come back to my PC now. That code has worked wonders!. I just need to fix my sql query now and I am up and running. Getting thrown an error on my WHERE clause. Should be a simple fix anyway. – BudgieBr0phy Mar 09 '15 at 17:58
  • If it's helped I'd appreciate it if you could give it an up-vote and mark it as the answer... – Peter Bowers Mar 09 '15 at 19:11
  • Marked as answer but can't up vote until my reputation hits 15. – BudgieBr0phy Mar 09 '15 at 20:12