How would you do this? (add, edit, delete table data in PHP)

Soldato
Joined
10 May 2004
Posts
3,790
Location
East Yorkshire, UK
Hi

I have created a PHP admin area, and would like to edit, add and delete PHP data that is held within a mysql table. However everything I find on the internet involves using a SQL query command in PHPmyadmin, I cannot find how you would do this from within a website. Any ideas?

Cheers!
 
Do you mean you want to allow the admin to edit the table contents directly? If so, this is exactly what phpMyAdmin is designed for, so you may as well use it.
 
If you wanted to do it from the website, you could create a form on the webpage and include some php containing mysql queries. Make sure the webpage is saved as .php if it contains any php code.

Something like this would add the field the user inputted to the database

<form action="phpfile.php" method="post"
<input type="text" name="name" />
<input type="text" name="age" />
<input type="submit" />
</form>

PHP:
<?php

$con = mysql_connect("hostname","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("databasename", $con);

$sql="INSERT INTO mytable (name, age )
VALUES
('$_POST[name]'), '$_POST[age]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "Text to output to user";

mysql_close($con)

?>
 
If you wanted to do it from the website, you could create a form on the webpage and include some php containing mysql queries. Make sure the webpage is saved as .php if it contains any php code.

Something like this would add the field the user inputted to the database

<form action="phpfile.php" method="post"
<input type="text" name="name" />
<input type="text" name="age" />
<input type="submit" />
</form>

PHP:
<?php

$con = mysql_connect("hostname","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("databasename", $con);

$sql="INSERT INTO mytable (name, age )
VALUES
('$_POST[name]'), '$_POST[age]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "Text to output to user";

mysql_close($con)

?>

This code is not safe/secure.
 
As you are learning i would highly recommend you implement safe and secure code FROM THE START. Learn how to make your code secure as a priority. :)
 
Back
Top Bottom