简体   繁体   English

将表单数据插入一个表中,然后将特定的表单数据插入表2中并更新枚举值?

[英]insert form data into one table, then insert specific form data in to table 2 and update enum value?

i am trying to insert my form data into my table 'supplier_registration', however this is not working, i am getting mysql 'ERROR', I am not sure why this is happening. 我正在尝试将表格数据插入到我的表“ supplier_registration”中,但是这不起作用,我正在获取mysql“ ERROR”,我不确定为什么会这样。

I am also wanting to insert the same form data into a second table 'supplier_session', but this time only inserting specific bits of the form data, '$cname' and '$creg'. 我也想将相同的表单数据插入第二个表“ supplier_session”,但是这次仅插入表单数据的特定位“ $ cname”和“ $ creg”。

When a user lands on the page there ip address is inserted into the table 'supplier_session' so when this form is submitted i also want the supplier_session table to update an enum value 'form1_complete' from no to yes, where that user's ip or 'user_IP' exists in the table row. 当用户登陆页面时,会将IP地址插入到表“ supplier_session”中,因此,在提交此表单时,我还希望Supplier_session表将枚举值“ form1_complete”从“否”更新为“是”,该用户的ip或“ user_IP” '存在于表格行中。 i am trying to d this by running three querys but its not working, please can someone show me where i am going wrong. 我正在尝试通过运行三个查询来解决这个问题,但无法正常工作,请有人能告诉我我要去哪里了。 Thanks 谢谢

Database: 数据库:

ID(AI) | company_name | company_reg_number | company_incorp_date | company_address | company_postcode | contact_name | contact_number | contact_email | company_vat_number | date_time | user_IP

Code: 码:

<?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'];
  $contacte      = $_POST['contacte'];
   $vat     = $_POST['vat'];
    $incorp      = $_POST['incorp'];

    $ipaddress = $_SERVER["REMOTE_ADDR"];




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

$result = mysql_query($sql); 


$qry2 = "INSERT INTO supplier_session (company_reg_number, company_name) VALUES('$creg','$cname') WHERE user_IP = '$ipaddress'";

 $result = @mysql_query($qry2);

$qry3="UPDATE supplier_session SET form1_completed = 'Yes' WHERE form1_completed = 'No' AND user_IP = '$ipaddress'";

$result = mysql_query($q); 



if($result){

header("Location: index.php?registration=sucess");


}else {
echo "ERROR";
}
?>

html: 的HTML:

<form name="myForm" id="myform" action="company_info_process.php" onsubmit="return validateForm()" method="post">

<input type="text" id="field_cname" name="cname" class="field_cname">

<input type='text' name='creg' id="field_creg">

<input type='text' name='incorp' id="field_incorp">

<input type='text' name='vat' id="field_vat">

<input type='text' name='contactn' id="field_contactn">

<input type="text" name="contactt" id="field_contactt">

<input type="text" id="field_contacte" name="contacte">

<input type="text" id="field_address" name="address">

<input type="text" id="field_post" name="post"/>

Firstly, you are calling the wrong variable in your third query. 首先,您在第三个查询中调用了错误的变量。

Change $result = mysql_query($q); 更改$result = mysql_query($q); to $result = mysql_query($qry3); $result = mysql_query($qry3);

Also, with the INSERT() syntax, you cannot have a WHERE() clause. 同样,使用INSERT()语法,您不能具有WHERE()子句。 The only time you will find INSERT that has a WHERE clause is when you are using an INSERT INTO...SELECT statement. 你会发现唯一的一次INSERT ,有一个WHERE子句是当你使用INSERT INTO...SELECT语句。

Besides that, the WHERE clause is mostly used when doing a SELECT() or UPDATE() . 除此之外,在执行SELECT()UPDATE()时,通常使用WHERE子句。

Quick sidenote: I noticed you don't have a closing </form> tag. 快速旁注:我注意到您没有结束</form>标记。 If that is your actual code, add it. 如果这是您的实际代码,请添加它。

Use error reporting at the top of your page(s): 在页面顶部使用错误报告:

error_reporting(E_ALL);
ini_set('display_errors', 1);

Plus, use: 另外,使用:

else{
    echo mysql_errno($db_server) . ": " . mysql_error($db_server) . "\n";
    }

Instead of: 代替:

else {
echo "ERROR";
}

Visit PHP.net for more information on error reporting. 访问PHP.net,以获取有关错误报告的更多信息。


Footnotes: 脚注:

mysql_* functions deprecation notice: mysql_*函数弃用通知:

http://www.php.net/manual/en/intro.mysql.php http://www.php.net/manual/zh/intro.mysql.php

This extension is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future. 自PHP 5.5.0起不推荐使用该扩展,不建议编写新代码,因为将来会删除该扩展。 Instead, either the mysqli or PDO_MySQL extension should be used. 相反,应使用mysqliPDO_MySQL扩展名。 See also the MySQL API Overview for further help while choosing a MySQL API. 另请参见MySQL API概述,以获取选择MySQL API时的更多帮助。

These functions allow you to access MySQL database servers. 这些功能使您可以访问MySQL数据库服务器。 More information about MySQL can be found at » http://www.mysql.com/ . 有关MySQL的更多信息,请参见» http://www.mysql.com/

Documentation for MySQL can be found at » http://dev.mysql.com/doc/ . 可以在» http://dev.mysql.com/doc/中找到MySQL的文档。

Where you run your call to mysql_query , output any error messages it generates: 在您运行对mysql_query的调用的地方,输出它生成的所有错误消息:

$result = mysql_query($sql) or die(mysql_error());

Should give some indication of which query is causing the error and why. 应该给出哪个查询导致错误以及为什么的指示。

Edit: Also, Noticed you have a WHERE clause within an INSERT statement, you can't do that. 编辑:另外,请注意您在INSERT语句中有WHERE子句,您不能这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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