以下代码无效

时间:2017-03-19 05:41:24

标签: php html wampserver

这是我的表单代码我希望用户输入的数据应该保存在mysql中...使用wamp ..任何解决方案plzz..please重写代码并粘贴它。 .................................................. ..............................
    

</head>
<body>

<form action="form.php" method="post">

    Project_Manager:<input type="text" name="ProjectManager">
<br />

    Email_Address:<input type="text" name="EmailAddress">
<br />

    Company_Name:<input type="text" name="CompanyName">
<br />

    Company_Street_Address:<input type="text" name="CompanyStreetAddress">
<br />

    Company_Suit:<input type="text" name="CompanySuit">
<br />

    City:<input type="text" name="City">
<br />

    State:<input type="text" name="State">
<br />

    Zip_code:<input type="text" name="Zipcode">
<br />

    Company_Phone:<input type="text" name="CompanyPhone">
<br />

    Billing_Name:<input type="text" name="BillingName">
<br />

    Billing_Company_Street_Address:<input type="text" name="BillingCompanyStreetAddress">
<br />

    Billing_Company_Suite:<input type="text" name="BillingCompanySuite">
<br />

    Billing_City:<input type="text" name="BillingCity">
<br />

    Billing_State:<input type="text" name="BillingState">
<br />

    Billing_Zip_Code:<input type="text" name="BillingZipCode">
<br />

    Billing_Phone:<input type="text" name="BillingPhone">
<br />


    <input type="submit" value="submit">

    </form>



<?php

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

    $con = mysql_connect("localhost","root","");
    if(!$con){
         die('Could not connect: ' . mysql_error());
    }

mysql_select_db("ecc1",$con);   


$sql = "INSERT INTO client(
    Project_Manager,
    Email_Address, 
    Company_Name, 
    Company_Street_Address, 
    Company_Suit, 
    City, 
    State, 
    Zip_code, 
    Company_Phone, 
    Billing_Name, 
    Billing_Company_Street_Address, 
    Billing_Company_Suite, 
    Billing_City, 
    Billing_State, 
    Billing_Zip_Code, 
    Billing_Phone
)
    VALUES( '$_POST[ProjectManager]',
            '$_POST[EmailAddress]',
            '$_POST[CompanyName]',
            '$_POST[CompanyStreetAddress]',
            '$_POST[CompanySuit]',
            '$_POST[City]',
            '$_POST[State]',
            '$_POST[Zipcode]',
            '$_POST[CompanyPhone]',
            '$_POST[BillingName]',
            '$_POST[BillingCompanyStreetAddress]',
            '$_POST[BillingCompanySuite]',
            '$_POST[BillingCity ]',
            '$_POST[BillingState]',
            '$_POST[BillingZipCode]',
            '$_POST[BillingPhone]',
            )";

mysql_query($sql,$con);
mysql_close($con);
    }

?>

</body>

</html>

3 个答案:

答案 0 :(得分:0)

如果表单已提交,则很可能是您的测试失败:

if(isset($_POST['submit'])){
    // this is probably false
}

如果您添加了姓名=&#34;请提交&#34;在提交按钮中,您可能会得到您期望的结果。

<input type="submit" name="submit" value="submit">

答案 1 :(得分:0)

好吧,对于初学者,您使用已弃用的mysql_*扩展程序,该扩展程序将不再适用于新版本的 PHP ,其次,您正在使用查询数据库时 MySQLi 标准。

您的整个脚本应如下所示:

<强> HTML

<input type="submit" value="true" name="submit"> <!-- set the name of submit button to "submit" so your if statement works !-->

PHP

$conn = new mysqli('localhost', 'root', '', 'ecc1'); // use mysqli instead of mysql. mysql is deprecated

if(isset($_POST['submit'])){
    $sql = "INSERT INTO client(
        Project_Manager,
        Email_Address, 
        Company_Name, 
        Company_Street_Address, 
        Company_Suit, 
        City, 
        State, 
        Zip_code, 
        Company_Phone, 
        Billing_Name, 
        Billing_Company_Street_Address, 
        Billing_Company_Suite, 
        Billing_City, 
        Billing_State, 
        Billing_Zip_Code, 
        Billing_Phone) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; // your sql
    $stmt = $conn->prepare($sql); // to protect against sql injection
    $stmt->bind_param("ssssssiisssssii", $_POST['ProjectManager'], $_POST'[EmailAddress'], $_POST['CompanyName'], $_POST['CompanyStreetAddress'], $_POST['CompanySuit'], $_POST['City'], $_POST['State'], $_POST['Zipcode'], $_POST['CompanyPhone'],  $_POST['BillingName'], $_POST['BillingCompanyStreetAddress'], $_POST['BillingCompanySuite'], $_POST['BillingCity'], $_POST['BillingState'], $_POST['BillingZipCode'], $_POST['BillingPhone']); // set all the values of the row columns

    if($stmt->execute()){ // if the query is successful
        die("Query Successful"); // output success message
    }
}

现在,假设我已经正确完成了所有事情(包括计算过多的帖子数据),查询执行,您应该收到消息:

  

查询成功

请注意:对于"ssssssiisssssii"s表示字符串,i表示整数。

如果出现任何问题,或者您收到任何错误,请将其留在评论中。

要了解有关预准备语句和 MySQLi 本身的更多信息,请查看 PHP 手册: http://php.net/manual/en/mysqli.prepare.php

答案 2 :(得分:0)

<?php

$sql = "INSERT INTO client(
    Project_Manager,
    Email_Address, 
    Company_Name, 
    Company_Street_Address, 
    Company_Suit, 
    City, 
    State, 
    Zip_code, 
    Company_Phone, 
    Billing_Name, 
    Billing_Company_Street_Address, 
    Billing_Company_Suite, 
    Billing_City, 
    Billing_State, 
    Billing_Zip_Code, 
    Billing_Phone
)
    VALUES( '{$_POST['ProjectManager']}',
            '{$_POST['EmailAddress']}',
            '{$_POST['CompanyName']}',
            '{$_POST['CompanyStreetAddress']}',
            '{$_POST['CompanySuit']}',
            '{$_POST['City']}',
            '{$_POST['State']}',
            '{$_POST['Zipcode']}',
            '{$_POST['CompanyPhone']}',
            '{$_POST['BillingName']}',
            '{$_POST['BillingCompanyStreetAddress']}',
            '{$_POST['BillingCompanySuite']}',
            '{$_POST['BillingCity']}',
            '{$_POST['BillingState']}',
            '{$_POST['BillingZipCode']}',
            '{$_POST['BillingPhone']}',
            )";

mysql_query($sql,$con);
mysql_close($con);
    }

?>