简体   繁体   中英

Insert form data in one table and then update an enum value in a different table?

I am using a code to insert a users form data into the database, that bit works fine. However, i also want to run another query and update an enum value 'form2_completed?' from No to Yes. I have added in the update query and now for some reason the script is not working and says 'ERROR'

Can someone please show me where i am going wrong. thanks

<?php
    session_start();

    $db_hostname = 'localhost';
    $db_database = 'hewden1'; 
    $db_username = 'root';
    $db_password = '';

    $db_server = mysql_connect($db_hostname, $db_username, $db_password)    
            or die("Unable to connect to MySQL: " . mysql_error());

    mysql_select_db($db_database)   
    or die("Unable to select database: " . mysql_error());

     $cname       = $_POST['cname'];
       $creg      = $_POST['creg'];  
       $address      = $_POST['address'];  
       $post      = $_POST['post'];  
       $contactn      = $_POST['contactn'];
       $contactt      = $_POST['contactt'];
       $email      = $_POST['email'];
       $vat     = $_POST['vat'];

        $ipaddress = $_SERVER["REMOTE_ADDR"];


    $sql="INSERT INTO supplier_registration (company_name, company_reg_number, company_address, company_postcode, contact_name, contact_number, contact_email, company_vat_number, date_time, user_ip)
    VALUES ('$cname', '$creg', '$address', '$post', '$contactn', '$contactt', '$email', '$vat', NOW(), '$ipaddress')";

    $sql="UPDATE supplier_session SET form2_completed? = 'Yes' WHERE form2_completed? = 'No'";

    $result = mysql_query($sql); 

    if($result){

    $success = "<div class='success'></div>"; // use the $success
    //encode the URL parameter as : 

    $success = urlencode($success);

    header("Location: index.php?success=$success");


    }else {
    echo "ERROR";
    }
    ?>

You are overwriting the variable $sql and not running INSERT . Try:

$sql="INSERT INTO supplier_registration (company_name, company_reg_number, company_address, company_postcode, contact_name, contact_number, contact_email, company_vat_number, date_time, user_ip)
    VALUES ('$cname', '$creg', '$address', '$post', '$contactn', '$contactt', '$email', '$vat', NOW(), '$ipaddress')";

$result = mysql_query($sql); 

$sql="UPDATE supplier_session SET form2_completed? = 'Yes' WHERE form2_completed? = 'No'";

$result = mysql_query($sql); 

Please note that the method you have used is deprecated from php 5.5.0. so i suggest you consider mysqli or PDO. examples can be found in below php manual links

http://www.php.net/manual/en/mysqli.query.php

http://www.php.net/manual/en/pdo.query.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM