Jump to content

Updating a table


Darim Design

Recommended Posts

Hi, learning php for the first time and the tutorials went smoothly, but I've got a little problem.I'm making a sort of wiki where people can edit a text on one of my pages, just a simple pure text. I've managed to set up a database and a table for it, and sending data to the table and then showing it on a different page works. But what I want is to update the data already in the table and not add a new line of data like the php tutorial does. So how can I do that? (link to the right tutorial would be nice aswell)Also, I would like the text that presently is in the table to be in the box you write in your edit. Pretty much like a wiki. How can I do these two things?I've tried changing 'PUT INTO' to 'UPDATE INTO' but that didn't work :)Any help apreciated!

Link to comment
Share on other sites

No I've read both insert and update. For the coding I have now it is the same as the tutorial except other names for databases and tables. From input to insert and then it saves in the table, and then it is displayed on another page. What I need is the input to overwrite data already in the table, rather than making a new post like a guest book. The update automatically updates the table, but I want others to be able to do that through the input. What I don't know is the right tags etc to do this.

Link to comment
Share on other sites

UPDATE is correct. You can capture the form's input in the same way you do for INSERT. For the form, you would use the form tags, <form>, <input>, <textarea>.What code to dyou have currently?

Link to comment
Share on other sites

<?php$con = mysql_connect("*******","******","******");if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("hyboria", $con);$sql="INSERT INTO vanaheim (Name, Info)VALUES('$_POST[name]','$_POST[info]')";if (!mysql_query($sql,$con))  {  die('Error: ' . mysql_error());  }echo "1 record added";mysql_close($con)?>

Link to comment
Share on other sites

If you want that other put a message in you database then you should have a public database (for guests ). Of course you need only to construct the design of your guest book .You could use a <form> for updating the database .Hope this can help you , and if do not understand , please let me know .

Link to comment
Share on other sites

No I don't get it :) But here's the code..Input:

<form action="insert.php" method="post">Name: <input type="text" name="name" />Info: <input type="text" name="info" /><input type="submit" /></form>

Insert:

<?php$con = mysql_connect("****","****","****");if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("hyboria", $con);$sql="INSERT INTO vanaheim (Name, Info)VALUES('$_POST[name]','$_POST[info]')";if (!mysql_query($sql,$con))  {  die('Error: ' . mysql_error());  }echo "1 record added";mysql_close($con)?>

Display:

<?php$con = mysql_connect("****","****","****");if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("hyboria", $con);$result = mysql_query("SELECT * FROM vanaheim");echo "<table border='1'><tr><th>Name</th><th>Info</th></tr>";while($row = mysql_fetch_array($result))  {  echo "<tr>";  echo "<td>" . $row['Name'] . "</td>";  echo "<td>" . $row['Info'] . "</td>";  echo "</tr>";  }echo "</table>";mysql_close($con);?>

Link to comment
Share on other sites

Insert.php

<?php$con = mysql_connect("****","****","****");if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("hyboria", $con);$sql="INSERT INTO vanaheim (Name, Info)VALUES('$_POST[name]','$_POST[info]')";if (!mysql_query($sql,$con))  {  die('Error: ' . mysql_error());  }echo "1 record added";include('Display.php'); //I hope that Display.php is in root directory mysql_close($con)?><html><body><form action="insert.php" method="post">Name: <input type="text" name="name" />Info: <input type="text" name="info" /><input type="submit" /></form></body></html>

I hope this can help you .

Link to comment
Share on other sites

To erase other posts or edit it , like w3school did it you will need to learn more .divs and css are standard , you will need design for your guest book .OR try asking some more experienced programmer .

Link to comment
Share on other sites

No no no, it's not a guest book! It's supposed to be a wiki, a really simple one. There is only going to be one piece of info that others can edit. I don't want to use wikimedia and such because it's too big and pointless with all the pictures and buttons etc. I thought I made that clear enough but ohh well :)

Link to comment
Share on other sites

When you submit something I want it to overwrite the things already there, like if you were editing a post.
What are you trying to edit? If someone types in the same value for "name" then you want to update the record in the database with that name?UPDATE table SET info=$info WHERE name=$nameThat's the specific statement to update, if that's what you want to do. But before you do that you would need to use a SELECT statement to check if a record exists at all with that name, if so then do an update and if not then do an insert. You'll also want to use more fields in your database, you'll at least want an autonumber ID and maybe some other stuff. Wikipedia also keeps track of what changes were made, you might want to do something like that also.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...