Results 1 to 14 of 14

Thread: Updateing tables in mysql *RESOLVED*

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    448

    Updateing tables in mysql *RESOLVED*

    can i update more than one peice of information, i dont know the right word. what i have made is this page for the admin to see if the information submited buy users is factual or not. And i had a it print out the information from the database to see it then i had a checkbox to say if it was able to be viewed by the public. but im having some problems with it.. i dont understand how to make it update more than one or one, sql thing. ill show yo uhave i have....

    PHP Code:
    <form action="admin_check.php" method="post">
    <?php
    include("./admin/config.php");
    mysql_connect($servername,$dbusername,$dbpassword) or die("Unable to connect to SQL server"); 
            
    mysql_select_db($dbname) or die("Unable to select database");

    if (isset(
    $_POST['a_accept'])) {

    $sql "SELECT * FROM alumni";
    $alumni mysql_query($sql) or die(mysql_error());
    while (
    $alumnus mysql_fetch_array($alumni)) {

        
    $id $_POST[$alumnus['alumni_id']];
    $update "UPDATE `alumni` SET `alumni_ax` = '1' WHERE `alumni_id` = '$id'";
    mysql_query($update);
    }
    } else {
            
    $sql "SELECT * FROM alumni";
    $alumni mysql_query($sql) or die(mysql_error());

    echo 
    '<table><tr><td colspan=3><hr color="#000000" noshade></td></tr>
        <tr>'
    ;

    while (
    $alumnus mysql_fetch_array($alumni)) {
        if (
    $alumnus['alumni_ax'] == 0) {
        if (
    $i == 3) {
            echo 
    '</tr><tr>';
            
    $i 0;
        }
        echo 
    '<td width="227" align="left" valign="top"><table><tr><td><b>' $alumnus['alumni_name'] . '</b></td></tr>';
        echo 
    '<tr><td>' $alumnus['alumni_year'] . '</td></tr>';
        echo 
    '<tr><td>' $alumnus['alumni_address'] . '</td></tr>';
        echo 
    '<tr><td>' $alumnus['alumni_phone'] . '</td></tr>';
        if (
    $alumnus['alumni_email'] == '')  { echo '<tr><td>n/a</td></tr>'; } else { echo '<tr><td><a href="mailto:' $alumnus['alumni_email'] . '">' $alumnus['alumni_email']. '</a></td></tr>'; }
        echo 
    '<tr><td>' $alumnus['alumni_afterschool'] . '</td></tr>';
        echo 
    '<tr><td>' '<input name="' $alumnus['alumni_id'] . '" type="checkbox">' ' Acceptable?</td></td></table><br></td>';

        
    $i++;
        }
    }

    echo 
    '</tr>';

    ?>
    <tr>
        <td colspan=3>
        <hr noshade color=black>
        Are You sure these submissions are ok?<br>
        <input name="a_accept" type="submit" value="Accept"> <input name="Clear" type="reset" value="clear">
        </td>
    </tr>
    <?
    echo '</table><br>';
    }
    ?>
    </form>
    i hope you understood what i was talking about, and can help. thanks
    Last edited by Muk108; Sep 22nd, 2003 at 05:57 AM.

  2. #2
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I'm confused as to what you're doing, but if you're asking about updating more than one field in a record at a time, then yes you can. The syntax would be like:

    Code:
    UPDATE tblName SET field1='value1', field2='value2', field3='value3' WHERE id=1
    Is that what you were asking?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    448
    im asking about updateing different id's but the feild i am updateding is the same, and it would be filled with the same number.

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629
    you could try

    Code:
    UPDATE tablename SET field='value' WHERE id='1' OR id='2'
    and just keep adding OR's.. if there's lots of ID's, there's always while() or for() loops..

    Code:
    $query = "UPDATE tablename SET field='value' WHERE ";
    $numberofid = 5;
    for($i = 1; $i <= $numberofid; $i++){
      if($i > 1){
        $query .= "OR ";
      }
      $query .= "id='$i' ";
    }
    mysql_query($query);
    it will produce something like

    Code:
    UPDATE tablename SET field='value' WHERE id='1' OR id='2' OR id='3' OR id='4' OR id='5'
    Last edited by kows; Sep 18th, 2003 at 10:38 PM.
    Like Archer? Check out some Sterling Archer quotes.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    448
    ok but im not really sure how i could put this in the code to get it after i have clicked the submition button?

    also how couldi keep each result displayed uniq(sp)?

  6. #6
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    What you're going to want to do is make the checkboxes a set of arrays that hold the ID values. That way, when you get to the submission in your PHP script, you can loop through the array (it will only contain the ones that were checked), and build your SQL statement.

    Here's an (untested) example. Beware of syntax errors and such:

    Code:
    <?php 
        include("./admin/config.php"); 
        mysql_connect($servername,$dbusername,$dbpassword) or die("Unable to connect to SQL server");  
        mysql_select_db($dbname) or die("Unable to select database"); 
    
        if (isset($_POST['a_accept'])) { 
            $sql = "SELECT * FROM alumni"; 
            $alumni = mysql_query($sql) or die(mysql_error()); 
    
            while ($alumnus = mysql_fetch_array($alumni)) {
                if (count($_POST['alumid']) != 0) {
                    $sql = "UPDATE alumi SET alumni_ax='1' WHERE ";
    
                    for ($i = 0; $i < count($_POST['alumid']); $i++) {
                        $sql .= " alumni_id='" . $_POST['alumid'][$i] . "'";
    
                        if ($i != (count($_POST['alumid']) - 1)) {
                            $sql .= " OR ";
                        }
                    }
                    
                    mysql_query($sql);
                }
            } 
        } else { 
             
            $sql = "SELECT * FROM alumni"; 
            $alumni = mysql_query($sql) or die(mysql_error()); 
    
            echo '<form action="admin_check.php" method="post"> 
            <table>
                <tr>
                    <td colspan=3><hr color="#000000" noshade></td>
                </tr> 
                <tr>'; 
    
            while ($alumnus = mysql_fetch_array($alumni)) { 
                if ($alumnus['alumni_ax'] == 0) { 
                    if ($i == 3) { 
                        echo '</tr><tr>'; 
                        $i = 0; 
                    } 
    
                    echo '<td width="227" align="left" valign="top">
                        <table>
                            <tr>
                                <td><b>' . $alumnus['alumni_name'] . '</b></td>
                            </tr>
                            <tr>
                                <td>' . $alumnus['alumni_year'] . '</td>
                            </tr>'; 
                            <tr>
                                <td>' . $alumnus['alumni_address'] . '</td>
                            </tr>
                            <tr>
                                <td>' . $alumnus['alumni_phone'] . '</td>
                            </tr>'; 
                    
                    if ($alumnus['alumni_email'] == '') { 
                        echo '<tr><td>n/a</td></tr>'; 
                    } else { 
                        echo '<tr>
                            <td><a href="mailto:' . $alumnus['alumni_email'] . '">' . $alumnus['alumni_email']. '</a></td>
                        </tr>'; 
                    } 
                    
                    echo '<tr>
                        <td>' . $alumnus['alumni_afterschool'] . '</td>
                    </tr>
                    <tr>
                        <td><input name="alumid[]" value="' . $alumnus['alumni_id'] . '" type="checkbox">Acceptable?</td>
                    </td>
                    </table><br></td>'; 
    
                    $i++; 
                } 
            } 
    
            echo '</tr>
                <tr> 
                    <td colspan=3> 
                        <hr noshade color=black> 
                        Are You sure these submissions are ok?<br> 
                        <input name="a_accept" type="submit" value="Accept"> <input name="Clear" type="reset" value="clear"> 
                    </td> 
                </tr> 
            </table><br></form>'
        }
    ?>
    Important changes are in bold.

    I also changed your SQL statements a bit. Having `quotes` around field names isn't usually required, and in this case, they aren't.

    If your alumni_ax and alumni_id fields are INT fields, they you also do not need 'quotes' around them, either. But I left them there just in case.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  7. #7
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    Originally posted by The Hobo
    If your alumni_ax and alumni_id fields are INT fields, they you also do not need 'quotes' around them, either. But I left them there just in case.
    not neccassarily. I have had instances of it failing if they didn't have single quotes. I always use them just for that reason. because just my luck they would fail when i didn't.

    so it is up to you I guess, I was just stating that I have had it fail.

  8. #8
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by phpman
    not neccassarily. I have had instances of it failing if they didn't have single quotes. I always use them just for that reason. because just my luck they would fail when i didn't.

    so it is up to you I guess, I was just stating that I have had it fail.
    Well I never have.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  9. #9
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629
    Yeah, unless you do decimals or plain text when entering it, you don't need any quotes around it. I just add quotes because I'm usually entering in variables, and not just plain numbers, so I could end up with some decimals or strings in it.

    hmm, now I'm wondering if you can just do something like:

    Code:
    $table = "table";
    $var = "variable and stuff";
    $query = "UPDATE $table SET var=$var WHERE id=1";
    mysql_query($query);
    and would it auto-quote $var, and actually print it to MySQL like:

    Code:
    UPDATE table SET var="variable and stuff" WHERE id=1
    I think I'll go try that out, but probably not tonight because I'm busy.
    Like Archer? Check out some Sterling Archer quotes.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    448
    yeah i tryed that and it still doesnt update...and there is a problem it was just missing a few semicolins but i got them. any other ideas as to why its not working?

  11. #11
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    try this and tell us what you get
    PHP Code:
    <?php 
        
    include("./admin/config.php"); 
        
    mysql_connect($servername,$dbusername,$dbpassword) or die("Unable to connect to SQL server");  
        
    mysql_select_db($dbname) or die("Unable to select database"); 

        if (isset(
    $_POST['a_accept'])) { 
            
    $sql "SELECT * FROM alumni"
            
    $alumni mysql_query($sql) or die(mysql_error()); 

            while (
    $alumnus mysql_fetch_array($alumni)) {
                if (
    count($_POST['alumid']) != 0) {
                    
    $sql "UPDATE alumni SET alumni_ax='1' WHERE ";

                    for (
    $i 0$i count($_POST['alumid']); $i++) {
                        
    $sql .= " alumni_id='" $_POST['alumid'][$i] . "'";

                        if (
    $i != (count($_POST['alumid']) - 1)) {
                            
    $sql .= " OR ";
                        }
                    }
                    
                   
    $resultmysql_query($sql);
                   if(!
    $result){echo mysql_error(); }
                }
            } 
        } else { 
             
            
    $sql "SELECT * FROM alumni"
            
    $alumni mysql_query($sql) or die(mysql_error()); 

            echo 
    '<form action="admin_check.php" method="post"> 
            <table>
                <tr>
                    <td colspan=3><hr color="#000000" noshade></td>
                </tr> 
                <tr>'


            while (
    $alumnus mysql_fetch_array($alumni)) { 
                if (
    $alumnus['alumni_ax'] == 0) { 
                    if (
    $i == 3) { 
                        echo 
    '</tr><tr>'
                        
    $i 0
                    } 

                    echo 
    '<td width="227" align="left" valign="top">
                        <table>
                            <tr>
                                <td><b>' 
    $alumnus['alumni_name'] . '</b></td>
                            </tr>
                            <tr>
                                <td>' 
    $alumnus['alumni_year'] . '</td>
                            </tr>'

                         echo
    '   <tr>
                                <td>' 
    $alumnus['alumni_address'] . '</td>
                            </tr>
                            <tr>
                                <td>' 
    $alumnus['alumni_phone'] . '</td>
                            </tr>'

                    
                    if (
    $alumnus['alumni_email'] == '') { 
                        echo 
    '<tr><td>n/a</td></tr>'
                    } else { 
                        echo 
    '<tr>
                            <td><a href="mailto:' 
    $alumnus['alumni_email'] . '">' $alumnus['alumni_email']. '</a></td>
                        </tr>'

                    } 
                    
                    echo 
    '<tr>
                        <td>' 
    $alumnus['alumni_afterschool'] . '</td>
                    </tr>
                    <tr>
                        <td><input name="alumid[]" value="' 
    $alumnus['alumni_id'] . '" type="checkbox">Acceptable?</td>
                    </td>
                    </table><br></td>'


                    
    $i++; 
                } 
            } 

            echo 
    '</tr>
                <tr> 
                    <td colspan=3> 
                        <hr noshade color=black> 
                        Are You sure these submissions are ok?<br> 
                        <input name="a_accept" type="submit" value="Accept"> <input name="Clear" type="reset" value="clear"> 
                    </td> 
                </tr> 
            </table><br></form>'
    ;
        }
    ?>
    Last edited by phpman; Sep 21st, 2003 at 10:04 AM.

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    448
    it had the same two errors and a few more, in the first few lines the database thing was the wrong table, changed that and then

    PHP Code:
                                <td><b>' . $alumnus['alumni_name'] . '</b></td
                            </
    tr
                            <
    tr
                                <
    td>' . $alumnus['alumni_year'] . '</td
                            </
    tr>'; 
    shouldnt have the ' or ; there

    then you need a ;

    PHP Code:
                   </td>  
                </
    tr>  
            </
    table><br></form>
    and still no luck

  13. #13
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    after you fixed those errors what did it tell you? did it echo a error message?

    I just copied the code from Hobo and added something, didn't check for errors.

    I fixed my code so try it again

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    448
    ok its working now thank you very much!

Posting Permissions

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



Click Here to Expand Forum to Full Width