Insert, Edit, Delete and view form problem [MySQL and PHP]

Hello, I am new to this forum and my name is Maciej. I am not bad with webdesign stuff but I’m having a problem with an php code I have done according to tutorial I found. The code was fully working when it was in its standard form(same as in tutorial, but when I added more VALUES to be added its not working correctly anymore.)

Here is the whole code I have:
[php]

<?php $objConnect = mysql_connect("localhost","user","password") or die(mysql_error()); $objDB = mysql_select_db("database"); if($_POST["hdnCmd"] == "Add") { $strSQL = "INSERT INTO reports_nata "; $strSQL .="(callsign,destination,selcal,fl,mach,wpt,wptata,nextwpt,nextwpteta,next) "; $strSQL .="VALUES "; $strSQL .="('".$_POST["txtAddCallsign"]."','".$_POST["txtAddDestination"]."' "; $strSQL .=",'".$_POST["txtAddSelcal"]."' "; $strSQL .=",'".$_POST["txtAddFL"]."','".$_POST["txtAddMach"]."' "; $strSQL .=",'".$_POST["txtAddWpt"]."' "; $strSQL .=",'".$_POST["txtAddWptAta"]."','".$_POST["txtNextWpt"]."' "; $strSQL .=",'".$_POST["txtAddNextWptEta"]."' "; $strSQL .=",'".$_POST["txtAddNext"]."') "; $objQuery = mysql_query($strSQL); if(!$objQuery) { echo "Error Save [".mysql_error()."]"; } //header("location:$_SERVER[PHP_SELF]"); //exit(); } if($_POST["hdnCmd"] == "Update") { $strSQL = "UPDATE reports_nata SET "; $strSQL .=",callsign = '".$_POST["txtEditCallsign"]."',destination = '".$_POST["txtEditDestination"]."' "; $strSQL .=",fl = '".$_POST["txtEditFL"]."',mach = '".$_POST["txtEditMach"]."' "; $strSQL .=",wpt = '".$_POST["txtEditWpt"]."' "; $strSQL .=",wptata = '".$_POST["txtEditWptAta"]."',nextwpt = '".$_POST["txtEditNextWpt"]."' "; $strSQL .=",nextwpteta = '".$_POST["txtEditNextWptEta"]."' "; $strSQL .=",next = '".$_POST["txtEditNext"]."' "; $strSQL .="WHERE callsign = '".$_POST["hdnEditCallsign"]."' "; $objQuery = mysql_query($strSQL); if(!$objQuery) { echo "Error Update [".mysql_error()."]"; } //header("location:$_SERVER[PHP_SELF]"); //exit(); } if($_GET["Action"] == "Del") { $strSQL = "DELETE FROM reports_nata "; $strSQL .="WHERE callsign = '".$_GET["callsign"]."' "; $objQuery = mysql_query($strSQL); if(!$objQuery) { echo "Error Delete [".mysql_error()."]"; } //header("location:$_SERVER[PHP_SELF]"); //exit(); } $strSQL = "SELECT * FROM reports_nata"; $objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]"); ?>
<h1>Reports</h1>
<form name="frmMain" method="post" action="<?=$_SERVER["PHP_SELF"];?>">
<input type="hidden" name="hdnCmd" value="">
<table width="950" border="1">
  <tr>
    <td width="64">Callsign</td>
    <td width="72">Destination</td>
    <td width="61">SELCAL</td>
    <td width="36">FL</td>
    <td width="47">Mach</td>
    <td width="89">Current Wpt.</td>
    <td width="77">Wpt. ATA</td>
    <td width="71">Next Wpt.</td>
    <td width="103">Next Wpt. ETA</td>
    <td width="112">Wpt. Therafter</td>
    <td width="84">Edit</td>
    <td width="84">Delete</td>
  </tr>
<?
while($objResult = mysql_fetch_array($objQuery))
{
?>

<?
if($objResult["callsign"] == $_GET["callsign"] and $_GET["Action"] == "Edit")
{
?>
   <tr>
   
    <div align="center">
	    <td>
		<input type="text" name="txtEditCallsign" size="6" value="<?=$objResult["callsign"];?>">
		<input type="hidden" name="hdnEditCallsign" size="5" value="<?=$objResult["callsign"];?>">
	    </td>
	    <td><input type="text" name="txtEditDestination" size="6" value="<?=$objResult["destination"];?>"></td>
	    <td><input type="text" name="txtEditSelcal" size="6" value="<?=$objResult["selcal"];?>"></td>
	    <td><input type="text" name="txtEditFL" size="6" value="<?=$objResult["fl"];?>"></td>
	    <td><input type="text" name="txtEditMach" size="6" value="<?=$objResult["mach"];?>"></td>
	    <td><input type="text" name="txtEditWpt" size="6" value="<?=$objResult["wpt"];?>"></td>
	    <td><input type="text" name="txtEditWptAta" size="6" value="<?=$objResult["wptata"];?>"></td>
	    <td><input type="text" name="txtEditNextWpt" size="6" value="<?=$objResult["nextwpt"];?>"></td>
	    <td><input type="text" name="txtEditNextWptEta" size="6" value="<?=$objResult["nextwpteta"];?>"></td>
	    <td><input type="text" name="txtEditNext" size="6" value="<?=$objResult["next"];?>"></td>
	    <td><input name="btnAdd" type="button" id="btnUpdate" value="Update" OnClick="frmMain.hdnCmd.value='Update';frmMain.submit();"></td>
	    <td><input name="btnAdd" type="button" id="btnCancel" value="Cancel" OnClick="window.location='<?=$_SERVER["PHP_SELF"];?>';"></td>
    </div>
  </tr>
  <?php
	}
  else
	{
  ?>
  <tr>
    <td><?=$objResult["callsign"];?></td>
    <td><?=$objResult["destination"];?></td>
    <td><?=$objResult["selcal"];?></td>
    <td><?=$objResult["fl"];?></td>
    <td><?=$objResult["mach"];?></td>
    <td><?=$objResult["wpt"];?></td>
    <td><?=$objResult["wptata"];?></td>
    <td><?=$objResult["nextwpt"];?></td>
    <td><?=$objResult["nextwpteta"];?></td>
    <td><?=$objResult["next"];?></td>
    <td><a href="<?=$_SERVER["PHP_SELF"];?>?Action=Edit&callsign=<?=$objResult["callsign"];?>">Edit</a></td>
    <td><a href="JavaScript:if(confirm('Confirm Delete?')==true){window.location='<?=$_SERVER["PHP_SELF"];?>?Action=Del&callsign=<?=$objResult["callsign"];?>';}">Delete</a></td>
  </tr>
  <?
	}
  }
  ?>
  <tr>
  
    <td><input type="text" name="txtAddCallsign" size="6"></td>
    <td><input type="text" name="txtAddDestiantion" size="6"></td>
    <td><input type="text" name="txtAddSelcal" size="6"></td>
    <td><input type="text" name="txtAddFL" size="6"></td>
    <td><input type="text" name="txtAddMach" size="6"></td>
    <td><input type="text" name="txtAddWpt" size="6"></td>
    <td><input type="text" name="txtAddWptAta" size="6"></td>
    <td><input type="text" name="txtAddNextWpt" size="6"></td>
    <td><input type="text" name="txtAddNextWptEta" size="6"></td>
    <td><input type="text" name="txtAddNext" size="6"></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);
?>

[/php]

Here is the live preview: http://www.maciejolborski.net/oceanic/reports.php
So delete function works correctly, but When I try to edit it gives me an error that I dont understand and dont know how to repair:
Error Update [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 ‘callsign = ‘BAW213’,destination = ‘1’ ,fl = ‘390’,mach = ‘.84’ ,wpt = ‘DOGAL’ ,w’ at line 1]

Also when I add records into the database through this form, Destination and Next Wpt. wont add and will show blank.

I will be really thankful for the help!
Maciej.

Anyone :frowning: ?

Try echo’ing out the update query to see how it looks with the variables populated…

[php]
//after $strSQL .=“WHERE callsign = '”.$_POST[“hdnEditCallsign”]."’ ";
echo $strSQL;
[/php]

[php]
UPDATE reports_nata SET ,callsign = ‘1’,destination = ‘2’ ,fl = ‘4’,mach = ‘5’ ,wpt = ‘6’ ,wptata = ‘7’,nextwpt = ‘8’ ,nextwpteta = ‘9’ ,next = ‘10’ WHERE callsign = ‘1’ Error Update [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 ‘callsign = ‘1’,destination = ‘2’ ,fl = ‘4’,mach = ‘5’ ,wpt = ‘6’ ,wptata = ‘7’,n’ at line 1]
[/php]

you have a comma after SET, remove that from your code to start with.

Cheers, edit is now working correctly, add is still not adding Destination and Next Wpt.

Maciej.

I’ve added same echo to print variables in the add:

[php]INSERT INTO reports_nata (callsign,destination,selcal,fl,mach,wpt,wptata,nextwpt,nextwpteta,next) VALUES (‘21’,’’ ,‘23’ ,‘24’,‘25’ ,‘26’ ,‘27’,’’ ,‘29’ ,‘30’)[/php]

i would suggest changing to something like this, its cleaner and less confusing to find bugs in your code

[php]
if($_POST[“hdnCmd”] == “Add”) {
$value1 = $_POST[‘txtAddCallsign’];
$value2 = $_POST[‘txtAddDestination’];
$value3 = $_POST[‘txtAddSelcal’];
$value4 = $_POST[‘txtAddFL’];
$value5 = $_POST[‘txtAddMach’];
$value6 = $_POST[‘txtAddWpt’];
$value7 = $_POST[‘txtAddWptAta’];
$value8 = $_POST[‘txtNextWpt’];
$value9 = $_POST[‘txtAddNextWptEta’];
$value10 = $_POST[‘txtAddNext’];

$strSQL = “INSERT INTO reports_nata (callsign,destination,selcal,fl,mach,wpt,wptata,nextwpt,nextwpteta,next) values ($value1, $value2, $value3, $value4, $value5, $value6, $value7, $value8, $value9, $value10)”;

$objQuery = mysql_query($strSQL);

}
[/php]

try something like that, if those 2 dont work you may have the wrong spelling for the $_POST’s

yeah looks like your $_POST[‘txtAddDestination’] and $_POST[‘txtNextWpt’] may be wrong, otherwise it was because of the layout of your old code

Your code given me diffrent error, but I’m slowly getting there, I looked through the whole code again and finded destination was misspelled in the form, now only Next.Wpt, everything seems correct, code for Add looks:
[php]
if($_POST[“hdnCmd”] == “Add”) {

		$strSQL = "INSERT INTO reports_nata ";
		$strSQL .="(callsign,destination,selcal,fl,mach,wpt,wptata,nextwpt,nextwpteta,next) ";
		$strSQL .="VALUES ";
		$strSQL .="('".$_POST["txtAddCallsign"]."','".$_POST["txtAddDestination"]."' ";
		$strSQL .=",'".$_POST["txtAddSelcal"]."' ";
		$strSQL .=",'".$_POST["txtAddFL"]."','".$_POST["txtAddMach"]."' ";
		$strSQL .=",'".$_POST["txtAddWpt"]."' ";
		$strSQL .=",'".$_POST["txtAddWptAta"]."','".$_POST["txtNextWpt"]."' ";
		$strSQL .=",'".$_POST["txtAddNextWptEta"]."' ";
		$strSQL .=",'".$_POST["txtAddNext"]."') ";
		echo $strSQL;
		$objQuery = mysql_query($strSQL);
		if(!$objQuery)
		{
			echo "Error Save [".mysql_error()."]";
		}
		//header("location:$_SERVER[PHP_SELF]");
		//exit();
		}

[/php]

And printed:
[php]INSERT INTO reports_nata (callsign,destination,selcal,fl,mach,wpt,wptata,nextwpt,nextwpteta,next) VALUES (‘2’,‘3’ ,‘4’ ,‘5’,‘6’ ,‘7’ ,‘8’,’’ ,‘10’ ,‘00’)[/php]

Okey I’ve got add missing there thats why it wasn’t working, thank you all for the help! :slight_smile:

Maciej.

Sponsor our Newsletter | Privacy Policy | Terms of Service