Jump to content

[SOLVED] Change/update MYSQL table contents via html form or something? How?


cs.punk

Recommended Posts

Ok I got a form that adds contents to a MYSQL table. But now I want one that updates/changes within the table. I used a HTML form and got something like change 'amount?'' WHERE '$_POST' = '$_POST' ... I just confused myself now... Nevermind that coding... But it does not work where I try to retrieve the forms (posts?)? But using a form is a bad thing right? How else can I change the contents within a MYSQL table?...

HTML form's aren't necessarily bad if you want to control what's modified, but you need to be careful with security. What you need to look into are MySQL queries and PHP. A simple UPDATE query performed in PHP:

 

<?php

// assuming you've connected to the database

$query = mysql_query("UPDATE numbers SET num + 1");

?>

 

I can't ever think of a reason why that query would be helpful but, illustrates the point. "numbers" is the table name, "num" is the field.

 

http://dev.mysql.com/doc/refman/5.0/en/update.html

 

EDIT: Forgot to mention that when you see values in the URL such as "?this=xx&that=yy" .. You return their value in PHP with:

 

echo $_GET['this'];
echo $_GET['that'];

 

Lemme' know if you get stuck!

 

A

Uhm here the form is:

 


<html>
<body>
<form action="update.php" method="post">
Item: <input type="text" name="item" />
Amount: <input type="text" name="amount" />
<input type="submit" />
</form>
</body>
</html>

 

Now on the receivng end

<?php
$con = mysql_connect("localhost","root","sorry removed lol");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("stock", $con);

mysql_query("UPDATE stock SET amount = ''$_POST[amount]''
WHERE item = '$_POST[item);
mysql_close($con);
?>

 

Some of your inverted commas seem to be a bit awry, try this and see if it works any better, pay special attention to single versus double commas.

 

mysql_query("UPDATE stock SET amount = '$_POST[amount]' WHERE item = '$_POST[item]' ");

 

Let me know if it's any warmer ..

LoL check it over and over. And I finally figured out the problem.

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

mysql_query(" UPDATE stuff SET Amount = ('$_POST[amount]')
WHERE Item = ('$_POST[item]') ");
mysql_close($con);
?>

 

All it needed was () brackets for the

'$_POST[example]'

.

 

 

Thanks to everyone!

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.