View Full Version : SQL help.
wo1olf
July 20th, 2007, 08:10 PM
I want to get the last index of my table, in order to remove the last row added.
I have 2 functions
addRow() wich adds a new row and removeRow() which remove row. I use mysql_insert_id() in the addRow function to return the last index. The thing is I'm trying to get it and the use it in the secon function but it doesn't work.
How can id do it
here is my code
function addRow()
(...) // add a new row
return mysql_insert_id ();
function removeRow($id)
(...)// remove the
$index = addRow();
if (isset(...))
removeRow($index);
how can i do it ?
eirche
July 20th, 2007, 10:11 PM
you need an auto-increment `id` field in the table.
wo1olf
July 20th, 2007, 10:27 PM
you need an auto-increment `id` field in the table.
I already have one, the problem is i can't acces the $index variable while it is out of each functiuns
eirche
July 20th, 2007, 10:44 PM
where do you access it from? you pass it into removeRow($id) via an argument.
if you are trying access a global variable in a function, use "global $g_var;" in the function. look up doc on variable scope.
wo1olf
July 20th, 2007, 10:57 PM
upon before the declaration of the two functions i initialized the variable $index.
later after the declaration of the two function i verify if the user want's to remove or to add a row with isset
like this
if (isset ($_POST['add']))
$index = addRow();
if (isset ($_POST['remove']))
removeRow($index);
May be if anyone knows a better way to do it, i'll like to know it, because i've spent a couple hours without result
icio
July 21st, 2007, 08:30 AM
function addRow() {
// ...
return mysql_insert_id();
}
function removeRow($index) {
mysql_query("DELETE FROM table WHERE id='$index' LIMIT 1;");
}
$index = addRow();
removeRow($index)
Or, if you explicitly want to use the index variable,
function removeRow() {
global $index;
mysql_query("DELETE FROM table WHERE id='$index' LIMIT 1;");
}
The first `removeRow` function will use the index passed as an argument from the function. The second will use the one from the main `index` variable because of the `global` command there.
Hope that helps :thumb:
wo1olf
July 21st, 2007, 12:50 PM
the mian problem is whenever i want to check if the $_POST var 'add' or 'remove' are set, then the code do not work anymore.
function addRow() {
// ...
return mysql_insert_id();
}
function removeRow($index) {
mysql_query("DELETE FROM table WHERE id='$index' LIMIT 1;");
}
if (isset ($_POST['add']))
{
$index = addRow();
}
if (isset ($_POST['remove']))
{
removeRow($index);
}i can add a new row bu i still can't remove it
eirche
July 21st, 2007, 01:04 PM
the mian problem is whenever i want to check if the $_POST var 'add' or 'remove' are set, then the code do not work anymore.
if (isset ($_POST['add']))
{
$index = addRow();
}
if (isset ($_POST['remove']))
{
removeRow($index);
}
for both of these statements to run, you will need to post both 'add' and 'remove' variables. it doesn't work just by posting 'remove' variable. how can you fail to see this?!
to remove the last row on a table with auto-increment `id` field
delete from `mytable` where `id` = max(`id`)
wo1olf
July 21st, 2007, 01:36 PM
i try the query above directly in phpMyadmin and i got this error
Function dbName.MAX does not exist
eirche
July 21st, 2007, 02:51 PM
see if you can do this
select max(`id`) from `mytable`
wo1olf
July 21st, 2007, 04:48 PM
thnks a lot, i've figured out the way to do it. But now i have another problem. Since i have like 4 different input type buttons (add, remove, edit, submit) in the same form. The thing is if i want one to do an action i have to push it twice. And if i press another different one then it does the same action as the previous pressed button. I guss this must be due to the $_POST variable i'm using. So is there a way to clear it after having it used ?
eirche
July 21st, 2007, 06:46 PM
The thing is if i want one to do an action i have to push it twice
think you may have screwed up $_session. post a SIMPLIFIED version of the html code and php code.
wo1olf
July 22nd, 2007, 06:07 PM
I'm not using any session var, but if i do would it work?
wo1olf
July 23rd, 2007, 05:27 PM
here is my code sorry if it is too long. I still got the same 'click' problem
<?php
include("connection.php");
$numLigne = 0;
$nbCol = mysql_num_fields(...);
// get the number of lines in the db
// see if the user want to sort or not
if(isset($_REQUEST['tri']) && !empty($_REQUEST['tri']))
{
$requete = mysql_query ("SELECT * FROM ... ORDER BY tablename.".$_REQUEST['tri']."");
}
else
{
$requete = mysql_query("SELECT * FROM ...");
}
$nbCol = mysql_num_fields(...);
function addNew_row()
{...}
//add a new row at the end of the table
function getLastID()
{...}
//return the last inserted id
function updateTable($arg1,$arg2,$arg3...)
{...}
// update the table with the values sent as parameters
function removerRow()
{...}
//reomve the last row
function get_tHeader($requete,$nbCol)
{
$tHeader = mysql_list_fields("bd","table");
echo "<tr> \n";
for($i = 0; $i < $nbCol; $i++)
{
echo "<th><a href= index.php?tri=".mysql_field_name($tHeader,$i).">".mysql_field_name($tHeader,$i)."</a></th>";
}
echo "<th> </th><th> </th>";
echo "</tr> \n";
}
// create the tabl header based on the table's field in the the db
// this display the bdContent
function getPays($requete,$nbCol)
{
if(isset($_REQUEST['changer']))
{
//if the user want to modify a given row of the table
// displays the content of that row in input text fields
while($ligne = mysql_fetch_row($requete))
{
echo "<tr> \n";
for($i = 0; $i < $nbCol; $i++)
{
if($ligne[0] == $_REQUEST['changer'])
{
if($i != 0)
{
if($ligne[$i] == null)
{
echo "<td><input type ='text' size = '10' name = ".$i." value = ".$ligne[$i]."></td>";
}
else
{
echo "<td><input type ='text' size = '10' name = ".$i." value = ".$ligne[$i]."></td>";
}
}
else
{
echo "<td><input type ='hidden' name = ".$i." value = ".$ligne[$i].">".$ligne[$i]."</td>";
}
}
else
{
if($ligne[$i] == null)
{
echo "<td> </td>";
}
else
{
echo "<td>".$ligne[$i]."</td>";
}
}
}
echo "<td>\n";
echo "<a href = index.php?changer=".$ligne[0]."> Changer </a>";
echo "</td>\n ";
echo "<td><input type = 'submit' value = 'Appliquer' name = 'Appliquer'></td>\n ";
echo "</tr> \n";
}
}
else
{
// else just displays the content in table
while($ligne = mysql_fetch_row($requete))
{
echo "<tr> \n";
for($i = 0; $i < $nbCol; $i++)
{
if($ligne[$i] == null)
{
echo "<td> </td>";
}
else
{
echo "<td>".$ligne[$i]."</td>";
}
}
echo "<td>\n";
echo "<a href = index.php?changer=".$ligne[0]."> Changer </a>";
echo "</td>\n ";
echo "<td><input type = 'submit' value = 'Appliquer' name = 'Appliquer'></td>\n ";
echo "</tr> \n";
}
}
}
// verify if the users want to apply changes
if(isset($_POST['Appliquer']))
{
if(isset($_POST[0]))
{
updateTable($_POST[0],...,$_POST[n]);
//$_POST[n] correspond to the n input text value
}
}
//verify what action to be done
if(isset($_REQUEST['action']))
{
switch($_REQUEST['action'])
{
//add a new row
case 'ajouter' : addNew_row();
$_REQUEST['action'] = null;
break;
//remove the last row
case 'supprimer' : removerRow();
$_REQUEST['action'] = null;
break;
default : break;
}
}
?>
<html>
<head>...
</head>
<body>
<table ...>
(...)
<td >
<form method= "post" action= "<?php echo $_SERVER['PHP_SELF'];?>">
<table width = "800px" border = "0" cellpadding = "3px"cellspasing = "2px">
<?php
get_tHeader($requete,$nbCol);
getPays($requete,$nbCol);
?>
</table>
(...)
<td align = "left">
<a href = "index.php?action=ajouter">Ajouter une Nouvelle ligne</a> |
<a href = "index.php?action=supprimer">Supprimer derniere ligne</a>
(...)
</form>
(...)
</body>
<html>
eirche
July 23rd, 2007, 07:44 PM
before and after the first click. see if the two html documents are identical.
wo1olf
July 24th, 2007, 10:09 AM
the php code is in the html page, just before it begins (i.e i have onyl one page 'index.php' with php & html in it)
eirche
July 24th, 2007, 12:51 PM
no. i meant when you are viewing them from a browser. just from user's perspective are the two html documents identical? i suspect they are not.
wo1olf
July 24th, 2007, 02:52 PM
so i have to verify if the original html page (with php) is similar to the final html page (produced in the web browser) ?
eirche
July 24th, 2007, 04:22 PM
But now i have another problem. Since i have like 4 different input type buttons (add, remove, edit, submit) in the same form. The thing is if i want one to do an action i have to push it twice.
1. open your web browser and go to that page.
2. right click the page and click "view source" (in IE), or "view page source" (in FF).
3. click any input button to next page.
4. right click and view source again.
5. examine if the two page sources are identical.
if this whole thing is too complicated for you, use multiple php file to handle each action instead of scramble everything into a single php file. have like add.php, remove.php etc. or start from scratch and only have the adding functionality.
wo1olf
July 24th, 2007, 05:00 PM
thnks a lot, i think that i will go for your multiple pages suggestion.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.