Jump to content

newbie need help with a small script


anarchoi

Recommended Posts

Yeah well this is something very simple but i've been messing around all night long and i couldn't make a working script  :'(

 

Here is my situation: I made a page where my users are able to submit modification requests on the biography of a particuliar band. The modification requests are stored in a table called "BANDS2" while the official biographys are in "NEWS2". So, when a user submit a request it is stored in a different table (bands2) until it is confirmed by an admin.

 

So far everything is good - i made the page to make requests, and the biography is successfully copied into "BANDS2" table. I also made a page in my admin section where it lists all the modification requests, and when you click on the name of a biography it sends you to  confirmation.php?groupe=BANDNAME

 

This is where i'm stuck. Here is what i need to do in confirmation.php:

- check if an entry with same 'title' is present in "NEWS2", if so : delete it, we want to replace it by the edited version from the other table  (title is stored in $_GET "Groupe")

- take the whole row with same 'title' from "BANDS2" in the database and copy it into the table "NEWS2" so the biography become official and replace the one we just deleted

- and finally delete the old entry in "BANDS2" so it gets out of the modification-requests list and only keep the new version in "NEWS2"

 

the entries to be moved from "BANDS2" to "NEWS2" are: id, title, localisation, web, infos

 

 

if someone can just give me a script that would do this, i'd be MORE than grateful....

Thanks for your time!!!!!

<3

Link to comment
Share on other sites

i'm not asking so much, basically it's just 3 different queries to do.. its not like i didnt try to do it myself

 

Like i said i already made the page to put the modification request at the correct place while waiting its validated... i only need the last page that executes the forum requests..

Link to comment
Share on other sites

i'm not asking so much, basically it's just 3 different queries to do.. its not like i didnt try to do it myself

 

OK, show us your code and explain what problems you have with it.  Maybe then someone will point you in the right direction.  Given that you've described in words what you want your queries to do, it's not obvious why you can't resolve this yourself ...

Link to comment
Share on other sites

omg man if i typed all this big text for a newbie question like this it is REALLY because i can't solve it myself.

 

I didn't post a code because there it's useless - my code is a mess, plus it's not working, and it would only confuse you more than it would help you to help me.

 

since you absoulutly want me to post a code, here is my page for my users to add a modification requests. It does like i described, stores the modification requests in "BANDS2" while waiting for an approval

 

 

<? 
if($_POST['submit']) //If submit is hit
{
   //then connect as user
   //change user and password to your mySQL name and password
   mysql_connect("localhost","anarchoi1","***");
   
   //select which database you want to edit
   mysql_select_db("anarchoi1_phpb1");
   
   //convert all the posts to variables:
   $id = $_POST['id'];
   $theid = $_POST['id'];
   $title = $_POST['title'];
   $infos = $_POST['infos'];
   $web = $_POST['web'];
   $localisation = $_POST['localisation'];
   $date = $_POST['date'];
   $time = $_POST['time'];
   $actif = $_POST['actif'];
   $strSQL = "SELECT * FROM news2 WHERE title='".$title."'";      
    $rs=mysql_query($strSQL); //execute the query





   $result=MYSQL_QUERY("INSERT INTO bands2 (id,title,infos,web,localisation,date,time,actif)".
      "VALUES ('$theid', '$title', '$infos', '$web', '$localisation', '$date', '$time', '$actif')");

    //confirm
   echo "Query Finished";

   
   
   
   //Insert the values into the correct database with the right fields
   //mysql table = bands2
   //table columns = id, title, infos, web, localisation, date, time
   //post variables = $title, $infos, '$web', '$localisation, $date, $time
   
}
?>
<form method="post" action="modifier2_redirect.php">





<?php
if(isset($_GET['groupe']))
{
$band = $_GET['groupe'];
} else {
$band = ""; 
}


// DATABASE
$server = "localhost";
$user = "anarchoi1";
$password = "***";
$database = "anarchoi1_phpb1";
mysql_connect($server, $user, $password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());

// Query the Database
$query = "SELECT * from news2 WHERE title = '$band'"; 

$res = mysql_query($query) or die(mysql_error());   
while($row = mysql_fetch_array($res)) { 

$validation = $row["valide"];
if ( $validation == 1 ) { 


$title = $row["title"];
if ($band == $title) {
$idid = $row["id"];
echo "ID / NUMÉRO DU GROUPE (à titre de référence): $idid";
echo "<INPUT TYPE='hidden' NAME='id' VALUE='$idid' size=60>";

echo "<br>";
}
}
} 






?>























<TABLE>
<TR>
   <TD>Nom du Groupe:</TD>
<?php
echo"   <TD><INPUT TYPE='TEXT' NAME='title' VALUE='$groupe' size=60></TD>";
?>
</TR>





<TR>
   <TD>Infos/Biographie:</TD>
   <TD>





<?php

if(isset($_GET['groupe']))
{
$band = $_GET['groupe'];
} else {
$band = ""; 
}


// DATABASE
$server = "localhost";
$user = "anarchoi1";
$password = "***";
$database = "anarchoi1_phpb1";
mysql_connect($server, $user, $password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());

// Query the Database
$query = "SELECT * from news2 WHERE title = '$band'"; 

$res = mysql_query($query) or die(mysql_error());   
while($row = mysql_fetch_array($res)) { 

$validation = $row["valide"];
// if ( $validation == 1 ) { //


$title = $row["title"];
// if ($band == $title) { //
$infos2 = $row["infos"];
$infos = nl2br($infos2);
echo "<textarea name=\"infos\" rows=\"10\" cols=\"60\">$infos</textarea></p>";
echo "<br>";
// } //
// } //
} 






?>






</TD>
</TR><br>
<TR>
   <TD><br>Localisation:</TD>
   <TD>















<?php

echo "<br><b>Ce groupe est présentement fiché comme étant localisé à ";

if(isset($_GET['groupe']))
{
$band = $_GET['groupe'];
} else {
$band = ""; 
}


// DATABASE
$server = "localhost";
$user = "anarchoi1";
$password = "***";
$database = "anarchoi1_phpb1";
mysql_connect($server, $user, $password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());

// Query the Database
$query = "SELECT * from news2 WHERE title = '$band'"; 

$res = mysql_query($query) or die(mysql_error());   
while($row = mysql_fetch_array($res)) { 

$validation = $row["valide"];
// if ( $validation == 1 ) { //


$title = $row["title"];
if ($band == $title) {
$ou = $row["localisation"];
echo "$ou";
echo "</b><br>Pour modifier, choisir la nouvelle région ici:";
// } //
}
} 



echo "<SELECT NAME='localisation'>";
echo "<OPTION selected VALUE='$ou'>$ou";
echo "<OPTION VALUE=''>----------";



?>


      
         <OPTION VALUE='MTL'>Montréal
         <OPTION VALUE='QC'>Québec
         <OPTION VALUE='T-R'>Trois-Rivières
         <OPTION VALUE='Laval'>Laval
         <OPTION VALUE='Saguenay'>Saguenay
         <OPTION VALUE='Bas-St-Laurent'>Bas-Saint-Laurent
         <OPTION VALUE='Estrie'>Estrie
         <OPTION VALUE='Outaouais'>Outaouais
         <OPTION VALUE='Abitibi'>Abitibi-Témiscamingue
         <OPTION VALUE='Côte-Nord'>Côte-Nord
         <OPTION VALUE='Gaspésie'>Gaspésie
         <OPTION VALUE='Lanaudière'>Lanaudière
         <OPTION VALUE='Laurentides'>Laurentides
	<OPTION VALUE=''>Région inconnue


      </SELECT><br>
   </TD>
</TR>





<TR>
   <TD><br>Site Web / MySpace:</TD>
<?php
$web = $row["web"];

echo "<TD><br><INPUT TYPE='TEXT' NAME='web' VALUE='$web' size=60></TD>";
?>
</TR>
<TR>
   <TD><br>Actif?</TD>
   <TD>

<?php

if(isset($_GET['groupe']))
{
$band = $_GET['groupe'];
} else {
$band = ""; 
}


// DATABASE
$server = "localhost";
$user = "anarchoi1";
$password = "***";
$database = "anarchoi1_phpb1";
mysql_connect($server, $user, $password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());

// Query the Database
$query = "SELECT * from news2 WHERE title = '$band'"; 

$res = mysql_query($query) or die(mysql_error());   
while($row = mysql_fetch_array($res)) { 



$actiff = $row["actif"];
if ($actiff == "1") {
echo "<br>Le statut de ce groupe est présentement marqué comme étant ACTIF!<br>";
echo "Faire un changement: <SELECT NAME='actif'><OPTION SELECTED VALUE='1'>Groupe Actif";
echo "<OPTION VALUE='0'>Mort / Séparé</SELECT>";
}
if ($actiff == "0") {
echo "Ce groupe est présentement marqué comme étant INACTIF! (mort ou séparé)<br>";
echo "<SELECT NAME='actif'><OPTION VALUE='1'>Groupe Actif";
echo "<OPTION SELECTED VALUE='0'>Mort / Séparé</SELECT>";
}
if ($actiff == "") {
echo "Le statut de ce groupe est présentement inconnu<br>";
echo "<SELECT NAME='actif'><OPTION SELECTED VALUE=''>Statut Inconnu";
echo "<OPTION VALUE='1'>Groupe Actif";
echo "<OPTION VALUE='0'>Mort / Séparé</SELECT>";



}
} 






?>

Link to comment
Share on other sites

So you're at the stage where the admin has confirmed they want the update to take place? You'll be wanting something like:

 

<?php
$groupe = $_GET['groupe'];
$groupe = mysql_real_escape_string($groupe);
$sql = "SELECT COUNT(*) FROM BANDS2,NEWS2 WHERE BANDS2.title = '$groupe' and NEWS2.title='$groupe'";
$result = mysql_query($sql) or die(mysql_error());
$num = mysql_result($result,0);
if($num == 0){//there isnt a match
    echo "Record does not exist!";
    exit;
}
mysql_query("UPDATE BANDS2,NEWS2 SET NEWS2.localisation=BANDS2.localisation, NEWS2.web=BANDS2.web, NEWS2.infos=BANDS2.infos WHERE BANDS2.title = '$groupe' AND NEWS2.title = '$groupe'") or die(mysql_error());
mysql_query("DELETE FROM BANDS2 WHERE title='$groupe'") or die(mysql_error());
echo "Modification complete";
?>

 

There are a few things to note, however. I've deliberately not included the updating of the id or title fields. I dont think you should ever be wanted to update the ids and given that you are using the title to identify the band at the moment, it would not be a good idea to update this either.

 

I would recommend that you modify this and use the id of the band to update - that way the name of the band can also be modified.

 

Also, you may wish to consider what would happen if two people suggested an update at the same time.

 

I would, however, agree that a bit of a better attitude will help you get along. As you can see, i've not done everything for you, which appears to be what you are asking for - rather given you a prod in the right direction. Your code is nearly always useful - sometimes it can clarify the situation. And it certainyl shows us that you're trying something.

 

Oh, and finally...FYI - it is not necessary to connect to the database prior to each query. Once (assuming you dont disconnect) per script is sufficient.

Link to comment
Share on other sites

Thanks a LOT!

 

i'm sorry for have asked for a whole script, but seriously i messed around trying to make it work all night long. It was 11AM in the morning and i still had not sleep so i was a bit impatient to get this working :P

 

You made me realize there is a very more simple way to do this than i thought (by just using things like NEWS2.localisation=BANDS2.localisation in the query). I was actually trying to make that script work by doing a total different query each time this query was executed, and my script was reaaal big and messy :P

 

It's also a good idea to make it use the ID instead of the title, i'll work on it. Because i realized the script will stop if title is changed, because it is not found in NEWS2. I was trying to make it create the row if it's not found, but i just realized working with ID's found be better and more simple ;)

 

thanks again!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.