Jump to content

Recommended Posts

Hi!

 

I'm having problems updating mySQL database with an HTML POST form I'm new in PHP so I can't find out what is the problem...Thanks for you help!!

 

The form part is working and the data fetch in the form correctly from mySQL database

Here's the HTML update form:

 

<?php
$id=$_POST["modif"];
$db = mysql_connect('localhost', 'user', 'pass'); 


mysql_select_db('database',$db); 


$sql = "SELECT `id`, `firstname`, `lastname` FROM table WHERE id='$id'"; 


$req = mysql_query($sql) or die('SQL Error !<br>'.$sql.'<br>'.mysql_error()); 


while($data = mysql_fetch_assoc($req)) 
     {
echo "
<center>
Modify
<table border=0 style=\"border: 1px solid #A7883F\" width=85% cellpadding=0 cellspacing=0>
<form action=modify.php method=post>
<tr>
<td colspan=4><center><u>Informations</u></center><br></td>
</tr>
<tr>
<td colspan=4>
ID :<input type=text value=\"$data[id]\">
</td>
</tr>
<tr>
<td align=right width=25%>First name :</td>
<td width=25%><input name=firstname value=\"$data[firstname]\" type=text size=20 MAXLENGTH=40></td>
<td align=right width=25%>Lastname :</td>
<td width=25%><input name=lastname value=\"$data[lastname]\" type=text size=20 MAXLENGTH=40></td>
</tr>
<tr>
<td colspan=4>
<center><br><input type=submit value=Save></center>
</td>
</tr>
</center>
</form>
</table>
    ";
    }
mysql_close(); 
?>

 

 

Here the part that isn't working...

 

modify.php :

 

<?php
$con = mysql_connect("localhost","user","pass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("database", $con);

mysql_query(" UPDATE table SET firstname = ('$_POST[firstname]'), lastname = ('$_POST[lastname]')
WHERE id = ('$_POST[id]')");
echo "Update Sucessful";
mysql_close($con);
?>

 

If someone could tell me what i'm doing wrong it woulb be really appreciated! Thanks!!

 

(edited to change the tags to


tags)

mysql_close is not needed. Just an fyi.

 

Your issue:

 

mysql_query(" UPDATE table SET firstname = '{$_POST['firstname']}', lastname = '{$_POST['lastname']}'
WHERE id = '{$_POST['id']}'");

 

With SQL Injection issues I would highly advise escaping the post data before inserting into a database. But that should solve your problem.

 

If you would have done this:

 

mysql_query(" UPDATE table SET firstname = '{$_POST['firstname']}', lastname = '{$_POST['lastname']}'
WHERE id = '{$_POST['id']}'") or die(mysql_error());

 

The error would have showed up to you.

echo $_POST['id'] out and see if that contains a value I doubt it does cause of this:

 

ID :<input type=text value=\"$data[id]\">

 

Should be

ID :<input type=text name=id value=\"$data[id]\">

 

That should do the update.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.