2017-01-26 43 views
-1

我对这段代码失去了主意。它似乎工作。没有错误,但是没有任何东西被添加到mysql数据库。我检查了它正在工作的db连接。数据没有被添加到SQL中,没有错误

我希望你能帮上忙。

报名表

<html> 
<head> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="initial-scale=1, maximum-scale=1"> 
     <title>Create a Company</title> 
     <link rel="stylesheet" type="text/css" href="/PITAKER/V2/css.css"/> 

</head> 
<body> 
    <h2 class="header"> Create a Company </h2> 
    <form action="processcompany.php" method="post"> 

     <input class="entry" placeholder="Company Name" name="companyname" type="text" required="required"><br> 
     <input class="entry" placeholder="GST/VAT/ABN/TAX No" name="taxno" type="text" required="required"><br> 
     <input class="entry" placeholder="Address" name="address1" type="text" value=""><br> 
     <input class="entry" placeholder="Suburb/County" name="suburb" type="text" value=""><br> 
     <input class="entry" placeholder="State" name="state" type="text" value=""><br> 
     <input class="entry" placeholder="Post/Zip Code" name="postcode" type="text" value=""><br> 
    <input class="entry" placeholder="Country" name="country" type="text" value=""><br> 
    <input class="entry" placeholder="Primary Contact" name="primarycontact" type="text" value=""><br> 
    <input class="entry" placeholder="Primary Email" name="primaryemail" type="text" value=""><br> 
    <input class="entry" placeholder="Subscription Type" name="subscriptiontype" type="text" value=""><br> 
    <input class="entry" placeholder="Subscription Status" name="subscriptionstatus" type="text" value=""><br> 
    <input class="entry" placeholder="Subscription End Date" name="subscriptionenddate" type="text" value=""><br> 

     <input class="button" type="submit"> 

    </form> 
</body> 

php脚本加入到MySQL

<?php 
include 'db.php'; 

$companyname=$_POST['companyname']; 
$taxno=$_POST['taxno']; 
$address1=$_POST['address1']; 
$suburb=$_POST['suburb']; 
$state=$_POST['state']; 
$postcode=$_POST['postcode']; 
$country=$_POST['country']; 
$primarycontact=$_POST['primarycontact']; 
$primaryemail=$_POST['primaryemail']; 
$subscriptiontype=$_POST['subscriptiontype']; 
$subscriptionstatus=$_POST['subscriptionstatus']; 
$subscriptionenddate=$_POST['subscriptionenddate']; 


$sql = "INSERT INTO `companies` 
       (`companyid` , `accountno` , `companyname` , 
       `taxno` , `address1` , `address2` , `suburb` , 
       `state` , `postcode` , `country` , `primarycontact` , 
       `primaryemail` , `subscriptiontype` , `subscriptionstatus` , 
       `subscriptionenddate` , `datecreated`) 
     VALUES (NULL , 
       NULL , 
      '".mysqli_real_escape_string($conn,$_POST['companyname'])."' , 
      '".mysqli_real_escape_string($conn,$_POST['taxno'])."' , 
      '".mysqli_real_escape_string($conn,$_POST['address1'])."' , 
      NULL , 
      '".mysqli_real_escape_string($conn,$_POST['suburb'])."' , 
      '".mysqli_real_escape_string($conn,$_POST['state'])."' , 
      '".mysqli_real_escape_string($conn,$_POST['postcode'])."' , 
      '".mysqli_real_escape_string($conn,$_POST['country'])."' , 
      '".mysqli_real_escape_string($conn,$_POST['primarycontact'])."' , 
      '".mysqli_real_escape_string($conn,$_POST['primaryemail'])."' , 
      '".mysqli_real_escape_string($conn,$_POST['subscriptiontype'])."' , 
      '".mysqli_real_escape_string($conn,$_POST['subscriptionstatus'])."' , 
      '".mysqli_real_escape_string($conn,$_POST['subscriptionenddate'])."')"; 

mysqli_query($conn, $sql); 

mysqli_close($conn); 
?> 

我的数据库文件

<?php 

$dbhost = 'localhost'; 
$dbuser = 'root'; 
$dbpass = ''; 
$db = 'a34511pidata'; 

$conn = mysqli_connect($dbhost,$dbuser,$dbpass); 
mysqli_select_db($conn, $db); 

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

echo "Database Connected Ok.."; 

?> 
+5

有没有错误,因为你不检查他们 –

+0

通常的程序进行故障排除这类问题是转储或回声查询文本字符串的内容(在你的情况下'$ sql')。通常您可以立即发现问题。如果没有,请尝试通过某种MySQL客户端发出查询,并查看会发生什么。另外,'mysqli_query()'如果成功则返回true。读这个。 http://php.net/manual/en/mysqli.query.php如果失败,你需要检查错误。读这个。 http://php.net/manual/en/mysqli.error.php如果你在完成这些事情后遇到麻烦,欢迎您提出更详细的问题。 –

+0

你的脚本存在[SQL注入攻击]的风险(http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) 看看发生了什么事[小鲍比表](http://bobby-tables.com/)即使 [如果你是逃避投入,它不安全!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets -around-mysql-real-escape-string) 使用[prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly

回答

0

如果你让你的代码可读性,你会发现它更容易o调试。

此外,当您使用mysqli_时,您需要在发出查询后检查错误。

你也应该使用准备好的和参数化查询,以保护自己免受SQL注入攻击

你实际的错误是,你必须列出16列和15只在值列表中的变量。

我用NOW()填充缺失的列我假设这将是datecreated列所需要的。

<?php 
include 'db.php'; 

$sql = "INSERT INTO `companies` 
       (`companyid`, `accountno`, `companyname` , 
       `taxno` , `address1`, `address2`, `suburb` , 
       `state` , `postcode`, `country`, `primarycontact` , 
       `primaryemail`, `subscriptiontype` , 
       `subscriptionstatus`, `subscriptionenddate`,  
       `datecreated`) 
     VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,NOW())"; 

$stmt = $conn->prepare($sql); 
if (! $stmt) { 
    echo $stmt->error; 
    exit; 
} 

// I dont know your data type so you may need to check the data types I used here 
$stmt->bind_param('iisssssssssssss', 
        NULL, NULL, 
        $_POST['companyname'], 
        $_POST['taxno'], 
        $_POST['address1'], 
        NULL , 
        $_POST['suburb'], 
        $_POST['state'], 
        $_POST['postcode'], 
        $_POST['country'], 
        $_POST['primarycontact'], 
        $_POST['primaryemail'], 
        $_POST['subscriptiontype'], 
        $_POST['subscriptionstatus'], 
        $_POST['subscriptionenddate'] 
       ); 



$stmt->execute(); 
if (! $stmt) { 
    echo $stmt->error; 
    exit; 
} 

mysqli_close($conn); 
?> 

我必须说,我觉得这是一个有点古怪要传递NULL作为列accountno第二个参数即。这可能是你的下一个错误,但这取决于你的数据库中如何定义该列。

+0

谢谢,我修改了代码,它的工作。尽管NULL不起作用。我为此做了一个工作。我正在尝试添加,以便将此数据添加到此表中,并将更多数据添加到另一个表中。我试图修改代码,但它仍然没有被添加到第二个表中,我没有收到错误消息。向您展示我的工作的最佳方式是什么?修改原来的帖子?感谢您的帮助。 – Raggs

+0

提问另一个问题,因为这个答案 – RiggsFolly

+0

http://stackoverflow.com/help/someone-answers – RiggsFolly

相关问题