添加/删除/编辑PHP / SQL表单

时间:2014-02-21 12:30:08

标签: php html sql sql-delete

以下是我用于管理足球设备和结果管理网站的代码。我遇到的问题是删除语法(我想我的[ID]混淆了)。我有其他一切正常。

如果我尝试删除第一行,则会显示以下错误:

  

错误删除[您的SQL语法中有错误;检查与MySQL服务器版本对应的手册,以便在第1行的'standings_id ='0''附近使用正确的语法。

SQL表中使用的字段如下:

standings_id, team_name, goals_for, goals_away, goal_difference, points

代码:

<?
$objConnect =mysql_connect('localhost', 'username', 'password') or die(mysql_error());
$objDB = mysql_select_db("database");

//*** Add Condition ***//
if($_POST["hdnCmd"] == "Add")
{
    $strSQL = "INSERT INTO division1_standings_birmingham";
    $strSQL .="(standings_id,team_name,goals_for,goals_away,goal_difference,points) ";
    $strSQL .="VALUES ";
    $strSQL .="('".$_POST["txtAddstandings_id"]."','".$_POST["txtAddteam_name"]."' ";
    $strSQL .=",'".$_POST["txtAddgoals_for"]."' ";
    $strSQL .=",'".$_POST["txtAddgoals_away"]."','".$_POST["txtAddgoal_difference"]."' ";
    $strSQL .=",'".$_POST["txtAddpoints"]."') ";
    $objQuery = mysql_query($strSQL);
    if(!$objQuery)
    {
        echo "Error Save [".mysql_error()."]";
    }
    //header("location:$_SERVER[PHP_SELF]");
    //exit();
}

//*** Update Condition ***//
if($_POST["hdnCmd"] == "Update")
{
    $strSQL = "UPDATE division1_standings_birmingham SET ";
    $strSQL .="standings_id = '".$_POST["txtEditstandings_id"]."' ";
    $strSQL .=",team_name = '".$_POST["txtEditteam_name"]."' ";
    $strSQL .=",goals_for = '".$_POST["txtEditgoals_for"]."' ";
    $strSQL .=",goals_away = '".$_POST["txtEditgoals_away"]."' ";
    $strSQL .=",goal_difference = '".$_POST["txtEditgoal_difference"]."' ";
    $strSQL .=",points = '".$_POST["txtEditpoints"]."' ";
    $strSQL .="WHERE standings_id = '".$_POST["hdnEditid"]."' ";
    $objQuery = mysql_query($strSQL);
    if(!$objQuery)
    {
        echo "Error Update [".mysql_error()."]";
    }
    //header("location:$_SERVER[PHP_SELF]");
    //exit();
}

//*** Delete Condition ***//
if($_GET["Action"] == "Del")
{
    $strSQL = "DELETE FROM division1_standings_birmingham";
    $strSQL .="WHERE standings_id = '".$_GET["standings_id"]."' ";
    $objQuery = mysql_query($strSQL);
    if(!$objQuery)
    {
        echo "Error Delete [".mysql_error()."]";
    }
    //header("location:$_SERVER[PHP_SELF]");
    //exit();
}

$strSQL = "SELECT * FROM division1_standings_birmingham";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
?> <br>Welcome, here you can Add/Edit/Delete your records or 
<a href="index.php">click here</a> to go back to main page.<br><br>


<form name="frmMain" method="post" action="<?=$_SERVER["PHP_SELF"];?>">
<input type="hidden" name="hdnCmd" value="">
<table width="600" border="1">
  <tr>
    <th width="91"> <div align="center">standings_id </div></th>
    <th width="98"> <div align="center">team_name </div></th>
    <th width="198"> <div align="center">goals_for </div></th>
    <th width="97"> <div align="center">goals_away </div></th>
    <th width="59"> <div align="center">goal_difference </div></th>
    <th width="71"> <div align="center">points </div></th>
    <th width="30"> <div align="center">Edit </div></th>
    <th width="30"> <div align="center">Delete </div></th>
  </tr>
<?
while($objResult = mysql_fetch_array($objQuery))
{
?>

  <?
    if($objResult["standings_id"] == $_GET["standings_id"] and $_GET["Action"] == "Edit")
    {
  ?>
  <tr>
    <td><div align="center">
        <input type="text" name="txtEditid" size="5" value="<?=$objResult["standings_id"];?>">
        <input type="hidden" name="hdnEditid" size="5" value="<?=$objResult["standings_id"];?>">
    </div></td>
    <td><input type="text" name="txtEditteam_name" size="20" value="<?=$objResult["team_name"];?>"></td>
    <td><input type="text" name="txtEditgoals_for" size="20" value="<?=$objResult["goals_for"];?>"></td>
    <td><div align="center"><input type="text" name="txtEditgoals_away" size="2" value="<?=$objResult["goals_away"];?>"></div></td>
    <td align="right"><input type="text" name="txtEditgoal_difference" size="5" value="<?=$objResult["goal_difference"];?>"></td>
    <td align="right"><input type="text" name="txtEditpoints" size="5" value="<?=$objResult["points"];?>"></td>


    <td colspan="2" align="right"><div align="center">
      <input name="btnAdd" type="button" id="btnUpdate" value="Update" OnClick="frmMain.hdnCmd.value='Update';frmMain.submit();">
      <input name="btnAdd" type="button" id="btnCancel" value="Cancel" OnClick="window.location='<?=$_SERVER["PHP_SELF"];?>';">
    </div></td>
  </tr>
  <?
    }
  else
    {
  ?>
  <tr>
    <td><div align="center"><?=$objResult["standings_id"];?></div></td>
    <td><?=$objResult["team_name"];?></td>
    <td><?=$objResult["goals_for"];?></td>
    <td><div align="center"><?=$objResult["goals_away"];?></div></td>
    <td align="right"><?=$objResult["goal_difference"];?></td>
    <td align="right"><?=$objResult["points"];?></td>

    <td align="center"><a href="<?=$_SERVER["PHP_SELF"];?>?Action=Edit&standings_id=<?=$objResult["standings_id"];?>">Edit</a></td>
    <td align="center"><a href="JavaScript:if(confirm('Confirm Delete?')==true){window.location='<?=$_SERVER["PHP_SELF"];?>?Action=Del&standings_id=<?=$objResult["standings_id"];?>';}">Delete</a></td>
  </tr>
  <?
    }
  ?>
<?
}
?>
  <tr>
    <td><div align="center"><input type="text" name="txtAddstandings_id" size="5"></div></td>
    <td><input type="text" name="txtAddteam_name" size="20"></td>
    <td><input type="text" name="txtAddgoals_for" size="20"></td>
    <td><div align="center"><input type="text" name="txtAddgoals_away" size="2"></div></td>
    <td align="right"><input type="text" name="txtAddgoal_difference" size="5"></td>
    <td align="right"><input type="text" name="txtAddpoints" size="5"></td>

    <td colspan="2" align="right"><div align="center">
      <input name="btnAdd" type="button" id="btnAdd" value="Add" OnClick="frmMain.hdnCmd.value='Add';frmMain.submit();">
    </div></td>
  </tr>
</table>
</form>
<?
mysql_close($objConnect);
?>

1 个答案:

答案 0 :(得分:-1)