Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Simple MySQL Table Edito

Featured Replies

Hello all, and thanks for what I've learnt so far from just reading various topics.

I'm still learning, I refuse to belive that I will ever stop learning with PHP. I've been following a tutorial HERE and I've built it all, I can't work out how to add another column from the sql database, I know how to add a coloumn through PHPmyadmin, But I can't seem to bend this script to how I want to. Can anyone help?

Thanks In advance

Heidi SQL - even makes editing stored procedures easier, iirc phpmyadmin doesn't do this.

I like it :o

Too bad my host doesn't allow remote connections :(

Edited by Artaex

Nice ideas, but I only need to edit one table, not a database. Any ideas how I add columns?

 

So the question is how to add a column to a table in a MySql database using PHP?

No! The opposite. The SQL table has the extra column, I need to add a column to the PHP script, It's doing my head in, I keep Mudling it up!

 

This is what I did (be warned, I am a PHP/MySql newbie myself):

 

Added a column to table 1 ('column6 - varchar(50)):

 


CREATE TABLE `table1` (
 `column1` varchar(50) NOT NULL,
 `column2` varchar(50) NOT NULL,
 `column3` varchar(50) NOT NULL,
 `column4` varchar(50) NOT NULL,
 `column5` varchar(50) NOT NULL,
 `column6` varchar(50) NOT NULL,
 PRIMARY KEY (`column1`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1

 

Changed Index.php, Update.php and Add.php to display column6

 

Index.php:

 


//Table header
echo "<div><table id=\"tableheader\" bgcolor=\"#4382b5\">\n";
echo "<tr>\n";
echo "<td> Column 1:</td>\n";
echo "<td> Column 2:</td>\n";
echo "<td> Column 3:</td>\n";
echo "<td> Column 4:</td>\n";
echo "<td> Column 5:</td>\n";
echo "<td> Column 6:</td>\n";
echo "<tr>";
echo "</table></div>";
if ($count !== 0) {
		while($row = mysql_fetch_array($result)) {
			echo "<div class=\"addform\"><form method='get' action=\"update.php\">\n";
			echo "	<input type=\"text\" value=\"".$row['column1']."\" name=\"column1\">\n";
			echo "	<input type=\"text\" name=\"column2\" value=\"".$row['column2']."\"/>\n";
			echo "	<input type=\"text\" name=\"column3\" value=\"".$row['column3']."\"/>\n";
			echo "	<input type=\"text\" name=\"column4\" value=\"".$row['column4']."\"/>\n";
			echo "	<input type=\"text\" name=\"column5\" value=\"".$row['column5']."\"/>\n";
			echo "	<input type=\"text\" name=\"column6\" value=\"".$row['column6']."\"/>\n";
			echo "	<input type=\"image\" src=\"images/update.png\" alt=\"Update Row\" class=\"update\" title=\"Update Row\">\n";
			echo "<a href=\"delete.php?column1=".$row['column1']."\"><img title='Delete Row' alt=\"Delete\" class='del' src='images/delete.png'/></a></form></div>\n";
		}
	echo "</table><br />\n";
} else {
	echo "<b><center>NO DATA</center></b>\n";
}
echo "<div>Add Row:</div>\n";
echo "<div class=\"addform\"><form method='get' action=\"add.php\">\n".
"	<input type=\"text\" name=\"column1\"/>\n".
"	<input type=\"text\" name=\"column2\"/>\n".
"	<input type=\"text\" name=\"column3\"/>\n".
"	<input type=\"text\" name=\"column4\"/>\n".
"	<input type=\"text\" name=\"column5\"/>\n".
"   <input type=\"text\" name=\"column6\"/>\n".
"	<input type=\"image\" src=\"images/add.png\" alt=\"Add Row\" class=\"update\" title=\"Add Row\">\n".
"</form></div>";

 

 

Add.php:

 


//Get database credentials
require 'config.php';

$column1 = $_GET['column1'];
$column1 = $_GET['column1'];
$column2 = $_GET['column2'];
$column3 = $_GET['column3'];
$column4 = $_GET['column4'];
$column5 = $_GET['column5'];
$column6 = $_GET['column6'];

// connect to the mysql database server.
mysql_connect ($dbhost, $dbusername, $dbuserpass);
//select the database
mysql_select_db($dbname) or die('Cannot select database');

$query = "INSERT INTO `table1` ( `column1` , `column2` , `column3` , `column4` , `column5` , `column6` )" .
"VALUES ('".$column1."', '".$column2."', '".$column3."', '".$column4."', '".$column5."', '".$column6."');";


 

 

Update.php:

 


$column1 = $_GET['column1'];
$column2 = $_GET['column2'];
$column3 = $_GET['column3'];
$column4 = $_GET['column4'];
$column5 = $_GET['column5'];
$column6 = $_GET['column6'];

// connect to the mysql database server.
mysql_connect ($dbhost, $dbusername, $dbuserpass);
//select the database
mysql_select_db($dbname) or die('Cannot select database');

$query = "UPDATE table1" .
" SET column2 = '".$column2."'," .
" column3 = '".$column3."'," .
" column4 = '".$column4."'," .
" column5 = '".$column5."'," .
" column6 = '".$column6."'" .
" WHERE column1 = '".$column1."'";

 

In Style.css, increased table and .addform width.

  • Author

Newby or not, you've provided help as opposed to "give up, try something else" lol(please guys dont take that the wrong way, I know you were helping really ;) ), but bid it work? I'm going to try it now and see how I get on, I will let you know the results

This is what I did (be warned, I am a PHP/MySql newbie myself):

 

Added a column to table 1 ('column6 - varchar(50)):

 


CREATE TABLE `table1` (
 `column1` varchar(50) NOT NULL,
 `column2` varchar(50) NOT NULL,
 `column3` varchar(50) NOT NULL,
 `column4` varchar(50) NOT NULL,
 `column5` varchar(50) NOT NULL,
 `column6` varchar(50) NOT NULL,
 PRIMARY KEY (`column1`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1

 

Changed Index.php, Update.php and Add.php to display column6

 

Index.php:

 


//Table header
echo "<div><table id=\"tableheader\" bgcolor=\"#4382b5\">\n";
echo "<tr>\n";
echo "<td> Column 1:</td>\n";
echo "<td> Column 2:</td>\n";
echo "<td> Column 3:</td>\n";
echo "<td> Column 4:</td>\n";
echo "<td> Column 5:</td>\n";
echo "<td> Column 6:</td>\n";
echo "<tr>";
echo "</table></div>";
if ($count !== 0) {
		while($row = mysql_fetch_array($result)) {
			echo "<div class=\"addform\"><form method='get' action=\"update.php\">\n";
			echo "	<input type=\"text\" value=\"".$row['column1']."\" name=\"column1\">\n";
			echo "	<input type=\"text\" name=\"column2\" value=\"".$row['column2']."\"/>\n";
			echo "	<input type=\"text\" name=\"column3\" value=\"".$row['column3']."\"/>\n";
			echo "	<input type=\"text\" name=\"column4\" value=\"".$row['column4']."\"/>\n";
			echo "	<input type=\"text\" name=\"column5\" value=\"".$row['column5']."\"/>\n";
			echo "	<input type=\"text\" name=\"column6\" value=\"".$row['column6']."\"/>\n";
			echo "	<input type=\"image\" src=\"images/update.png\" alt=\"Update Row\" class=\"update\" title=\"Update Row\">\n";
			echo "<a href=\"delete.php?column1=".$row['column1']."\"><img title='Delete Row' alt=\"Delete\" class='del' src='images/delete.png'/></a></form></div>\n";
		}
	echo "</table><br />\n";
} else {
	echo "<b><center>NO DATA</center></b>\n";
}
echo "<div>Add Row:</div>\n";
echo "<div class=\"addform\"><form method='get' action=\"add.php\">\n".
"	<input type=\"text\" name=\"column1\"/>\n".
"	<input type=\"text\" name=\"column2\"/>\n".
"	<input type=\"text\" name=\"column3\"/>\n".
"	<input type=\"text\" name=\"column4\"/>\n".
"	<input type=\"text\" name=\"column5\"/>\n".
"   <input type=\"text\" name=\"column6\"/>\n".
"	<input type=\"image\" src=\"images/add.png\" alt=\"Add Row\" class=\"update\" title=\"Add Row\">\n".
"</form></div>";

 

 

Add.php:

 


//Get database credentials
require 'config.php';

$column1 = $_GET['column1'];
$column1 = $_GET['column1'];
$column2 = $_GET['column2'];
$column3 = $_GET['column3'];
$column4 = $_GET['column4'];
$column5 = $_GET['column5'];
$column6 = $_GET['column6'];

// connect to the mysql database server.
mysql_connect ($dbhost, $dbusername, $dbuserpass);
//select the database
mysql_select_db($dbname) or die('Cannot select database');

$query = "INSERT INTO `table1` ( `column1` , `column2` , `column3` , `column4` , `column5` , `column6` )" .
"VALUES ('".$column1."', '".$column2."', '".$column3."', '".$column4."', '".$column5."', '".$column6."');";


 

 

Update.php:

 


$column1 = $_GET['column1'];
$column2 = $_GET['column2'];
$column3 = $_GET['column3'];
$column4 = $_GET['column4'];
$column5 = $_GET['column5'];
$column6 = $_GET['column6'];

// connect to the mysql database server.
mysql_connect ($dbhost, $dbusername, $dbuserpass);
//select the database
mysql_select_db($dbname) or die('Cannot select database');

$query = "UPDATE table1" .
" SET column2 = '".$column2."'," .
" column3 = '".$column3."'," .
" column4 = '".$column4."'," .
" column5 = '".$column5."'," .
" column6 = '".$column6."'" .
" WHERE column1 = '".$column1."'";

 

In Style.css, increased table and .addform width.

it's decent for practicing and whatnot. But you might want to use POST instead, rather than GET.

 

Anyone can simply add values through the browser url and update/add whatever they want.

 

You also might want to look into sanitizing your data so that mischevious users don't attack your database through sql injection.

  • Author

As a new PHP personage I have done a bit of research into both methods. If I would to literaly change

$column1 = $_GET['column1']; 

to

$column1 = $_POST['column1'];

Would this work? Were can I read about sanitizing my data...and when I mean read I mean "A complete Idiots guide to..."

Cheers Guys great stuff!

Kyle

  • Author

I'm also having trouble changing the column names, I will keep this updated as to how I get on. But any tips would be appreciated

Kyle

Done it, eventually got my head around it. Gonna try Posting instead of getting now :p

Thanks again

Kyle

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.