Results 1 to 5 of 5

Thread: SQL PHP Update/Delete/Add

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2009
    Posts
    156

    SQL PHP Update/Delete/Add

    This is my code so far for updating...

    PHP Code:

    $namereq 
    $_REQUEST['namefield'] ;
    $emailreq $_REQUEST['emailfield'] ;
    $messagereq $_REQUEST['messagefield'] ;
    $datereq $_REQUEST['datefield'] ;

    $con mysql_connect("server""username""password");
    if (!
    $con)
      {
      die(
    'Could not connect: ' mysql_error());
      echo 
    "update failed";
      }

    mysql_select_db("database"$con);
    global 
    $customernumber;

    mysql_query("UPDATE enquiryform SET name=$namereq WHERE customerno = $customernumber LIMIT 1");
    mysql_query("UPDATE enquiryform SET email=$emailreq WHERE customerno = $customernumber LIMIT 1");
    mysql_query("UPDATE enquiryform SET message=$messagereq WHERE customerno = $customernumber LIMIT 1");
    mysql_query("UPDATE enquiryform SET startdate=$datereq WHERE customerno = $customernumber LIMIT 1");

    mysql_close($con);

    echo 
    $namereq
    echo $emailreq
    echo $messagereq
    echo "Update Compleated"

    $_REQUEST['field'] have been retreved for previous page and everything echos/prints ok but nothing changes on the database table?!
    I've checked field/database/passwords/etc what am I missing?
    Last edited by LingoOutsider; May 14th, 2009 at 03:17 AM.

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: SQL PHP Update/Delete/Add

    well, first -- you shouldn't be using 4 different queries to update one record. this one query accomplishes the same as your four:
    PHP Code:
    mysql_query("UPDATE enquiryform SET name='$namereq', email='$emailreq', message='$messagereq', startdate='$datereq' WHERE customerno='$customernumber' LIMIT 1"); 
    the reason none of your things are updating is probably because you're not enclosing any values in quotes (like in my example above). MySQL will interpret strings not enclosed in quotes as functions/reserved words. this is why none of your tables are updating.

    you should also be checking your user input; you can use mysql_real_escape_string():
    PHP Code:
    $namereq mysql_real_escape_string($_REQUEST['namefield']);
    $emailreq mysql_real_escape_string($_REQUEST['emailfield']);
    $messagereq mysql_real_escape_string($_REQUEST['messagefield']);
    $datereq mysql_real_escape_string($_REQUEST['datefield']); 

  3. #3
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: SQL PHP Update/Delete/Add

    To check if there is a problem with your mysql query, you can check the value of mysql_error() after you call a query. Or run your query like this:

    PHP Code:
    mysql_query($Query) or die("MySQL error: ".mysql_error()); 
    So execution will stop and you can see straight away the error.


    Has someone helped you? Then you can Rate their helpful post.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Mar 2009
    Posts
    156

    Re: SQL PHP Update/Delete/Add

    Thanks, its updating now!

    If I want to use multiple submit buttons (add/delete/update) on the one form whats the easiest way to identify which has been pressed?
    PHP Code:
    <input type="submit" name="Change" value="Change" /> 
    what command will identify my pressed button?

  5. #5
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: SQL PHP Update/Delete/Add

    you can name the buttons and then check whether or not they've been pressed by using isset(). instead of an IF statement to handle all of it, you could do:
    PHP Code:
    <?php

      
    if($_SERVER['REQUEST_METHOD']){

        
    $button = (isset($_POST['add'])) ? 'add' : ((isset($_POST['update'])) ? 'update' 'delete');

        switch(
    $button):
          case 
    'add':

            
    //add stuff

            
    break;
          case 
    'update':

            
    //update stuff

            
    break;
          case 
    'delete':

            
    //delete stuff

            
    break;
        endswitch;
      }

    ?>
    <form action="script.php" method="post">
      <input type="submit" name="add" value="add" />
      <input type="submit" name="update" value="update" />
      <input type="submit" name="delete" value="delete" />
    </form>

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