Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jan 2004
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Deleting Rows in SQL Table from PHP Link

    Hello everyone, this is my first time here. i didnt find any thread that had this topic, so im going to start one to see if i cant get any assistance.

    What i want to do is make a link (either just a text link, or image link) that deletes a specific row in a MySQL table. I have the table sorted by auto_increment(id), so i can find it by desc. Thats not the problem.

    I have tried experimenting with different code, but i just cant seem to get it right.

    Any Assistance would be great, thanks ahead of time.

  • #2
    piz
    piz is offline
    Regular Coder
    Join Date
    Jul 2002
    Location
    Barcelona
    Posts
    246
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well.. with what code you experimented?
    How do you want to do that?

    You can execute PHP only before the browser loads the page and display it. So, if you want to use PHP and execute the PHP code on clicking somewhere, you have to link to the same or to another page putting there the code you want to execute.

  • #3
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It's absolutely not hard at all.

    you just append the id of the record to the querystring. So your link will look like:

    <a href="deleterecord.php?id=25" etc

    Then inside deleterecord, you use that querystringvariable inside your deletestatement. Like

    $sql="DELETE FROM table WHERE youridvariable=" . $_GET['id'] ;


    But you need to be extra carefull that the user doesn't alter the querystring and deletes another record.
    You can do that for instance by checking if his userID is the same as the one who created the record or if the user is a moderator or whatever.(like this forum probably does)

    So at the top of deleterecord.php, you need to do some sort of checking. Unless that these pages can only be used inside a tightly secured admin-section, so that only trusted peole use them.

  • #4
    New to the CF scene
    Join Date
    Jan 2004
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the replies.

    i will try Your idea asap and post how it went.

    oh, I dont believe im going to worry about whos deleting whos row because its going to be placed in the admin section of a website. so there may be only like 1-2 people posting content who will have the option of deleting rows.
    Last edited by xxxemptyxxx; 01-12-2004 at 06:33 AM.

  • #5
    New to the CF scene
    Join Date
    Jan 2004
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey, thanks raf. Your Idea worked great!

    i have another question. I have one table that has a 'rounds_for' field in it.

    the table that it's in looks something like this:

    PHP Code:
    ID  |  OPPONENT  |  ROUNDS_FOR  |  ROUNDS_AGAINST
    - - - - - - - - - - - - - - - - - - - - - - - - -
     
    1  |    TBD     |      20      |        4
     2  
    |    TBD     |      10      |       20
     3  
    |    TBD     |      16      |       10
     4  
    |    TBD     |      49      |        1
     5  
    |    TBD     |      05      |       18 
    I was curious how i would go about adding up all the data in a specific section like ROUNDS_FOR and displaying it. Which would produce a summed number (which in this case is 100).

    Also, I found out that you cant insert the number 0 into a MySQL (or at least it wouldn't process in my form.) I dont know if you just cant have zeros in tables, or theres something i need to put in my form.

    heres my form html:
    PHP Code:
    <form name="" method="post" action="<? $PHP_SELF ?>">
            <table width="100%" border="0" cellpadding="0" cellspacing="0">
              <tr>
                <td width="45%" valign="middle"><p align="right">Opponent:&nbsp;&nbsp;&nbsp;</p></td>
                <td width="55%" valign="middle"><input name="opponent" type="text" style="border: none; border-color:#FFFFFF; color: #003399; background-color: #99CCFF; font-size:10px; font-family: arial;" value="<? $opponent ?>" size="20"></td>
              </tr>
              <tr>
                <td colspan="2"><p align="right">&nbsp;</p></td>
              </tr>
              <tr>
                <td valign="middle"><p align="right">Total Points For :&nbsp;&nbsp;&nbsp;</p></td>
                <td valign="middle"><input name="rounds_for" type="text" style="border: none; border-color:#FFFFFF; color: #003399; background-color: #99CCFF; font-size:10px; font-family: arial;" value="<? $rounds_for ?>" size="15"></td>
              </tr>
              <tr>
                <td colspan="2">&nbsp;</td>
              </tr>
              <tr>
                <td valign="middle"><p align="right">Total Points Against :&nbsp;&nbsp;&nbsp;</p></td>
                <td valign="middle"><input name="rounds_against" type="text" style="border: none; border-color:#FFFFFF; color: #003399; background-color: #99CCFF; font-size:10px; font-family: arial;" value="<? $rounds_against ?>" size="15"></td>
              </tr>
              <tr>
                <td colspan="2">&nbsp;</td>
              </tr>
              <tr valign="top">
                <td height="31" colspan="2"><div align="center">If you guys get a shut out(24-0, 0-24), or visa versa, Type <span class="style7">zero</span> instead <br>
                  of the number until I get our problem fixed, thanks. </div></td>
              </tr>
              <tr>
                <td colspan="2"><div align="center">
                </div>              
                    <div align="center">
                      <input type="submit" name="submit" value="Sign" style="border: inset; height: 18pt; width: 50pt; font-size:10px; font-family: arial;">
                    </div></td>
                </tr>
            </table>
            </form>
    <?php

    #on first opening display the form
    if( !$submit)
    $msg $form; }
    else 

    #redisplay a message and the form if incomplete
    if( !$opponent or !$rounds_for or !$rounds_against )
    {
      
    $msg "<b>Please complete all fields</b><br><br>";
      
    $msg.= $form;
    }
    else

    #add the form data to the database table
    {
      
    #connect to MySQL
      
    $conn mysql_connect("XXXXXXXX""XXXXXXX","XXXXXXXX"
        or die(
    "Count not connect to database");
      
      
    #select the database
      
    $rs mysql_select_db("ammoclan_ammoclan_com",$conn
        or die (
    "Could not select database");  

      
    #create the SQL query
      
    if($opponent and $rounds_for and $rounds_against)
      {
         
    $sql "insert into matches (opponent, rounds_for, rounds_against) 
                values (\"$opponent\",\"$rounds_for\",\"$rounds_against\")"

         
    $rs mysql_query($sql,$conn
        or die (
    "Could not execute SQL query");
      }

      
    #confirm the entry and display a link to view
      
    if($rs)
      {
        
    $msg "<center>Your match has been posted.</center>";
        
    $msg.= "blah blah blah";
      }
    }
    echo(
    $msg);
    ?>
    As I said before, the problem is that It gives me the error "Please Complete all fields" when I put in a 0 (zero) for any of the fields. Thanks for your help.

  • #6
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    was curious how i would go about adding up all the data in a specific section like ROUNDS_FOR and displaying it. Which would produce a summed number (which in this case is 100).
    2 possebilitys:

    use sum() iside the selectstatement. Like
    select sum(variable) as totalsum from table

    your can then print out $row['totalsum']

    or loop through the recordset and add them all to a variable

    $total='';
    while ($row=mysql_fetch_assoc($result)){
    $total += $row['variable'];
    }
    echo $total ;

    sum() is the fastest and generates less trafic. The incrementing variable is only better if you also use other values from that recordset. Forr instance displaying all individual values and then in the last row showing the total.

    Also, I found out that you cant insert the number 0 into a MySQL (or at least it wouldn't process in my form.) I dont know if you just cant have zeros in tables, or theres something i need to put in my form.
    ... As I said before, the problem is that It gives me the error "Please Complete all fields" when I put in a 0 (zero) for any of the fields. Thanks for your help.
    You can insert '0' since it is a value. You can even insert Null (--> no value/system missing) which you should use if you want to insert blanks. It makes a big differnce if your gonnan run computations on these fields.
    For instance, the average of (Null, Null, 10) = 10, while the average of (0,0,10) = 3.33333333

    The parser doesn't even reach the insert so its not an sql-issue.
    It's php.

    I have seen other people also complaining about how PHP threats the value '0' --> it often threats it as 'empty' which will probably gives you that problem.

    I suppose you want to check if these three fields are filled in. I always check the valuelength. Like
    PHP Code:
    if((strlen($opponent) < 1) or (strlen($rounds_for) < 1) or strlen($rounds_against ) < 1)){ 
    which wount have the 0 issue.


    Also, this
    PHP Code:
    $sql "insert into matches (opponent, rounds_for, rounds_against) 
                values (\"$opponent\",\"$rounds_for\",\"$rounds_against\")"

    is probably incorrect (it's not a recommended way anyhow)
    If these are numerical values, then use
    PHP Code:
    $sql "insert into matches (opponent, rounds_for, rounds_against) 
                values (" 
    $opponent ", " $rounds_for ", " $rounds_against ")"
    if these are stringvalues, then use
    PHP Code:
    $sql "insert into matches (opponent, rounds_for, rounds_against) 
                values ('" 
    addslashes($opponent) . "', '" addslashes($rounds_for) . "', '" addslashes($rounds_against) . "')"


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •