Jump to content

Updating MySQL data VIA a form


josephman1988

Recommended Posts

Ok guys, so I have a form where i want to edit and change the data within that field.

 

Here's the code I have so far:

<h1>Edit Operation</h1>
<hr />
Editing Operation: <span class="subhead"><?php print $operation['name']; ?></span>
</div>

<!-- ADMIN BOX 1 -->

<div id="adminbox1">
<?php

$doid = $_GET['oid'];
$sql = @mysql_query("SELECT * FROM operation_data");

while ($odata = mysql_fetch_array($sql))
{
?>
<form action='<?php $_SERVER['PHP_SELF']; ?>' method='post'>
Case Name:<br />
<input type='text' name='name' value='<?php print $odata['name']; ?>' />
Case Briefing:<br />
<textarea cols='50' rows='10' name='briefing'><?php print $odata['briefing']; ?></textarea>
<input type="submit" value="Edit Operation" />
        </form>
<?php
}
$upnamenew = $_POST['name'];
$upbriefingnew = $_POST['briefing'];

print $upnamenew;
print $upbriefingnew;

 

Up to this point, I 'believe' I have succeeded as printing the results give me the newly entered fields.

HOWEVER, the update query just doesn't update.

 

$sql2 = @mysql_query("UPDATE FROM operation_data SET name = $upnamenew, briefing = $upbriefingnew WHERE oid = $doid");

 

I get a syntax error about this paticular query, with the new data added:

UPDATE FROM operation_data SET name = bla, briefing = $blahmore WHERE oid = 1"

 

And the data in the operation table:

-----------------------

| oid | name | briefing |

-----------------------

Doesn't update.

 

Any help would be great.

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/120077-updating-mysql-data-via-a-form/
Share on other sites

if u are asking mysql related questions you must do 2 things before anyone can help you

 

1) Do not suppress the error on the function via the @ symbol (This holds true for any function you think is failing)

2) Use the or die clause on a query ($q = "Select * from `table`"; $r = mysql_query($q) or die(mysql_error()."<Br /><br />\n\n".$q);)

3) Display the error if there is one.

 

 

Do this then post back

Thankyou for the quick reply.

 

After editing the lines you suggested to change, and adding it into an if statement:

if (isset($upnamenew, $upbriefingnew))
{
$sql2 = mysql_query("UPDATE FROM operation_data SET name = $upnamenew, briefing = $upbriefingnew WHERE 'oid' = $doid") or die(mysql_error() . $sql2);
}

 

Displays the following after submitting the form:

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 'FROM operation_data SET name = change title, briefing = change briefing WHERE 'o' at line 1

Change

UPDATE FROM operation_data SET name = $upnamenew, briefing = $upbriefingnew WHERE oid = $doid

 

to

 

UPDATE operation_data SET name = $upnamenew, briefing = $upbriefingnew WHERE oid = $doid

 

Always check mysql statement syntax section in mySQL docs (section 12) ;)

http://dev.mysql.com/doc/refman/5.0/en/sql-syntax.html

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.