mySQL populated dropdown box how to update a table on form submit?

Discussion in 'PHP' started by zer0c00l, Nov 6, 2010.

  1. #1
    I have a dropdown box populated from a table in mySQL within a form. When I submit the form currently it records the record. I now need it to update a column within the table and change the status from "in" to "out" upon submitting the form. What code will i need to do something like this? I can get simple update mySQL working in stand alone example using the following code, but I cannot seem to get this to work with my dropdown box. Any ideas?

    UPDATE table_name SET
    column_name1 = value1,
    column_name2 = value2,
    column_name3 = value3 ...
    [WHERE conditions];
    
    PHP:
    This is my current code that makes the dropdown box.
    
    
    <form id="submit" name="submit" action= "ajax.php" method="post">
    
    <select onchange="display_data(this.value);"> 
    
        <option>Select Laptop</option> 
    
        <?php 
    
        $query="SELECT tnumber FROM laptops WHERE status='in'";
    
        $result = mysql_query($query) or die(mysql_error()); 
    
        while(list($tnumber)=mysql_fetch_row($result)) { 
    
            echo "<option value=\"".$id."\">".$tnumber."</option>"; 
    
        } 
    
        ?> 
    
    </select> 
    PHP:
     
    Last edited: Nov 6, 2010
    zer0c00l, Nov 6, 2010 IP
  2. jimmy4feb

    jimmy4feb Peon

    Messages:
    56
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I am not getting what you are trying to ask....
     
    jimmy4feb, Nov 6, 2010 IP
  3. zer0c00l

    zer0c00l Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    yea, it's kindof hard to explain in words. Let me try again.

    I have a input form on a page called checkout.php that uses a submit button. When you click submit it posts to a page called ajax.php, which handles the posting of a new record into a table called 'customers'. That is all working just dandy, but now I also need to update a column named 'status' in a table called 'laptops' which is a master table of laptops that are able to be checked out. Here is a screen shot to help understand what i am trying to do.

    The dropdown is populated with the table 'laptops' and has a field called 'status' that i need to change from "in" to "out" when it is submitted.
    [​IMG]

    Here is my ajax.php page that does the submitting of the record.

    // command to start database connection
    
    
    
    mysql_connect($mysql_hostname,$username,$password);
    
    
    
    // Select the database
    
    @mysql_select_db($database) or die( "Unable to select database");
    
    
    
    
    
    	// CLIENT INFORMATION
    
    	$tnumber      = htmlspecialchars(trim($_POST['tnumber']));
    
    	$gid          = htmlspecialchars(trim($_POST['gid']));
    
    	$fname        = htmlspecialchars(trim($_POST['fname']));
    
    	$lname        = htmlspecialchars(trim($_POST['lname']));
    
    	$ophone       = htmlspecialchars(trim($_POST['ophone']));
    
    	$cphone       = htmlspecialchars(trim($_POST['cphone']));
    
    	$email        = htmlspecialchars(trim($_POST['email']));
    
    	$cdate        = htmlspecialchars(trim($_POST['cdate']));
    
    	$rdate        = htmlspecialchars(trim($_POST['rdate']));
    
    	$ctech        = htmlspecialchars(trim($_POST['ctech']));
    
    
    
        $addClient  = "INSERT INTO customers (tnumber,gid,fname,lname,ophone,cphone,email,cdate,rdate,ctech) VALUES ('$tnumber','$gid','$fname','$lname','$ophone','$cphone','$email','$cdate','$rdate','$ctech')";
    
        mysql_query($addClient) or die(mysql_error());
    
    
    
    ?>
    PHP:
     
    zer0c00l, Nov 6, 2010 IP
  4. zer0c00l

    zer0c00l Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    would it be something like this

    
    
    $status = $POST['status']
    $updateStatus ="UPDATE laptops SET status='out' WHERE tnumber=$tnumber" ;
    
    mysql_query($updateStatus) or die(mysql error());
    
    
    PHP:
    although i never defined 'status' in the form.
     
    Last edited: Nov 6, 2010
    zer0c00l, Nov 6, 2010 IP
  5. helloguys12345678

    helloguys12345678 Greenhorn

    Messages:
    50
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    20
    #5
    Remove this line:
    $status = $POST['status']
    PHP:
    Why would you need to use $_POST['status'] ? You already know the hardcoded value of status which is "out"
     
    helloguys12345678, Nov 7, 2010 IP