/    Sign up×
Community /Pin to ProfileBookmark

Add, delete, edit page working for each table automaticly

Good day to you all,
I’m working on a MySQL database production area.
I don’t yet have access to my SSH connect ion and I’m trying to not use it since it’s for a project orther that my regular.

I have my adding , deleting and editing script all in separate php pages.

For each table that I want to be able to play with I have to rename and edit thos pages acordingly.

Is there a way that I can make only a single adding, deleting and editing page work for each table in all database ?

What is all the people do ?, that is the part I don’t catch.

Here is my pages:

notes.php

[code=php]

<?PHP

//check if the starting row variable was passed in the URL or not
if (!isset($_GET[‘startrow’]) or !is_numeric($_GET[‘startrow’])) {
//we give the value of the starting row to 0 because nothing was found in URL
$startrow = 0;
//otherwise we take the value from the URL
} else {
$startrow = (int)$_GET[‘startrow’];
}

mysql_connect(“localhost”,”USER”,”PASSWORD”);//database connection
mysql_select_db(“DB”);
$order = mysql_query(“SELECT * FROM Notes”);
$num_rows = mysql_num_rows($order);

// Sort Process debut
$ord = (isset($_GET[‘ord’]) && ($_GET[‘ord’] == ‘ASC’ || $_GET[‘ord’] == ‘DESC’) ? $_GET[‘ord’] : ‘ASC’); // sets ‘desc’ as default
$link = ($ord == ‘ASC’ ? ‘DESC’ : ‘ASC’);
if(empty($_GET[‘coll’]))
{
$_GET[‘coll’] = “priorite”;
}

$col = mysql_real_escape_string(trim($_GET[‘coll’]));
// Sort Process fin

//General mysql query debut
$order = “SELECT * FROM Notes ORDER BY `$col` $ord LIMIT $startrow, 30”;
if($order == false)
{
user_error(mysql_error() . “<br />n$order”);
die(“SQL error”);
}
//General mysql query fin

// Headers
$result = mysql_query($order);
for ($i = 0; $i < mysql_num_fields($result); $i++) {
if (mysql_field_name($result, $i) == “id”){
print “<th id=”title” style=”color:#FF0000;”><a href=””.$_SERVER[‘PHP_SELF’].”?coll=”.mysql_field_name($result, $i).”&ord=”.$link.”&time=”.date(“hisu”).”” title=”Sort by : “.mysql_field_name($result, $i).”” style=”color:#000000;”>ID</a></th>n”;
}else{
print “<th id=”title”><a href=””.$_SERVER[‘PHP_SELF’].”?coll=”.mysql_field_name($result, $i).”&ord=”.$link.”&time=”.date(“hisu”).”” title=”Sort by : “.mysql_field_name($result, $i).””style=”color:#000000;”>”.mysql_field_name($result, $i).”</a></th>n”;
}

}

echo “</tr>”;
// Headers fin

// Form Debut
echo “<tr><form method=”post” name=”action” action=”add_row_notes.php”>”;
print “<td id=”submit”><input type=”submit” id=”envoyer” value=”&nbsp;&nbsp;&nbsp;”></td>n”;
for ($i = 0; $i < mysql_num_fields($result); $i++) {
if (mysql_field_name($result, $i) == “modif”){
print “<td id=”submit”><input id=”submitt” type=”text” size=”10″ name=””.mysql_field_name($result, $i).”” value=””.date(‘Y-m-d G:i:s’).””></td>n”;
}else{
if (mysql_field_name($result, $i) == “note”){
print “<td id=”submit”><input id=”submitt” type=”text” size=”50″ name=””.mysql_field_name($result, $i).”” value=””.$_SESSION[mysql_field_name($result, $i)].””></td>n”;
}else{
if (mysql_field_name($result, $i) == “titre”){
print “<td id=”submit”><input id=”submitt” type=”text” size=”30″ name=””.mysql_field_name($result, $i).”” value=””.$_SESSION[mysql_field_name($result, $i)].””></td>n”;
}else{
if (mysql_field_name($result, $i) == “priorite”){
print “<td id=”submit”><select name=””.mysql_field_name($result, $i).””><option value=”1-Urgent” selected>1-Urgent</option><option value=”2-Normal”>2-Normal</option><option value=”3-Eventuellement”>3-Eventuellement</option><option value=”4-Autre”>4-Autre</option></select></td>n”;
}else{
if (mysql_field_name($result, $i) == “status”){
print “<td id=”submit”><select name=””.mysql_field_name($result, $i).””><option value=”Ouvert” selected>Ouvert</option><option value=”Attente_Discussion”>Attente Discussion</option><option value=”Fermer”>Fermer</option></select></td>n”;
}else{
if (mysql_field_name($result, $i) == “categorie”){
print “<td id=”submit”>”;
print “<select name=””.mysql_field_name($result, $i).””>”;
print “<option value=”**-Franck” selected>**-test</option>”;
print “<option value=”**-Rocky” selected>**-test2</option>”;
print “<option value=”Vivants” selected>Vivants</option>”;
print “<option value=”Inspirations”>Inspirations</option>”;
print “<option value=”Connaissances”>Connaissances</option>”;
print “<option value=”Ensemble”>Ensemble</option>”;
print “<option value=”Outils”>Outils</option>”;
print “</select></td>n”;
}else{
print “<td id=”submit”><input id=”submitt” type=”text” size=”1″ name=””.mysql_field_name($result, $i).”” value=””.$_SESSION[mysql_field_name($result, $i)].””></td>n”;
}
}
}
}
}
}
}
echo “</form></tr>”;
// Headers Fin

//Grid
$result = mysql_query($order);

$i = 1;
while(($data = mysql_fetch_row($result))!== false){
$i++;

if ($data[1] == ”){$data[1] = “<br>”;}else{}
if ($data[2] == ”){$data[2] = “<br>”;}else{}
if ($data[3] == ”){$data[3] = “<br>”;}else{}
if ($data[4] == ”){$data[4] = “<br>”;}else{}
if ($data[5] == ”){$data[5] = “<br>”;}else{}
if ($data[5] == ”){$data[6] = “<br>”;}else{}
if ($data[5] == ”){$data[7] = “<br>”;}else{}

echo(“<tr style=”border:0px;” onMouseOver=”this.className=’highlight'” onMouseOut=”this.className=’normal'”>
<td id=”fields”.($i & 1).””>
<a href=”delete_row_notes.php?id=” . $data[‘0’] . “” title=”DELETE : Row ID #:” . $data[‘0’] . “”>
<img src=”Template/Images/delete.png” border=”0″/></a>
<a href=”editor_row.php?id=”.$data[‘0’].”” title=”Edit : Row ID #:”.$data[‘0’].”” onclick=”load(‘editor_row.php?id=”.$data[‘0’].”‘,'”.$data[‘0’].”‘);return false;”>
<img src=”Template/Images/edit_icon.png” border=”0″/></a>
</td>
<td id=”fields”.($i & 1).””>”. $data[0].”</td>
<td id=”fields”.($i & 1).””>”. $data[1].”</td>
<td id=”fields”.($i & 1).””>”. $data[2].”</td>
<td id=”fields”.($i & 1).””>”. $data[3].”</td>
<td id=”fields”.($i & 1).””>”. $data[4].”</td>
<td id=”fields”.($i & 1).””>”. $data[5].”</td>
<td id=”fields”.($i & 1).””>”. $data[6].”</td>
</tr><tr><td colspan=”8″ id=””.$data[‘0’].””></td></tr>”);
}
// Grid fin

//PREV & NEXT debut
echo “<tr><td colspan=”8″ align=”center” id=”listpage”>”;
//only print a “Previous” link if a “Next” was clicked
$prev = $startrow – 30;

if ($prev >= 0)
echo ‘<a href=”‘.$_SERVER[‘PHP_SELF’].’?startrow=’.$prev.'” title=”Pr&eacute;cedante (‘.$prev.’/’.$startrow.’)” style=”color:#FFFFFF;”> (‘.$prev.’/’.$startrow.’) Pr&eacute;cedante</a> ‘;
$next = $startrow+30;
echo ‘<a href=”‘.$_SERVER[‘PHP_SELF’].’?startrow=’.($startrow+30).'” title=”Prochaine (‘.$startrow.’/’.$next.’)” style=”color:#FFFFFF;”>Prochaine <font style=”color:#003300″>(</font>’.$startrow.'<font style=”color:#003300″>-</font>’.$next.'<font style=”color:#003300″> / </font>’.$num_rows .'<font style=”color:#003300″>)</font></a>’;
$prev = $startrow – 30;
echo “</td></tr>”;
//PREV & NEXT fin

echo “</table></td></tr></table>”;

?>

[/code]

Add_row.php

[code=php]

<?PHP

$id = $_POST[‘id’];
$_SESSION[‘id’] = $_POST[‘id’];
$modif = $_POST[‘modif’];
$_SESSION[‘modif’] = $_POST[‘modif’];
$priorite = $_POST[‘priorite’];
$_SESSION[‘priorite’] = $_POST[‘priorite’];
$status = $_POST[‘status’];
$_SESSION[‘status’] = $_POST[‘status’];
$categorie = $_POST[‘categorie’];
$_SESSION[‘categorie’] = $_POST[‘categorie’];
$titre = $_POST[‘titre’];
$_SESSION[‘titre’] = $_POST[‘titre’];
$note = $_POST[‘note’];
$_SESSION[‘note’] = $_POST[‘note’];

mysql_connect(“localhost”,”USER”,”PASSWORD”);//database connection
mysql_select_db(“DB”);

//inserting data order
$order = “INSERT INTO Notes
(id, modif, priorite, status, categorie, titre, note)
VALUES
(‘NULL’,
‘$modif’,
‘$priorite’,
‘$status’,
‘$categorie’,
‘$titre’,
‘$note’)”;

//declare in the order variable
$result = mysql_query($order); //order executes
if($result){
echo(“<br>Input data is succeed”);

} else{
echo(“<br>Input data is fail”);
}

$time = date(“hisu”);
?>

<script language=”javascript”><!–
location.replace(“Notes.php?time=<?PHP echo $time; ?>”)
//–>
</script>

[/code]

I won’t put my deleting and editing script, but I have added my script for adding a row to the db so you can see more of what I mean.

I hope you will understand my question.

Thanks !

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@HHCOwnerMay 31.2010 — You could make a page that ask which table the user wants to work with using a code like this...

[code=php]
<html>
<head>
</head>
<body>
<center>
Which table would you like to work with?<br>
<form name="table" action="tables.php" method="post">
<select name="table" id="table">
<?php
include "connect.php";

$sql = "SELECT * FROM tables";
$result = mysql_query($sql);
$counter = 0;

while($row = mysql_fetch_array($result)){

$table = "{$row['table']}";
printf("<option value="$table">$table</option>", $counter++, $row['0']);

}

?>
</select>
<input type="submit" value="Submit" name="Submit" />
</form>
</body>
</html>
[/code]


...and have the following table in your database that list each table name...


__________
[U]| id | table |

| 1 | table1 |

| 2 | table2 |

| 3 | table3 |

[/U]


...then on the "tables.php" have this code...

[code=php]
<?
$table = $_POST['table'];
?>
<html>
<head>
</head>
<body>
<script type="text/javascript">
function page1()
{ window.location="../add.php?table=<? echo $table; ?>" }

function page2()
{ window.location="../edit.php?table=<? echo $table; ?>" }

function page3()
{ window.location="../delete.php?table=<? echo $table; ?>" }
</script>

<center>
<input type='button' value='Add Table' onclick='page1()' /><br>
<input type='button' value='Edit Table' onclick='page2()' /><br>
<input type='button' value='Delete Table' onclick='page3()' /><br>
</center>
</body>
</html>
[/code]


...and on each page (add, edit, and delete) just add...

[code=php]
$table = $_GET['table'];
[/code]


...then simply use that instead of an actual table name. I think this is what you were asking for advice on...?

Hope it helps. ?
Copy linkTweet thisAlerts:
@katierosyJun 01.2010 — You want to show up all fields values in the add forms itself and also provide one delete button on the same form. Then provide one hidden field also to delete, edit the records based on the value on this field.

You may take the values on the forms as filled and based on the button pressed and based on the value in the hidden id field you may carry out the add,edit,delete methods just after posting to the same page.

You may see If this helps.
×

Success!

Help @Peuplarchie spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...